Skip to content

Emit a friendlier warning on invalid exclude regex, instead of a stacktrace #19102

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 19, 2025

Conversation

wyattscarpenter
Copy link
Contributor

@wyattscarpenter wyattscarpenter commented May 17, 2025

If an invalid exclude is used, the error message

Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "C:\Users\wyatt\files\gits\wyattscarpenter\!!! contributory forks\mypy\mypy\__main__.py", line 37, in <module>
    console_entry()
  File "C:\Users\wyatt\files\gits\wyattscarpenter\!!! contributory forks\mypy\mypy\__main__.py", line 15, in console_entry
    main()
  File "C:\Users\wyatt\files\gits\wyattscarpenter\!!! contributory forks\mypy\mypy\main.py", line 89, in main
    sources, options = process_options(args, stdout=stdout, stderr=stderr, fscache=fscache)
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\wyatt\files\gits\wyattscarpenter\!!! contributory forks\mypy\mypy\main.py", line 1531, in process_options
    targets = create_source_list(special_opts.files, options, fscache)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\wyatt\files\gits\wyattscarpenter\!!! contributory forks\mypy\mypy\find_sources.py", line 48, in create_source_list
    sub_sources = finder.find_sources_in_dir(path)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\wyatt\files\gits\wyattscarpenter\!!! contributory forks\mypy\mypy\find_sources.py", line 121, in find_sources_in_dir
    if matches_exclude(subpath, self.exclude, self.fscache, self.verbosity >= 2):
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\wyatt\files\gits\wyattscarpenter\!!! contributory forks\mypy\mypy\modulefinder.py", line 687, in matches_exclude
    if re.search(exclude, subpath_str):
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\wyatt\AppData\Local\Programs\Python\Python312\Lib\re\__init__.py", line 177, in search
    return _compile(pattern, flags).search(string)
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\wyatt\AppData\Local\Programs\Python\Python312\Lib\re\__init__.py", line 307, in _compile
    p = _compiler.compile(pattern, flags)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\wyatt\AppData\Local\Programs\Python\Python312\Lib\re\_compiler.py", line 750, in compile
    p = _parser.parse(p, flags)
        ^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\wyatt\AppData\Local\Programs\Python\Python312\Lib\re\_parser.py", line 979, in parse
    p = _parse_sub(source, state, flags & SRE_FLAG_VERBOSE, 0)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\wyatt\AppData\Local\Programs\Python\Python312\Lib\re\_parser.py", line 460, in _parse_sub
    itemsappend(_parse(source, state, verbose, nested + 1,
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\wyatt\AppData\Local\Programs\Python\Python312\Lib\re\_parser.py", line 544, in _parse
    code = _escape(source, this, state)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\wyatt\AppData\Local\Programs\Python\Python312\Lib\re\_parser.py", line 443, in _escape
    raise source.error("bad escape %s" % escape, len(escape))
re.error: bad escape \p at position 2

now looks like this:

error: The exclude ..\publish is an invalid regular expression, because: bad escape \p at position 2
(Hint: use / as a path separator, even if you're on Windows!)
For more information on Python's flavor of regex, see: https://docs.python.org/3/library/re.html

This comment has been minimized.

based on Jelle's review, I realized that mypy is pretty consistent about sending usage errors to stderr, and so I have fixed both my code and the code I modelled by code after to do that instead
Copy link
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

@JelleZijlstra JelleZijlstra merged commit 301c3b6 into python:master May 19, 2025
18 checks passed
@wyattscarpenter wyattscarpenter deleted the friendre branch May 19, 2025 16:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy