Skip to content

Support nested namedtuple classes. #15064

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
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions mypy/semanal_namedtuple.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@ def check_namedtuple_classdef(
# And docstrings.
if isinstance(stmt, ExpressionStmt) and isinstance(stmt.expr, StrExpr):
continue
# Allow nested classes.
if isinstance(stmt, ClassDef):
self.api.analyze_class(stmt)
Copy link
Contributor Author

@hmc-cs-mdrissi hmc-cs-mdrissi Apr 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on exploring debugger I feel like this line is wrong. Main issue is for this case,

class A(NamedTuple):
  class B:
    ...

  x: B

B must be analyzed before field x's type is examined. If I drop this line then reveal_type(A.B) gives desired result and class gets analyzed I think here, but it introduces an error of Name "Bar" is not defined [name-defined] from this line. I could maybe add scope here, but sorta feels right path is to analyze nested class defs before processing fields. Seems like I should defer for this and unsure on right way to do it.

edit: The main difference with nested non-namedtuple class is we analyze x's type during analyze_class_body_common after B's class def has been analyzed. For namedtuple case analyze_class_body_common happens but after all attributes have been analyzed first.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what's the issue here, but you may need to set up a bunch of state/context in the semantic analyzer to allow nested classes to be processed correctly. For named tuples we probably don't set up all the context right now, since it's not needed to process attributes.

One idea would be to process named tuple class bodies normally (similar to regular classes) in the semantic analyzer even for named tuples, but we'd skip attribute definitions during this pass. The current namedtuple pass would then ignore everything else except attribute definitions. This would be a more complex change, however.

continue
statements.pop()
defn.removed_statements.append(stmt)
self.fail(NAMEDTUP_CLASS_ERROR, stmt)
Expand Down
4 changes: 4 additions & 0 deletions mypy/semanal_shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,10 @@ def process_placeholder(
) -> None:
raise NotImplementedError

@abstractmethod
def analyze_class(self, defn: ClassDef) -> None:
raise NotImplementedError


def set_callable_name(sig: Type, fdef: FuncDef) -> ProperType:
sig = get_proper_type(sig)
Expand Down
20 changes: 20 additions & 0 deletions test-data/unit/check-class-namedtuple.test
Original file line number Diff line number Diff line change
Expand Up @@ -728,3 +728,23 @@ reveal_type(y) # N: Revealed type is "builtins.int"
point.y = 6 # E: Property "y" defined in "Point" is read-only

[builtins fixtures/tuple.pyi]

[case testNestedNamedTuple]
from enum import Enum
from typing import NamedTuple

class T(NamedTuple):
class State(Enum):
A = 1
state: State

class RaggedFeature(NamedTuple):
class RowSplits(NamedTuple):
x: int

splits: RowSplits

reveal_type(T.State.A) # N: Revealed type is "Literal[__main__.State.A]?"
reveal_type(RaggedFeature.RowSplits.x) # N: Revealed type is "builtins.int"

[builtins fixtures/tuple.pyi]