-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Extending redefined-outer-name check to cover the case of loop variables #8663
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
Closed
Lucas-C
wants to merge
32
commits into
pylint-dev:main
from
Lucas-C:extending-redefined-outer-name-to-loop-variables
Closed
Changes from all commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
1409f42
Extending redefined-outer-name check to cover the case of loop variables
Lucas-C 11fa067
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 2f6aef8
Adding documentation
Lucas-C a10bad0
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] d75a981
Work in progress
Lucas-C 77a99e2
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 8a8d743
Better handling of nodes.Starred
Lucas-C 22c6679
Work in progress
Lucas-C 57e7953
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] b3e29b2
Handling tricky case
Lucas-C 0c71d06
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 3a0e0c5
Fixing edge case
Lucas-C 567a36c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 1a31d2a
Handling case of variable re-assigned AFTER the for loop
Lucas-C 41f6d6b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 7feb489
Final fixups (hopefully)
Lucas-C 6cfbb16
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] e83856a
Pleasing pre-commit / pylint
Lucas-C a839978
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] c3e5ade
Pleasing mypy
Lucas-C 30e74e2
Disabling redefined-outer-name for Pylint temporarily as required by …
Lucas-C a3092a1
Upgrade astroid to 3.0.0a3
Pierre-Sassoulas c831559
Ignore more stdlib classes with complex ancestry
jacobtylerwalls 11bb684
Add dummy args to empty Dict instantiation
jacobtylerwalls 75e7954
Merge branch 'upgrade-astroid' into extending-redefined-outer-name-to…
jacobtylerwalls ff5488f
Use bleeding-edge astroid
jacobtylerwalls cb3f914
[temporary] Skip home-assistant
jacobtylerwalls e733253
[primer] Don't cross-reference missing package
jacobtylerwalls 17aea9f
[temporary] Check out this branch in comment flow
jacobtylerwalls 3edb200
Revert "Use bleeding-edge astroid"
jacobtylerwalls 41c6d83
Merge branch 'main' into extending-redefined-outer-name-to-loop-varia…
jacobtylerwalls b2aaaa1
Merge branch 'main' into extending-redefined-outer-name-to-loop-varia…
Pierre-Sassoulas 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,6 +31,8 @@ jobs: | |
| steps: | ||
| - name: Check out code from GitHub | ||
| uses: actions/[email protected] | ||
| with: | ||
| ref: extending-redefined-outer-name-to-loop-variables | ||
| - name: Set up Python ${{ env.DEFAULT_PYTHON }} | ||
| id: python | ||
| uses: actions/[email protected] | ||
|
|
||
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,4 @@ | ||
| The rule ``redefined-outer-name`` now checks for loop variables | ||
| shadowing variables defined before in the local scope. | ||
|
|
||
| Closes #8646 |
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
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,81 @@ | ||
| "redefined-outer-name with loop variables - cf. issue #8646 for context" | ||
| # pylint: disable=global-statement,invalid-name,missing-function-docstring | ||
| from collections import defaultdict | ||
| errors = [ | ||
| Exception("E101", "Boom!"), | ||
| Exception("E101", "Bam!"), | ||
| Exception("E102", "Shazam!"), | ||
| ] | ||
| errors_per_arg0 = defaultdict(list) | ||
| for error in errors: | ||
| errors_per_arg0[error.args[0]].append(error) | ||
| for arg0, errors in errors_per_arg0.items(): # [redefined-outer-name] | ||
| print(arg0, "count:", len(errors)) | ||
|
|
||
| pair = 2 | ||
| for pair in errors_per_arg0.items(): # [redefined-outer-name] | ||
| print(pair) | ||
|
|
||
| words = ( | ||
| ("A", ("a", "abandon", "ability", ...)), | ||
| ("B", ("ball", "banana", ...)), | ||
| ) | ||
| for letter, *words in words: # [redefined-outer-name] | ||
| print(letter, words) | ||
|
|
||
| def func0(*args, **kwargs): | ||
| print(args, kwargs) | ||
| for args in range(10): # [redefined-outer-name] | ||
| pass | ||
| for _, kwargs in {}.items(): # [redefined-outer-name] | ||
| pass | ||
|
|
||
|
|
||
| #--- Notable cases where redefined-outer-name should NOT be raised: | ||
|
|
||
| # When a dummy variable is used: | ||
| _ = 42 | ||
| for _ in range(10): | ||
| pass | ||
|
|
||
| # When loop variables are re-assigned: | ||
| for i, err in enumerate(errors): | ||
| i += 1 | ||
| err = None | ||
|
|
||
| # When the same loop variables are used consecutively: | ||
| for k, v in errors_per_arg0.items(): | ||
| print(k, v) | ||
| for k, v in errors_per_arg0.items(): | ||
| print(k, v) | ||
|
|
||
| # When a similarly named loop variable is assigned in another loop: | ||
| for n in range(10): | ||
| n += 1 | ||
| for n in range(10): | ||
| n += 1 | ||
|
|
||
| # When the loop variable is re-assigned AFTER the for loop: | ||
| def func1(): | ||
| for value in range(10): | ||
| print(value) | ||
| if 1 + 1: | ||
| value = 42 | ||
|
|
||
| # When the variable is global or nonlocal: | ||
| glob = 42 | ||
| def func2(): | ||
| global glob | ||
| for glob in range(10): | ||
| pass | ||
|
|
||
| # When the variable was just type-declared before the loop: | ||
| var: int | ||
| for var in (1, 2): | ||
| print(var) | ||
|
|
||
| # Function argument override, already covered by redefined-argument-from-local: | ||
| def func3(arg): | ||
| print(arg) | ||
| for arg in range(10): # [redefined-argument-from-local] | ||
| 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,6 @@ | ||
| redefined-outer-name:12:0:13:38::Redefining name 'errors' from outer scope (line 4):HIGH | ||
| redefined-outer-name:16:0:17:15::Redefining name 'pair' from outer scope (line 15):HIGH | ||
| redefined-outer-name:23:0:24:24::Redefining name 'words' from outer scope (line 19):HIGH | ||
| redefined-outer-name:28:4:29:12:func0:Redefining name 'args' from outer scope (line None):HIGH | ||
| redefined-outer-name:30:4:31:12:func0:Redefining name 'kwargs' from outer scope (line None):HIGH | ||
| redefined-argument-from-local:80:8:80:11:func3:Redefining argument with the local name 'arg':UNDEFINED |
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
namemight not exist. See test case:Gives:
You could use
repr_name()(new in astroid 3.0 alpha, so you'll want to rebase on pylint-dev@upgrade-astroid) EDIT: my pushes did that for you! (but you can remove the bleeding-edge commit)