Repository navigation
B905, B910: don't flag zip() and defaultdict() the module binds to something else #610
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
Open
LuisFigueroaG
wants to merge
2
commits into
PyCQA:main
Choose a base branch
from
LuisFigueroaG:fix/b905-b910-shadowed-names
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+229
−22
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| """ | ||
| Should emit: | ||
| B018 - on line 11 | ||
| """ | ||
|
|
||
| # An annotated assignment of builtins.map to map still means the lazy builtin. | ||
| import builtins | ||
|
|
||
| map: object = builtins.map | ||
|
|
||
| map(print, [1, 2]) # B018: 0, "Call" |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| """ | ||
| Should emit: | ||
| B031 - on line 15 | ||
| """ | ||
|
|
||
| # An annotated assignment of itertools.groupby to groupby still means | ||
| # itertools.groupby. | ||
| import itertools | ||
|
|
||
| groupby: object = itertools.groupby | ||
|
|
||
| for _, v in groupby(None, None): | ||
| if v: | ||
| pass | ||
| if v: # B031: 7, "v" | ||
| pass |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| """ | ||
| Should emit: | ||
| B910 - on line 18 | ||
| """ | ||
|
|
||
| # A module-level function named zip is not the builtin zip(). | ||
| from collections import defaultdict | ||
| from itertools import zip_longest | ||
|
|
||
|
|
||
| def zip(*iterables): | ||
| return zip_longest(*iterables, fillvalue="") | ||
|
|
||
|
|
||
| _ = zip("a", "bc") | ||
|
|
||
| # defaultdict() is still collections.defaultdict | ||
| _ = defaultdict(int) # B910: 4 |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| """ | ||
| Should emit: | ||
| B905 - on line 11 | ||
| """ | ||
|
|
||
| # Assigning builtins.zip to zip still means the builtin. | ||
| import builtins | ||
|
|
||
| zip = builtins.zip | ||
|
|
||
| _ = zip("a", "bc") # B905: 4 |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| """ | ||
| Should emit: | ||
| B905 - on line 13 | ||
| """ | ||
|
|
||
| # An annotated assignment of builtins.zip to zip still means the builtin, and a | ||
| # bare annotation doesn't bind zip at all. | ||
| import builtins | ||
|
|
||
| zip: object = builtins.zip | ||
| zip: object | ||
|
|
||
| _ = zip("a", "bc") # B905: 4 |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| """ | ||
| Should emit: | ||
| B910 - on line 15 | ||
| """ | ||
|
|
||
| # zip() from another library is not the builtin, even after importing that. | ||
| from builtins import zip | ||
| from collections import defaultdict | ||
|
|
||
| from mylib import zip | ||
|
|
||
| _ = zip("a", "bc") | ||
|
|
||
| # defaultdict() is still collections.defaultdict | ||
| _ = defaultdict(int) # B910: 4 |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| """ | ||
| Should emit: | ||
| B905 - on line 25 | ||
| B910 - on line 26 | ||
| """ | ||
|
|
||
| # Only module-level bindings replace zip and defaultdict. A method or class | ||
| # attribute of that name, or an import that only runs on Python 2, leaves the | ||
| # module's zip() the builtin. | ||
| import sys | ||
| from collections import defaultdict | ||
|
|
||
| if sys.version_info < (3,): | ||
| from itertools import izip as zip | ||
|
|
||
|
|
||
| class Archive: | ||
| zip = None | ||
| defaultdict = None | ||
|
|
||
| def zip(self, other): | ||
| return self | ||
|
|
||
|
|
||
| _ = zip("a", "bc") # B905: 4 | ||
| _ = defaultdict(int) # B910: 4 |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| """ | ||
| Should emit: | ||
| B905 - on line 18 | ||
| """ | ||
|
|
||
| # A module-level class named defaultdict is not collections.defaultdict. | ||
| import collections | ||
|
|
||
|
|
||
| class defaultdict(collections.defaultdict): | ||
| def __missing__(self, key): | ||
| return self.default_factory(key) | ||
|
|
||
|
|
||
| _ = defaultdict(int) | ||
|
|
||
| # zip() is still the builtin | ||
| _ = zip("a", "bc") # B905: 4 |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| """ | ||
| Should emit: | ||
| B910 - on line 11 | ||
| """ | ||
|
|
||
| # Assigning collections.defaultdict to defaultdict still means collections. | ||
| import collections | ||
|
|
||
| defaultdict = collections.defaultdict | ||
|
|
||
| _ = defaultdict(int) # B910: 4 |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| """ | ||
| Should emit: | ||
| B910 - on line 13 | ||
| """ | ||
|
|
||
| # An annotated assignment of collections.defaultdict to defaultdict still means | ||
| # collections.defaultdict, and a bare annotation doesn't bind it at all. | ||
| import collections | ||
|
|
||
| defaultdict: type = collections.defaultdict | ||
| defaultdict: type | ||
|
|
||
| _ = defaultdict(int) # B910: 4 |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| """ | ||
| Should emit: | ||
| B905 - on line 13 | ||
| """ | ||
|
|
||
| # defaultdict() from another library is not collections.defaultdict. | ||
| from mylib import defaultdict | ||
|
|
||
| _ = defaultdict(int) | ||
| _ = defaultdict(int, {"a": 1}) | ||
|
|
||
| # zip() is still the builtin | ||
| _ = zip("a", "bc") # B905: 4 |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| """ | ||
| Should emit: | ||
| B911 - on line 12 | ||
| """ | ||
|
|
||
| # An annotated assignment of itertools.batched to batched still means | ||
| # itertools.batched. | ||
| import itertools | ||
|
|
||
| batched: object = itertools.batched | ||
|
|
||
| batched(range(3), 2) # B911: 0 |
Oops, something went wrong.
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.
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.
Good catch, fixed in be37743:
_binds_from_sourcenow treats anAnnAssignwith a value like a plain assignment, and a barezip: objectstill isn't a binding. The helper is shared, so this also applied to B018, B031 and B911 (the latter two since 26.10.27). Each has a*_shadowed_alias_annotated.pycase now.