File tree Expand file tree Collapse file tree 3 files changed +10
-10
lines changed Expand file tree Collapse file tree 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 )
@@ -479,9 +479,11 @@ def _parse_string(
479
479
)
480
480
except SyntaxError as exc :
481
481
# If the type annotations are misplaced for some reason, we do not want
482
- # to fail the entire parsing of the file, so we need to retry the parsing without
483
- # type comment support.
484
- if exc .args [0 ] != MISPLACED_TYPE_ANNOTATION_ERROR or not type_comments :
482
+ # to fail the entire parsing of the file, so we need to retry the
483
+ # parsing without type comment support. We use a heuristic for
484
+ # determining if the error is due to type annotations.
485
+ type_annot_related = re .search (r"#\s+type:" , exc .text or "" )
486
+ if not (type_annot_related and type_comments ):
485
487
raise
486
488
487
489
parser_module = get_parser_module (type_comments = False )
Original file line number Diff line number Diff line change @@ -914,12 +914,6 @@ def test_module_build_dunder_file() -> None:
914
914
assert module .path [0 ] == collections .__file__
915
915
916
916
917
- @pytest .mark .xfail (
918
- reason = (
919
- "The builtin ast module does not fail with a specific error "
920
- "for syntax error caused by invalid type comments."
921
- ),
922
- )
923
917
def test_parse_module_with_invalid_type_comments_does_not_crash ():
924
918
node = builder .parse (
925
919
"""
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