-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Allow subclassing NamedTuple as a nested class #4419
Closed
Closed
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a295319
tests for local NamedTuple subclass (#4349)
elliott-beach dc180e7
Allow subclassing NamedTuple in function (#4349)
elliott-beach ea573e2
direct test for inner namedtuple
elliott-beach 6eae524
add test for incremental namedtuple
elliott-beach abf5a0a
handle caching namedtuple
elliott-beach b398542
fix lint errors
elliott-beach 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 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 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 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 |
---|---|---|
|
@@ -132,6 +132,48 @@ MypyFile:1( | |
__main__.N@2) | ||
PassStmt:2())) | ||
|
||
[case testNamedTupleDirectSubclass] | ||
from typing import NamedTuple | ||
class A(NamedTuple): | ||
x: int | ||
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. Style nit: indent should be 4 spaces. |
||
|
||
[out] | ||
MypyFile:1( | ||
ImportFrom:1(typing, [NamedTuple]) | ||
ClassDef:2( | ||
A | ||
TupleType( | ||
Tuple[builtins.int]) | ||
BaseType( | ||
builtins.tuple[builtins.int]) | ||
AssignmentStmt:3( | ||
NameExpr(x [m]) | ||
TempNode:-1( | ||
Any) | ||
builtins.int))) | ||
|
||
[case testLocalNamedTupleDirectSubclass] | ||
from typing import NamedTuple | ||
def foo(): | ||
class A(NamedTuple): | ||
x: int | ||
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. Style nit: indent should be 4 spaces. |
||
[out] | ||
MypyFile:1( | ||
ImportFrom:1(typing, [NamedTuple]) | ||
FuncDef:2( | ||
foo | ||
Block:2( | ||
ClassDef:3( | ||
A | ||
TupleType( | ||
Tuple[builtins.int]) | ||
BaseType( | ||
builtins.tuple[builtins.int]) | ||
AssignmentStmt:4( | ||
NameExpr(x [m]) | ||
TempNode:-1( | ||
Any) | ||
builtins.int))))) | ||
-- Errors | ||
|
||
[case testNamedTupleWithTooFewArguments] | ||
|
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.
While this works, I'm not sure if this is the best change that could be made. As we can see in
analyze_namedtuple_classdef
:(referencing this TODO),
the root of the issue is that the analysis and processing of namedtuples is unconditionally global. If I understand things correctly, your change adds the node early to the symbol table so it is coerced into being a local definition by
setup_class_def_analysis
. That works, but is rather brittle.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.
I agree that this is incomplete because we need to handle local definitions of NamedTuple in a few other places, but I would still argue that we should call
setup_class_def_analysis
for NamedTuples in this way. Local classes are added to the symbol table insetup_class_def_analysis
, and there is no reason not to add local NamedTuples at that point as well.This doesn't seem early to me because semantic analysis appears to me to proceed in two steps of (1) adding nodes with basic info to the symbol table and then (2) adding more complex type info. I think here is the right place to do step (1) for NamedTuples.
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 TODO comments by @JukkaL appear to be referring to assigning
node.kind
asGDEF
orLDEF
, which I think would be simple to fix.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.
Okay I think this is acceptable.
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.
Great, Let me fix the todos.
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.
Actually, my understanding is LDEF and GDEF for NamedTuples, TypedDicts, and Enums is a longstanding issue that is not the cause of the specific problem this PR is solving, and I am not too confident on the solution. So I won't address those.