From 2074d00141c6945cb837e54e2334e21d90eb5420 Mon Sep 17 00:00:00 2001 From: Artem Yurchenko Date: Mon, 23 Sep 2024 18:43:02 -0700 Subject: [PATCH] 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. --- astroid/builder.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/astroid/builder.py b/astroid/builder.py index ddc1aee712..ba4b760014 100644 --- a/astroid/builder.py +++ b/astroid/builder.py @@ -33,7 +33,6 @@ # The comment used to select a statement to be extracted # when calling extract_node. _STATEMENT_SELECTOR = "#@" -MISPLACED_TYPE_ANNOTATION_ERROR = "misplaced type annotation" if PY312_PLUS: warnings.filterwarnings("ignore", "invalid escape sequence", SyntaxWarning) @@ -478,9 +477,11 @@ def _parse_string( ) except SyntaxError as exc: # If the type annotations are misplaced for some reason, we do not want - # to fail the entire parsing of the file, so we need to retry the parsing without - # type comment support. - if exc.args[0] != MISPLACED_TYPE_ANNOTATION_ERROR or not type_comments: + # to fail the entire parsing of the file, so we need to retry the + # parsing without type comment support. We use a heuristic for + # determining if the error is due to type annotations. + type_annot_related = "# type:" in (exc.text or "") + if not (type_annot_related and type_comments): raise parser_module = get_parser_module(type_comments=False)