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
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions mypy/modulefinder.py
Original file line number Diff line number Diff line change
Expand Up @@ -684,12 +684,27 @@ def matches_exclude(
if fscache.isdir(subpath):
subpath_str += "/"
for exclude in excludes:
if re.search(exclude, subpath_str):
if verbose:
print(
f"TRACE: Excluding {subpath_str} (matches pattern {exclude})", file=sys.stderr
try:
if re.search(exclude, subpath_str):
if verbose:
print(
f"TRACE: Excluding {subpath_str} (matches pattern {exclude})",
file=sys.stderr,
)
return True
except re.error as e:
print(
f"error: The exclude {exclude} is an invalid regular expression, because: {e}"
+ (
"\n(Hint: use / as a path separator, even if you're on Windows!)"
if "\\" in exclude
else ""
)
return True
+ "\nFor more information on Python's flavor of regex, see:"
+ " https://docs.python.org/3/library/re.html",
file=sys.stderr,
)
sys.exit(2)
return False


Expand Down Expand Up @@ -786,7 +801,8 @@ def default_lib_path(
print(
"error: --custom-typeshed-dir does not point to a valid typeshed ({})".format(
custom_typeshed_dir
)
),
file=sys.stderr,
)
sys.exit(2)
else:
Expand Down