Use Tokens
from parsed type annotation or parsed source
#11740
Merged
+159
−31
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
This PR fixes a bug where the checker would require the tokens for an invalid offset w.r.t. the source code.
Taking the source code from the linked issue as an example:
Now, this isn't really a valid type annotation but that's what this PR is fixing. Regardless of whether it's valid or not, Ruff shouldn't panic.
The checker would visit the parsed type annotation (
0.0is 64
) and try to detect any violations. Certain rule logic requests the tokens for the same but it would fail because the lexer would only have theString
token considering original source code. This worked before because the lexer was invoked again for each rule logic.The solution is to store the parsed type annotation on the checker if it's in a typing context and use the tokens from that instead if it's available. This is enforced by creating a new API on the checker to get the tokens.
But, this means that there are two ways to get the tokens via the checker API. I want to restrict this in a follow-up PR (#11741) to only expose
tokens
andcomment_ranges
as methods and restrict access to the parsed source code.fixes: #11736
Test Plan
F632
rule and update the snapshot