Skip to content

Synthesize the __slots__ attribute on namedtuple classes #2490

@AlexWaygood

Description

@AlexWaygood

NamedTuple classes (of all kinds) have a __slots__ attribute generated for them at runtime (it's always an empty tuple):

>>> import collections, typing
>>> A = collections.namedtuple("A", ())
>>> B = typing.NamedTuple("B", ())
>>> class C(typing.NamedTuple): ...
... 
>>> A.__slots__
()
>>> B.__slots__
()
>>> C.__slots__
()

We currently emit a false-positive on the C.__slots__ attribute access above, and after astral-sh/ruff#22327 we will also start emitting false-positive errors on the A.__slots__ and B.__slots__ attribute accesses too. We should fix this by synthesizing the generated __slots__ attribute for these classes.

Metadata

Metadata

Assignees

Labels

runtime semanticsAccurate modeling of how Python's semantics work at runtime

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions