Repository navigation
B031, B905, B911: recognize itertools imported under another name - #644
Open
LuisFigueroaG wants to merge 1 commit into
Open
LuisFigueroaG wants to merge 1 commit into
LuisFigueroaG wants to merge 1 commit into
Conversation
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`.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:The last one is from pydantic-extra-types'
isbn.py(androuting_number.pyhas the same shape). B905 already skipsitertools.cycle(),count()andrepeat()because they never run out, so addingstrict=Truethere would make thezip()raise._is_module_name()now treats a name bound byimport itertools as ...anywhere in the module asitertools, for thegroupbycheck in B031, the infinite iterator exception in B905 and the attribute form of B911. It reuses the lookup_module_binding_is_fromalready does to acceptbatched = it.batched, moved into_module_import_names(). Nothing else changes:itertools.<name>is still checked even without an import, as before.more_itertools.batched(),other.groupby()) are still ignored.from itertools import batched as btisn'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
itertoolsunder another name. Running those throughmainand 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.pyandb911_module_alias.py. All three fail onmain, 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.