-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[ruff
] Implement global-or-nonlocal-in-branch
(RUF103
)
#19569
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very cool! This is the first rule I've seen using the control flow graph, and it's closing quite a long-standing rule request too.
I'll leave the review to @dylwil3 since he wrote the CFG code, but I had one small suggestion about the rule code in passing.
crates/ruff_linter/src/codes.rs
Outdated
(Ruff, "100") => (RuleGroup::Stable, rules::ruff::rules::UnusedNOQA), | ||
(Ruff, "101") => (RuleGroup::Stable, rules::ruff::rules::RedirectedNOQA), | ||
(Ruff, "102") => (RuleGroup::Preview, rules::ruff::rules::InvalidRuleCode), | ||
(Ruff, "103") => (RuleGroup::Preview, rules::ruff::rules::GlobalOrNonlocalInBranch), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not quite sure, but I think the RUF100
rules are a bit special. It looks like they're all related to noqa codes. I'd suggest pushing this up to RUF066
, which is the next available code after RUF065
, which is currently used in #19518.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you so much for reviewing my PR!
This is my first contribution to this repository, so I wasn’t familiar with the rules for choosing a code number.
I’ll update the rule to use RUF066.
|
code | total | + violation | - violation | + fix | - fix |
---|---|---|---|---|---|
RUF066 | 15 | 15 | 0 | 0 | 0 |
Thanks @ya7on ! Have you gotten a chance to look through the ecosystem results? It seems like there are cases where we have a violation even when the assignment is in the same branch (unless I'm missing something). Let me know if the ecosystem results look okay to you and I can take another look. Also, work on the control flow graph got paused at some point so it's very possible there are some bugs - let me know if you run into trouble or if something doesn't seem to be working right! |
Thanks for the review! I only tested the rule against the unit tests so far, didn't realize I should also check the ecosystem results. I'll run that and see what comes up. If I find issues, I'll fix them — and if it looks like a CFG bug, I'll come back with questions. Appreciate you pointing this out! |
Thanks! One thing, just in case it wasn't clear:
You don't have to run the ecosystem check yourself - you can click through the results in this comment #19569 (comment) |
Oh, i see. Thank you! I'll check those results. |
It looks like there is indeed a bug in the control-flow graph. I tried running def test_func():
a = True
b = True
if a:
global GLOBAL_NAME
GLOBAL_NAME = 1
if b:
return The generated CFG contains only 2 blocks Block 0 includes: a = True
b = True
if a: But block 1 is completely empty. It doesnt include the What would you recommend i do in this case? UPD: after a little debugging i find out following.
|
Sorry this is my bad - I did not quite remember how much of the CFG work had been merged in. It looks like we never actually merged in the work on if and match statements #17268 (so not much functionality is available then...) I don't have an immediate answer for when work is planned to continue on that. It's up to you whether you'd like to wait a bit, or whether you'd like to try using the available methods on the |
@ya7on can you help me understand what the status of this PR is? Are you planning additional work or are you waiting on input a review from us? |
@MichaReiser Hi! I am currently not working on this PR as i am waiting for updates or fixes to the CFG. I am happy to return to this once that work is unblocked, but in the meantime i think it makes sense to close this PR for now. Thanks again for the review! |
Apologies again @ya7on - I do hope to return to the CFG work at some point! |
Summary
Implement the
global-or-nonlocal-in-branch
rule (RUF103
), which detects cases where a variabledeclared as
global
ornonlocal
within a function is assigned in a separate branch of the functionwithout an explicit declaration along that code path. Such usage can be confusing and is often unintended.
Example:
Closes #6470.
Test Plan
Added unit tests covering:
nonlocal
usage (mirrors the global behavior).