Skip to content

Use Tokens from parsed type annotation or parsed source#11740

Merged
dhruvmanila merged 3 commits intomainfrom
dhruv/parsed-tokens
Jun 5, 2024
Merged

Use Tokens from parsed type annotation or parsed source#11740
dhruvmanila merged 3 commits intomainfrom
dhruv/parsed-tokens

Conversation

@dhruvmanila
Copy link
Copy Markdown
Member

@dhruvmanila dhruvmanila commented Jun 4, 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:

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

  • Add a test case for F632 rule and update the snapshot
  • Check all affected rules
  • No ecosystem changes

@dhruvmanila dhruvmanila added the bug Something isn't working label Jun 4, 2024
@github-actions
Copy link
Copy Markdown
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 requested a review from MichaReiser June 5, 2024 06:46
Base automatically changed from dhruv/type-annotation-parsed to main June 5, 2024 07:29
@dhruvmanila dhruvmanila force-pushed the dhruv/parsed-tokens branch from 1d129f0 to f8260cb Compare June 5, 2024 07:30
@dhruvmanila
Copy link
Copy Markdown
Member Author

I've added test cases for all affected rules as well. Even though the type annotations themselves aren't valid, Ruff shouldn't panic.

@dhruvmanila dhruvmanila enabled auto-merge (squash) June 5, 2024 07:47
@dhruvmanila dhruvmanila merged commit b021b5b into main Jun 5, 2024
@dhruvmanila dhruvmanila deleted the dhruv/parsed-tokens branch June 5, 2024 07:50
dhruvmanila added a commit that referenced this pull request Jun 5, 2024
## 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`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Rule F632 cause panic

2 participants