Skip to content

B018, B031, B911: count parameters when checking whether a name is shadowed - #640

Merged
cooperlees merged 1 commit into
PyCQA:mainfrom
LuisFigueroaG:fix/shadowed-names-parameters
Nov 18, 2026
Merged

cooperlees merged 1 commit into
PyCQA:mainfrom
LuisFigueroaG:fix/shadowed-names-parameters

Conversation

@LuisFigueroaG

Copy link
Copy Markdown
Contributor

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_from looks for def/class, imports, assignment targets, except ... as and match captures, but not function parameters. So a local variable named filter already counts, while a parameter with the same name doesn't:

def test_copied_filter(self):
    _, filter = self.init_filter()
    filter()  # not reported, as in #609


def run_all(func, items, map):
    map(func, items)  # B018, but this may be an executor's map()


def chunks(items, batched):
    for chunk in batched(items, 10):  # B911
        ...


def summarize(rows, key, groupby):
    for _, group in groupby(rows, key):  # B031 if group is used twice
        ...

A parameter is just as local as the variable in the first function, so the call there may do real work, and a batched() or groupby() that's passed in may not take strict= or return single-use groups at all.

_module_name_is_from now treats an ast.arg with that name as a binding. That covers positional, keyword-only, *args/**kwargs and lambda parameters. Nothing else changes:

  • names the module never binds are still reported.
  • from itertools import batched and the other imports from the source module still count, through _module_binding_is_from as before.
  • B905, B910, B912 and B913 use the module-level check, where parameters (like other locals) don't count, so they're unaffected.

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/lambda line with one of these names through main and 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.py and b911_shadowed_parameter.py. All three fail on main, and each also checks that a call that isn't shadowed is still reported. tox (py311–py315, pep8_naming, mypy) and pre-commit pass.

…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 cooperlees left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) and lambda map: map(...) all behave as expected.
  • A module-level from itertools import batched still takes precedence through _module_binding_is_from, so a module-level batched(range(3), 2) is still reported even when some function has a batched parameter. ✅

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):

  1. 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 named filter, map and 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.

  2. 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.

@cooperlees
cooperlees merged commit 509c260 into PyCQA:main Nov 18, 2026
6 checks passed
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

Successfully merging this pull request may close these issues.

2 participants