Skip to content

Commit 2074d00

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 2074d00

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

astroid/builder.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
# The comment used to select a statement to be extracted
3434
# when calling extract_node.
3535
_STATEMENT_SELECTOR = "#@"
36-
MISPLACED_TYPE_ANNOTATION_ERROR = "misplaced type annotation"
3736

3837
if PY312_PLUS:
3938
warnings.filterwarnings("ignore", "invalid escape sequence", SyntaxWarning)
@@ -478,9 +477,11 @@ def _parse_string(
478477
)
479478
except SyntaxError as exc:
480479
# 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:
480+
# to fail the entire parsing of the file, so we need to retry the
481+
# parsing without type comment support. We use a heuristic for
482+
# determining if the error is due to type annotations.
483+
type_annot_related = "# type:" in (exc.text or "")
484+
if not (type_annot_related and type_comments):
484485
raise
485486

486487
parser_module = get_parser_module(type_comments=False)

0 commit comments

Comments
 (0)