File tree 3 files changed +10
-10
lines changed 3 files changed +10
-10
lines changed Original file line number Diff line number Diff line change 12
12
13
13
import ast
14
14
import os
15
+ import re
15
16
import textwrap
16
17
import types
17
18
import warnings
33
34
# The comment used to select a statement to be extracted
34
35
# when calling extract_node.
35
36
_STATEMENT_SELECTOR = "#@"
36
- MISPLACED_TYPE_ANNOTATION_ERROR = "misplaced type annotation"
37
37
38
38
if PY312_PLUS :
39
39
warnings .filterwarnings ("ignore" , "invalid escape sequence" , SyntaxWarning )
@@ -478,9 +478,11 @@ def _parse_string(
478
478
)
479
479
except SyntaxError as exc :
480
480
# 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 ):
484
486
raise
485
487
486
488
parser_module = get_parser_module (type_comments = False )
Original file line number Diff line number Diff line change @@ -883,12 +883,6 @@ def test_module_build_dunder_file() -> None:
883
883
assert module .path [0 ] == collections .__file__
884
884
885
885
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
- )
892
886
def test_parse_module_with_invalid_type_comments_does_not_crash ():
893
887
node = builder .parse (
894
888
"""
Original file line number Diff line number Diff line change @@ -1304,6 +1304,10 @@ def test_type_comments_invalid_expression() -> None:
1304
1304
def test_type_comments_invalid_function_comments () -> None :
1305
1305
module = builder .parse (
1306
1306
"""
1307
+ def func(
1308
+ # type: () -> int # inside parentheses
1309
+ ):
1310
+ pass
1307
1311
def func():
1308
1312
# type: something completely invalid
1309
1313
pass
You can’t perform that action at this time.
0 commit comments