Skip to content

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

Merged
JelleZijlstra merged 3 commits into
python:masterfrom
wyattscarpenter:friendre
May 19, 2025
Merged

Emit a friendlier warning on invalid exclude regex, instead of a stacktrace#19102
JelleZijlstra merged 3 commits into
python:masterfrom
wyattscarpenter:friendre

Conversation

@wyattscarpenter

@wyattscarpenterwyattscarpenter commented May 17, 2025

Copy link
Copy Markdown
Contributor

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

@github-actions

This comment has been minimized.

Comment threadmypy/modulefinder.py Outdated
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
@github-actions

Copy link
Copy Markdown
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:masterMay 19, 2025
@wyattscarpenter
wyattscarpenter deleted the friendre branch May 19, 2025 16:09
Sign up for freeto 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

@wyattscarpenter@JelleZijlstra