-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
new rule - fstring syntax in non-fstring #8151
Comments
Interesting! Although not sure how to reliably detect this. One way could be to pass the string as a f-string to the parser and check if there are any |
I'm worried about false positives on this one. |
i feel like they would be rare as long as it only complains when the expression inside the |
I would find this useful. Coming from Rust and JavaScript, I wrote |
Can you give an example where there would be false positives? Maybe we could only highlight when the node within |
My common use false positive x = "{foo}"
x.format(foo="bar") |
@zanieb in that case it shouldn't trigger a false positive if the rule works how i suggested in #8151 (comment), because there's no variable in scope called imo the |
I agree requiring the variables to be in scope is a reasonable heuristic for avoiding false positives. I'm not sure I can come up with a common one that meets that requirement. I found https://github.com/PrefectHQ/prefect/blob/f654a1b23eb5662afb5b8bfd08c9c86668e817ee/tests/agent/test_agent_queue_selection.py#L36 but that probably ought to be an f-string anyway. We can probably hint something else when |
We could just skip if the said string is part of a large I think then we could support this as:
What do you think? @zanieb |
I'd welcome this. Cross reference peterjc/flake8-sfs#5 See also the opposite request, f-strings without any variables in them - briefly implemented as flake8-pie's PIE782 code but withdrawn due to false positives. Cross reference peterjc/flake8-sfs#3 and sbdchd/flake8-pie#24 |
We do support this - https://docs.astral.sh/ruff/rules/f-string-missing-placeholders/ :) |
pyanalyze supports this check: https://github.com/quora/pyanalyze/blob/647c64e8c9b4eaa81a5309d8e76e72bafe61d687/pyanalyze/name_check_visitor.py#L3096 Here are the heuristics it uses to avoid false positives:
|
I'm supportive of adding this with the above heuristics. |
<!-- Thank you for contributing to Ruff! To help us out with reviewing, please consider the following: - Does this pull request include a summary of the change? (See below.) - Does this pull request include a descriptive title? - Does this pull request include references to any relevant issues? --> ## Summary Fixes #8151 This PR implements a new rule, `RUF027`. ## What it does Checks for strings that contain f-string syntax but are not f-strings. ### Why is this bad? An f-string missing an `f` at the beginning won't format anything, and instead treat the interpolation syntax as literal. ### Example ```python name = "Sarah" dayofweek = "Tuesday" msg = "Hello {name}! It is {dayofweek} today!" ``` It should instead be: ```python name = "Sarah" dayofweek = "Tuesday" msg = f"Hello {name}! It is {dayofweek} today!" ``` ## Heuristics Since there are many possible string literals which contain syntax similar to f-strings yet are not intended to be, this lint will disqualify any literal that satisfies any of the following conditions: 1. The string literal is a standalone expression. For example, a docstring. 2. The literal is part of a function call with keyword arguments that match at least one variable (for example: `format("Message: {value}", value = "Hello World")`) 3. The literal (or a parent expression of the literal) has a direct method call on it (for example: `"{value}".format(...)`) 4. The string has no `{...}` expression sections, or uses invalid f-string syntax. 5. The string references variables that are not in scope, or it doesn't capture variables at all. 6. Any format specifiers in the potential f-string are invalid. ## Test Plan I created a new test file, `RUF027.py`, which is both an example of what the lint should catch and a way to test edge cases that may trigger false positives.
to avoid false positives, it should only complain when the expression inside the
{}
refers to an existing variable. that way, most usages of theformat
function won't trigger false positives:The text was updated successfully, but these errors were encountered: