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 for non-runtime context binding #14000

Open
Daverball opened this issue Oct 30, 2024 · 7 comments
Open

RUF027 False positive for non-runtime context binding #14000

Daverball opened this issue Oct 30, 2024 · 7 comments
Labels
rule Implementing or modifying a lint rule

Comments

@Daverball
Copy link
Contributor

Daverball commented Oct 30, 2024

This might be incredibly easy to fix depending on how this check is implemented. Currently RUF027 can be triggered by something like:

from typing import TYPE_CHECKING
if TYPE_CHECKING:
    from datetime import date

path = "foo/{date}/"  # RUF027

Now, to be fair, this would also be a false positive for a runtime binding. But this seems like an easy low hanging fruit to reduce the false positive rate at least a little bit.

Although a more reliable solution long-term would be to add a setting for specifying additional methods which expect a template string.

@MichaReiser
Copy link
Member

Now, to be fair, this would also be a false positive for a runtime binding. But this seems like an easy low hanging fruit to reduce the false positive rate at least a little bit.

Can you talk me through the heuristic you have in mind to identifying the false positives? Is it that ruff should ignore all strings in function decorator positions if the function has a parameter with the same name?

@MichaReiser MichaReiser added the rule Implementing or modifying a lint rule label Oct 30, 2024
@Daverball
Copy link
Contributor Author

Daverball commented Oct 30, 2024

It's very simple. From what I understand, this rule triggers when a string template contains a name that would reference a binding if it were changed into a f-string. But if the binding is not in a runtime context, then turning it into a f-string would trigger a NameError (or UnboundLocalError if the name is shadowed later in the same scope).

So all I'm suggesting is to ignore references that can't actually be used at runtime.

@Daverball
Copy link
Contributor Author

@MichaReiser I've simplified the example, so it's just about the runtime context of the binding and there's no additional visual noise, even if it would trigger additional violations for the unused import.

@AlexWaygood
Copy link
Member

@Daverball, is this something that's actually causing false-positive errors to be emitted on your code, or is this just a theoretical concern? It feels like a bit of an edge case

@Daverball
Copy link
Contributor Author

@AlexWaygood I am seeing some false positives in my code that would go away with this heuristic, yes, otherwise I would not have reported it. Since you're already keeping track of the runtime context of bindings in the semantic model anyways this seemed like a fairly cheap win.

Although as I've said, being able to specify functions in addition to gettext/logging/FastAPI.path where this check is skipped would be more reliable.

@AlexWaygood
Copy link
Member

@AlexWaygood I am seeing some false positives in my code that would go away with this heuristic, yes, otherwise I would not have reported it.

Thanks for confirming. We get more issues reported than you might think which are actually purely theoretical, so I just wanted to check :-)

I agree that RUF027 has too many false positives right now, and I'd happily accept any PR that's not too complicated and manages to reduce the number of false positives from the rule! Thanks for opening the issue with the suggestion.

@Daverball
Copy link
Contributor Author

I just realized that this might come for free with #12927, since this will add simulate_runtime_load to the semantic model, the current implementation appears to be using lookup_symbol which handles deletion properly, but doesn't care about which context the lookup happens from, it just assumes it's being called at the right time during semantic analysis.

So if that ever gets merged I will open a PR to change lookup_symbol to simulate_runtime_load.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rule Implementing or modifying a lint rule
Projects
None yet
Development

No branches or pull requests

3 participants