mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-29 05:15:12 +00:00

## 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: ```py relese_version :"0.0is 64" ``` 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 the `String` 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` and `comment_ranges` as methods and restrict access to the parsed source code. fixes: #11736 ## Test Plan - [x] Add a test case for `F632` rule and update the snapshot - [x] Check all affected rules - [x] No ecosystem changes
34 lines
406 B
Python
34 lines
406 B
Python
if x is "abc":
|
|
pass
|
|
|
|
if 123 is not y:
|
|
pass
|
|
|
|
if 123 is \
|
|
not y:
|
|
pass
|
|
|
|
if "123" is x < 3:
|
|
pass
|
|
|
|
if "123" != x is 3:
|
|
pass
|
|
|
|
if ("123" != x) is 3:
|
|
pass
|
|
|
|
if "123" != (x is 3):
|
|
pass
|
|
|
|
{2 is
|
|
not ''}
|
|
|
|
{2 is
|
|
not ''}
|
|
|
|
# Regression test for
|
|
if values[1is not None ] is not '-':
|
|
pass
|
|
|
|
# Regression test for https://github.com/astral-sh/ruff/issues/11736
|
|
variable: "123 is not y"
|