| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
…ython#21583) When a dataclass field's default value is a `namedtuple('Name', ...)` call, the semantic analyzer treats the assignment as a NamedTuple class definition and binds the field's name to a TypeInfo instead of a Var, regardless of any type annotation on the left-hand side. The dataclass plugin's collect_attributes() assumed every non-alias, non-decorator symbol table node was a Var and asserted so, crashing when it encountered this TypeInfo. Skip such fields the same way TypeAlias/Decorator nodes are already skipped, since they aren't valid dataclass fields. Fixes python#21583
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
Sorry, something went wrong.
| Back | FazBrowse Home | New Git URL |
Fixes #21583
Root cause
crashes mypy with:
When the right-hand side of a class-body assignment is a namedtuple(...)
call, mypy's semantic analyzer (analyze_namedtuple_assign in
mypy/semanal.py) always treats the statement as a functional NamedTuple
class definition and binds the assigned name in the class's symbol table to
a TypeInfo, regardless of whether the left-hand side also carries a type
annotation. So p's symbol table entry ends up being a TypeInfo for a
synthesized Point class, not a Var, even though it looks like a normal
annotated dataclass field.
DataclassTransformer.collect_attributes() in mypy/plugins/dataclasses.py
did not anticipate this and unconditionally asserted
isinstance(node, Var) for any class-body annotated assignment that wasn't
already special-cased (TypeAlias, Decorator), causing the crash.
Fix
Skip such fields the same way TypeAlias and Decorator nodes are already
skipped in collect_attributes(), since a name bound to a TypeInfo this
way is not a valid dataclass field. This mirrors the existing handling
immediately above it in the same function and does not touch the
Var-based path used by ordinary dataclass/NamedTuple fields.
Testing
test-data/unit/check-dataclasses.test, covering both the
name-mismatched case from the issue and a name-matched variant.
that normal dataclass fields with forward-referenced NamedTuple
class types still type-check correctly.