[flake8-annotations
] Fix parsing of "complex" forward reference type annotations in any-type
(ANN401)
#14700
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #14695.
The root cause was tricky to find.
"Complex" forward reference type annotations are handled differently. Forward references are "complex" in this case when the raw contents (without quotes) of the expression with the parsed contents is not contained in the string literal.
ruff/crates/ruff_python_parser/src/typing.rs
Line 65 in 84748be
Annotations are recursively parsed, for instance:
"'int'"
(complex) =>"int"
(simple)The raw contents of the expression "in\x74" is not contained in the string literal
int
and hence requires multiple complex parsing steps:"'in\x74'"
(complex) ->"in\x74"
(complex). This is rare, and probably that's why the bug went unnoticed.Parsed type annotations are cached by their start offset:
ruff/crates/ruff_linter/src/checkers/ast/mod.rs
Line 2544 in 84748be
As it turned out, for these complex annotations the starting offset was not properly updated (removing the quotes), as well already do for the simple annotations. This caused an infinite loop, and eventually a stack overflow.
Test Plan
Extended the test suite with regression tests.