Repository navigation
B018, B031, B911: count parameters when checking whether a name is shadowed - #640
Conversation
…adowed B018, B031 and B911 skip a bare map(), groupby() or batched() when the module binds that name anywhere, so a local variable named filter already counts. A function or lambda parameter with the same name didn't, so an executor's map() or a groupby() passed in as an argument was still taken for the builtin/itertools one. Parameters now count as bindings there, like local variables do.
cooperlees
left a comment
There was a problem hiding this comment.
This review was written by Claude (Anthropic's AI assistant) on behalf of @cooperlees.
Thanks @LuisFigueroaG. This is a small, well-reasoned change. A parameter is a binding just as much as a local assignment is, so it's consistent for _module_name_is_from to count it. Using ast.arg covers positional, keyword-only, *args, **kwargs and lambda parameters in one go, which is nice. The corpus run and the "still reported" control case in each eval file are appreciated.
What I checked
- On the PR branch,
pytest tests -k "not selfclean"gives 164 passed, 3 skipped. CI and pre-commit are green. def g(**map),def h(*groupby)andlambda map: map(...)all behave as expected.- A module-level
from itertools import batchedstill takes precedence through_module_binding_is_from, so a module-levelbatched(range(3), 2)is still reported even when some function has abatchedparameter. ✅
Non-blocking notes (both follow from the existing "nested scopes are intentionally not modeled" design, so I'm not asking you to change anything here):
-
Module-wide suppression gets broader. The helper walks the whole module, so a parameter in one function now hides a bare call in an unrelated one:
def get(self, filter=None): # very common in ORM/API code ... def g(items): filter(None, items) # B018 on main, silent on this branch
This already happened with
filter = ...locals. But parameters namedfilter,mapand so on are much more common than assignments, so in practice this widens the false negatives more than the old local-variable rule did. Your corpus run suggests the effect is negligible, so I'm fine with it. Maybe mention it in the changelog wording, or keep it in mind if anyone ever adds per-scope resolution. -
The opposite case is still a false positive (already on
main, not caused by this PR):from itertools import batched def chunks(items, batched): return batched(items, 2) # still B911, though `batched` here is the parameter
This would need real scope tracking to fix, so it's out of scope for this PR. I'm only noting it for completeness.
Approving.
B018, B031 and B911 skip a bare
map(),filter(),groupby(),batched()and so on when the module binds that name anywhere (#608, #609)._module_name_is_fromlooks fordef/class, imports, assignment targets,except ... asand match captures, but not function parameters. So a local variable namedfilteralready counts, while a parameter with the same name doesn't:A parameter is just as local as the variable in the first function, so the call there may do real work, and a
batched()orgroupby()that's passed in may not takestrict=or return single-use groups at all._module_name_is_fromnow treats anast.argwith that name as a binding. That covers positional, keyword-only,*args/**kwargsand lambda parameters. Nothing else changes:from itertools import batchedand the other imports from the source module still count, through_module_binding_is_fromas before.On about 60,000 files (Ubuntu's Python 3.12 stdlib and dist-packages, plus the repos and virtualenvs in my workspace), I ran the 545 that have a
def/lambdaline with one of these names throughmainand this branch, and B018, B031 and B911 report the same outside the new eval files. So this is mostly about parameters matching local variables.New cases are in
tests/eval_files/b018_shadowed_parameter.py,b031_shadowed_parameter.pyandb911_shadowed_parameter.py. All three fail onmain, and each also checks that a call that isn't shadowed is still reported. tox (py311–py315, pep8_naming, mypy) and pre-commit pass.