Skip to content

Commit

Permalink
change the type annotation error heuristic
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
temyurchenko committed Sep 24, 2024
1 parent c8e8831 commit 2074d00
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions astroid/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 2074d00

Please sign in to comment.