Skip to content

B031, B905, B911: recognize itertools imported under another name - #644

Open
LuisFigueroaG wants to merge 1 commit into
PyCQA:mainfrom
LuisFigueroaG:fix/itertools-module-alias
Open

LuisFigueroaG wants to merge 1 commit into
PyCQA:mainfrom
LuisFigueroaG:fix/itertools-module-alias

Conversation

@LuisFigueroaG

Copy link
Copy Markdown
Contributor

B031, B905 and B911 only look at itertools.<name>(...) with the literal module name, so code that imports it under another name gets the wrong result in both directions:

import itertools as it

for chunk in it.batched(rows, 100):  # not reported by B911
    ...

for key, group in it.groupby(rows, key):  # not reported by B031 if group is used twice
    ...

total = sum(int(d) * f for d, f in zip(isbn, it.cycle((1, 3))))  # B905

The last one is from pydantic-extra-types' isbn.py (and routing_number.py has the same shape). B905 already skips itertools.cycle(), count() and repeat() because they never run out, so adding strict=True there would make the zip() raise.

_is_module_name() now treats a name bound by import itertools as ... anywhere in the module as itertools, for the groupby check in B031, the infinite iterator exception in B905 and the attribute form of B911. It reuses the lookup _module_binding_is_from already does to accept batched = it.batched, moved into _module_import_names(). Nothing else changes:

  • itertools.<name> is still checked even without an import, as before.
  • other modules (more_itertools.batched(), other.groupby()) are still ignored.
  • from itertools import batched as bt isn't covered; bare names still go through _bare_name_is_from.

On the files in my corpus (~60,000 from Ubuntu's Python 3.12 stdlib and dist-packages, plus the repos and virtualenvs in my workspace), only 11 import itertools under another name. Running those through main and this branch, the two B905 reports above go away and nothing else changes for B031, B905 or B911.

New cases are in tests/eval_files/b031_module_alias.py, b905_module_alias.py and b911_module_alias.py. All three fail on main, and each also checks that a call that shouldn't change is still reported or still ignored. tox (py311–py315, pep8_naming, mypy) and pre-commit pass.

B031, B905 and B911 only treat a call like groupby(), batched() or
cycle() as coming from itertools when it's written as itertools.<name>.
After `import itertools as it`, it.batched() and it.groupby() were never
checked, and zip(xs, it.cycle(...)) was reported by B905 even though
itertools.cycle() is infinite and strict=True would make it raise.

Names bound by `import itertools as ...` now count as itertools there,
reusing the lookup _module_binding_is_from already does for
`batched = it.batched`.
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.

1 participant