-
-
Notifications
You must be signed in to change notification settings - Fork 3k
New semantic analyzer: fix issues with 'X = X' assignments #7144
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2886,3 +2886,85 @@ def f() -> None: | |
x, (y, *z) = t | ||
reveal_type(z) # N: Revealed type is 'builtins.list[builtins.str*]' | ||
[builtins fixtures/list.pyi] | ||
|
||
[case testNewAnalyzerIdentityAssignment1] | ||
from foo import * | ||
|
||
try: | ||
X = X | ||
except: | ||
class X: # E: Name 'X' already defined (possibly by an import) | ||
pass | ||
|
||
reveal_type(X()) # N: Revealed type is 'foo.X' | ||
|
||
[file foo.py] | ||
class X: pass | ||
|
||
[case testNewAnalyzerIdentityAssignment2] | ||
try: | ||
int = int | ||
reveal_type(int()) # N: Revealed type is 'builtins.int' | ||
except: | ||
class int: # E: Name 'int' already defined (possibly by an import) | ||
pass | ||
|
||
reveal_type(int()) # N: Revealed type is 'builtins.int' | ||
|
||
[case testNewAnalyzerIdentityAssignment3] | ||
forwardref: C | ||
|
||
try: | ||
int = int | ||
reveal_type(int()) # N: Revealed type is 'builtins.int' | ||
except: | ||
class int: # E: Name 'int' already defined (possibly by an import) | ||
pass | ||
|
||
reveal_type(int()) # N: Revealed type is 'builtins.int' | ||
|
||
class C: pass | ||
|
||
[case testNewAnalyzerIdentityAssignment4] | ||
try: | ||
C = C | ||
C | ||
except: | ||
class C: | ||
pass | ||
|
||
reveal_type(C()) # N: Revealed type is '__main__.C' | ||
|
||
[case testNewAnalyzerIdentityAssignment5] | ||
forwardref: D | ||
|
||
try: | ||
C = C | ||
C | ||
except: | ||
class C: | ||
pass | ||
|
||
class D: pass | ||
|
||
reveal_type(C()) # N: Revealed type is '__main__.C' | ||
|
||
[case testNewAnalyzerIdentityAssignment6] | ||
x: C | ||
class C: | ||
pass | ||
C = C | ||
|
||
reveal_type(C()) # N: Revealed type is '__main__.C' | ||
reveal_type(x) # N: Revealed type is '__main__.C' | ||
|
||
[case testNewAnalyzerIdentityAssignment7] | ||
C = C # E: Name 'C' is not defined | ||
|
||
reveal_type(C) # N: Revealed type is 'Any' \ | ||
# E: Name 'C' is not defined | ||
|
||
[case testNewAnalyzerIdentityAssignment8] | ||
from typing import Final | ||
x: Final = 0 | ||
x = x # E: Cannot assign to final name "x" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great to see so detailed test cases! |
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.
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.
name_not_defined()
currently already callsdefer()
, is this additional defer actually needed?Also this returns
True
, I would make it more clear by putting the return directly here.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.
The fact that
name_not_defined
calls defer requires some pretty subtle reasoning so I'd rather keep make it explicit here.