From c40552e3e5ef51ee8e5408c82295e5c19e051dde Mon Sep 17 00:00:00 2001 From: wyattscarpenter Date: Sat, 17 May 2025 09:20:22 -0700 Subject: [PATCH 1/3] emit a friendlier warning on invalid exclude regex, instead of a stacktrace --- mypy/modulefinder.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/mypy/modulefinder.py b/mypy/modulefinder.py index 3040276dea6dd..b83767ce5639d 100644 --- a/mypy/modulefinder.py +++ b/mypy/modulefinder.py @@ -684,12 +684,19 @@ 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 - ) - return True + 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) + if '\\' in exclude: + print("(Hint: use / as a path separator, even if you're on Windows!)") + print("For more information on Python's flavor of regex, see: https://docs.python.org/3/library/re.html") + sys.exit(2) return False From 3c203aa3a9200e6935689a1c8672ccf345bb96bb Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 17 May 2025 16:22:22 +0000 Subject: [PATCH 2/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- mypy/modulefinder.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/mypy/modulefinder.py b/mypy/modulefinder.py index b83767ce5639d..d96580fc87153 100644 --- a/mypy/modulefinder.py +++ b/mypy/modulefinder.py @@ -688,14 +688,17 @@ def matches_exclude( if re.search(exclude, subpath_str): if verbose: print( - f"TRACE: Excluding {subpath_str} (matches pattern {exclude})", file=sys.stderr + 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) - if '\\' in exclude: + if "\\" in exclude: print("(Hint: use / as a path separator, even if you're on Windows!)") - print("For more information on Python's flavor of regex, see: https://docs.python.org/3/library/re.html") + print( + "For more information on Python's flavor of regex, see: https://docs.python.org/3/library/re.html" + ) sys.exit(2) return False From d0931c3a4e1147578e31c6283bd17141fe0c4a4e Mon Sep 17 00:00:00 2001 From: wyattscarpenter Date: Mon, 19 May 2025 06:36:39 -0700 Subject: [PATCH 3/3] send errors to stderr 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 --- mypy/modulefinder.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/mypy/modulefinder.py b/mypy/modulefinder.py index d96580fc87153..4cbeed9d14ff4 100644 --- a/mypy/modulefinder.py +++ b/mypy/modulefinder.py @@ -693,11 +693,16 @@ def matches_exclude( ) return True except re.error as e: - print(f"error: The exclude {exclude} is an invalid regular expression, because:", e) - if "\\" in exclude: - print("(Hint: use / as a path separator, even if you're on Windows!)") print( - "For more information on Python's flavor of regex, see: https://docs.python.org/3/library/re.html" + 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 "" + ) + + "\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 @@ -796,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: