Skip to content

Conversation

dhruvmanila
Copy link
Member

Summary

This PR is a follow-up to #11740 to restrict access to the Parsed output by replacing the parsed API function with a more specific one. Currently, that is comment_ranges but the linked PR exposes a tokens method.

The main motivation is so that there's no way to get an incorrect information from the checker. And, it also encapsulates the source of the comment ranges and the tokens itself. This way it would become easier to just update the checker if the source for these information changes in the future.

Test Plan

cargo insta test

@dhruvmanila dhruvmanila added the internal An internal refactor or improvement label Jun 4, 2024
@dhruvmanila dhruvmanila requested a review from AlexWaygood as a code owner June 4, 2024 16:56
@dhruvmanila dhruvmanila requested a review from MichaReiser June 4, 2024 16:56
@dhruvmanila dhruvmanila force-pushed the dhruv/checker-comment-ranges branch from adece38 to 73bd3ed Compare June 4, 2024 17:12
Copy link
Contributor

github-actions bot commented Jun 4, 2024

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@dhruvmanila dhruvmanila force-pushed the dhruv/parsed-tokens branch from 1d129f0 to f8260cb Compare June 5, 2024 07:30
Base automatically changed from dhruv/parsed-tokens to main June 5, 2024 07:50
dhruvmanila added a commit that referenced this pull request Jun 5, 2024
## 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
@dhruvmanila dhruvmanila force-pushed the dhruv/checker-comment-ranges branch from 73bd3ed to 86f4019 Compare June 5, 2024 08:20
@dhruvmanila dhruvmanila enabled auto-merge (squash) June 5, 2024 08:21
@dhruvmanila dhruvmanila merged commit 2e0a975 into main Jun 5, 2024
@dhruvmanila dhruvmanila deleted the dhruv/checker-comment-ranges branch June 5, 2024 08:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
internal An internal refactor or improvement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants