Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update F541 to use new tokens to detect f-strings #7292

Closed
Tracked by #7299
dhruvmanila opened this issue Sep 12, 2023 · 0 comments · Fixed by #7327
Closed
Tracked by #7299

Update F541 to use new tokens to detect f-strings #7292

dhruvmanila opened this issue Sep 12, 2023 · 0 comments · Fixed by #7327
Assignees
Labels
python312 Related to Python 3.12 rule Implementing or modifying a lint rule

Comments

@dhruvmanila
Copy link
Member

F541 checks for useless f-strings i.e., f-strings without any placeholders.

The rule needs to be updated to use the new f-string tokens to detect that.

Possible solution

Once we encounter the FStringStart token and if there are no placeholders inside the f-string, then the next token is guaranteed going to a FStringMiddle token containing the entire content of the f-string and then we'll get the FStringEnd token. For example,

f"we're inside a f-string"

The above code will emit the following tokens:

0 FStringStart 2
2 FStringMiddle {
    value: "we're inside a f-string",
    is_raw: false,
} 25
25 FStringEnd 26

And, for triple-quoted f-strings:

f"""this is a triple
quoted f-string with
multiple lines of text"""

We get the following tokens:

0 FStringStart 4
4 FStringMiddle {
    value: "this is a triple\nquoted f-string with\nmultiple lines of text",
    is_raw: false,
} 64
64 FStringEnd 67

I propose to create two edits:

  1. Remove the f-string prefix. This can be extracted using the FStringStart token
  2. Unescape the f-string using the FStringMiddle token i.e., replacing {{ and }} with { and }
@dhruvmanila dhruvmanila added python312 Related to Python 3.12 rule Implementing or modifying a lint rule labels Sep 12, 2023
@dhruvmanila dhruvmanila self-assigned this Sep 13, 2023
@dhruvmanila dhruvmanila linked a pull request Sep 13, 2023 that will close this issue
dhruvmanila added a commit that referenced this issue Sep 18, 2023
## Summary

This PR updates the `F541` rule to use the new f-string tokens.

## Test Plan

Add new test case and uncomment a broken test case.

fixes: #7292
dhruvmanila added a commit that referenced this issue Sep 19, 2023
## Summary

This PR updates the `F541` rule to use the new f-string tokens.

## Test Plan

Add new test case and uncomment a broken test case.

fixes: #7292
dhruvmanila added a commit that referenced this issue Sep 20, 2023
## Summary

This PR updates the `F541` rule to use the new f-string tokens.

## Test Plan

Add new test case and uncomment a broken test case.

fixes: #7292
dhruvmanila added a commit that referenced this issue Sep 21, 2023
## Summary

This PR updates the `F541` rule to use the new f-string tokens.

## Test Plan

Add new test case and uncomment a broken test case.

fixes: #7292
dhruvmanila added a commit that referenced this issue Sep 22, 2023
## Summary

This PR updates the `F541` rule to use the new f-string tokens.

## Test Plan

Add new test case and uncomment a broken test case.

fixes: #7292
dhruvmanila added a commit that referenced this issue Sep 22, 2023
## Summary

This PR updates the `F541` rule to use the new f-string tokens.

## Test Plan

Add new test case and uncomment a broken test case.

fixes: #7292
dhruvmanila added a commit that referenced this issue Sep 22, 2023
## Summary

This PR updates the `F541` rule to use the new f-string tokens.

## Test Plan

Add new test case and uncomment a broken test case.

fixes: #7292
dhruvmanila added a commit that referenced this issue Sep 26, 2023
## Summary

This PR updates the `F541` rule to use the new f-string tokens.

## Test Plan

Add new test case and uncomment a broken test case.

fixes: #7292
dhruvmanila added a commit that referenced this issue Sep 27, 2023
## Summary

This PR updates the `F541` rule to use the new f-string tokens.

## Test Plan

Add new test case and uncomment a broken test case.

fixes: #7292
dhruvmanila added a commit that referenced this issue Sep 28, 2023
## Summary

This PR updates the `F541` rule to use the new f-string tokens.

## Test Plan

Add new test case and uncomment a broken test case.

fixes: #7292
dhruvmanila added a commit that referenced this issue Sep 29, 2023
## Summary

This PR updates the `F541` rule to use the new f-string tokens.

## Test Plan

Add new test case and uncomment a broken test case.

fixes: #7292
dhruvmanila added a commit that referenced this issue Sep 29, 2023
## Summary

This PR updates the `F541` rule to use the new f-string tokens.

## Test Plan

Add new test case and uncomment a broken test case.

fixes: #7292
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
python312 Related to Python 3.12 rule Implementing or modifying a lint rule
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant