Skip to content

Commit 547b56f

Browse files
committed
change the type annotation error heuristic
The previous one depended on the message from "typed_ast", which is not used anymore. Instead, we check if there is a "# type:" substring in the source line of the exception. This can yield some false positives, but probably rarely.
1 parent c8e8831 commit 547b56f

File tree

2 files changed

+6
-10
lines changed

2 files changed

+6
-10
lines changed

astroid/builder.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import ast
1414
import os
15+
import re
1516
import textwrap
1617
import types
1718
import warnings
@@ -33,7 +34,6 @@
3334
# The comment used to select a statement to be extracted
3435
# when calling extract_node.
3536
_STATEMENT_SELECTOR = "#@"
36-
MISPLACED_TYPE_ANNOTATION_ERROR = "misplaced type annotation"
3737

3838
if PY312_PLUS:
3939
warnings.filterwarnings("ignore", "invalid escape sequence", SyntaxWarning)
@@ -478,9 +478,11 @@ def _parse_string(
478478
)
479479
except SyntaxError as exc:
480480
# If the type annotations are misplaced for some reason, we do not want
481-
# to fail the entire parsing of the file, so we need to retry the parsing without
482-
# type comment support.
483-
if exc.args[0] != MISPLACED_TYPE_ANNOTATION_ERROR or not type_comments:
481+
# to fail the entire parsing of the file, so we need to retry the
482+
# parsing without type comment support. We use a heuristic for
483+
# determining if the error is due to type annotations.
484+
type_annot_related = re.search(r"#\s+type:", exc.text or "")
485+
if not (type_annot_related and type_comments):
484486
raise
485487

486488
parser_module = get_parser_module(type_comments=False)

tests/test_builder.py

-6
Original file line numberDiff line numberDiff line change
@@ -883,12 +883,6 @@ def test_module_build_dunder_file() -> None:
883883
assert module.path[0] == collections.__file__
884884

885885

886-
@pytest.mark.xfail(
887-
reason=(
888-
"The builtin ast module does not fail with a specific error "
889-
"for syntax error caused by invalid type comments."
890-
),
891-
)
892886
def test_parse_module_with_invalid_type_comments_does_not_crash():
893887
node = builder.parse(
894888
"""

0 commit comments

Comments
 (0)