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

RUF027 false positive with reserved keywords, how to handle? #9908

Closed
sshishov opened this issue Feb 9, 2024 · 4 comments
Closed

RUF027 false positive with reserved keywords, how to handle? #9908

sshishov opened this issue Feb 9, 2024 · 4 comments

Comments

@sshishov
Copy link

sshishov commented Feb 9, 2024

Hi,

I would like to ask how to handle the cases where the "variable" for interpolation is used in the constant or other variable beforehand, example below

CONSTANT_CACHE_KEY = 'my:{id}:here'  # <-- RUF027 is triggered here (Possible f-string without an `f` prefix)

def some_function(value: str) -> None:
    cache_key = CONSTANT_CACHE_KEY.format(id=value)
    ...  # later on we use this cache key and `value` here is like a variable for cache key

I have tried to "hack" slightly by using:

CONSTANT_CACHE_KEY = f'my:{{id}}:here'  # <-- F541 is triggered here (f-string without any placeholders)

UPDATE: this is happening only with reserved keywords like id or filter. If variable does not exist, then this error is not raised.

Should it be somehow treated or handled? Or for this cases should we add # noqa: RUF027? Or maybe the good suggestion would be to rename the variable inside the string from id to object_id?

ruff version: 0.2.1

@sshishov sshishov changed the title RUF027 false positive with constants, how to handle? RUF027 false positive with reserved keywords, how to handle? Feb 9, 2024
@sshishov
Copy link
Author

sshishov commented Feb 9, 2024

Just a side note, we have updated all usages of prefix:{id}:suffix constants to prefix:{object_id}:suffix and now we do not have any issues with amazing ruff 🎉

@Avasam
Copy link

Avasam commented Feb 9, 2024

As a precision, id, filter, etc., are not reserved keywords, they are builtins (global names from the stdlib).

RUF027's heuristic detects when you try to use an existing symbols in brackets.

my_value = 5
message = "I have {my_value} things"
print(message)

But since id is always defined, it'll always trigger in your case.
It's just like https://docs.astral.sh/ruff/rules/builtin-variable-shadowing/

Maybe RUF027 could respect https://docs.astral.sh/ruff/settings/#lint_flake8-builtins_builtins-ignorelist ?

@AlexWaygood
Copy link
Member

I think this is already fixed on main, due to:

It should be included in the next release!

@charliermarsh
Copy link
Member

👍 I believe this will be fixed in the next release -- sorry about that!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants