8000 Emit a friendlier warning on invalid exclude regex, instead of a stac… · python/mypy@301c3b6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 301c3b6

Browse files
Emit a friendlier warning on invalid exclude regex, instead of a stacktrace (#19102)
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 ``` --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 93ff49d commit 301c3b6

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

mypy/modulefinder.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -684,12 +684,27 @@ def matches_exclude(
684684
if fscache.isdir(subpath):
685685
subpath_str += "/"
686686
for exclude in excludes:
687-
if re.search(exclude, subpath_str):
688-
if verbose:
689-
print(
690-
f"TRACE: Excluding {subpath_str} (matches pattern {exclude})", file=sys.stderr
687+
try:
688+
if re.search(exclude, subpath_str):
689+
if verbose:
690+
print(
691+
f"TRACE: Excluding {subpath_str} (matches pattern {exclude})",
692+
file=sys.stderr,
693+
)
694+
return True
695+
except re.error as e:
696+
print(
697+
f"error: The exclude {exclude} is an invalid regular expression, because: {e}"
698+
+ (
699+
"\n(Hint: use / as a path separator, even if you're on Windows!)"
700+
if "\\" in exclude
701+
else ""
691702
)
692-
return True
703+
+ "\nFor more information on Python's flavor of regex, see:"
704+
+ " https://docs.python.org/3/library/re.html",
705+
file=sys.stderr,
706+
)
707+
sys.exit(2)
693708
return False
694709

695710

@@ -786,7 +801,8 @@ def default_lib_path(
786801
print(
787802
"error: --custom-typeshed-dir does not point to a valid typeshed ({})".format(
788803
custom_typeshed_dir
789-
)
804+
),
805+
file=sys.stderr,
790806
)
791807
sys.exit(2)
792808
else:

0 commit comments

Comments
 (0)
0