Skip to content

fix(dataclasses): skip non-Var ClassVars - #21916

Open
daleselaji-dev wants to merge 1 commit into
python:masterfrom
daleselaji-dev:codex/fix-dataclass-typeddict-classvar-21915
Open

fix(dataclasses): skip non-Var ClassVars#21916
daleselaji-dev wants to merge 1 commit into
python:masterfrom
daleselaji-dev:codex/fix-dataclass-typeddict-classvar-21915

Conversation

@daleselaji-dev

Copy link
Copy Markdown

Problem

Mypy reports an INTERNAL ERROR when a dataclass contains a ClassVar assigned to a functional TypedDict definition. The combination of dataclass, ClassVar, and TypedDict should be valid, but the dataclass plugin crashes during semantic analysis.

Root Cause

The dataclass plugin assumes every annotated assignment symbol is a Var. A ClassVar assigned a class definition can instead be represented as a TypeInfo, causing collect_attributes() to hit an assertion before it can apply the normal ClassVar exclusion.

Solution

Skip non-Var symbols during dataclass attribute collection. Dataclasses ignore ClassVars, so a class-definition symbol has no instance attribute to collect.

Changes

  • Replace the unconditional Var assertion with a narrow non-Var guard.
  • Add a check-dataclasses.test regression covering ClassVar = TypedDict(...).

Testing

  • Baseline on current master deda499: the supplied reproducer failed with INTERNAL ERROR.
  • After the change, the reproducer completed with Success: no issues found in 1 source file.
  • New regression: testClassVarTypedDict passed.
  • Full check-dataclasses.test: 151 passed.
  • Black check: passed.
  • Ruff check on the changed source: passed.
  • Compileall: passed.
  • Mypy self-check: 196 source files passed.
  • git diff --check: passed.

Compatibility/Risk

This preserves existing handling for Var, TypeAlias, and Decorator symbols. It only avoids collecting a non-Var symbol that dataclasses would ignore, with no public API or runtime behavior change.

Notes for Reviewer

The regression uses the same minimal shape reported in #21915 and exercises the plugin through mypy's standard semantic-analysis test harness. The guard is intentionally limited to the attribute-collection boundary.

Linked Issue

Fixes #21915

@daleselaji-dev
daleselaji-dev marked this pull request as ready for review September 1, 2026 02:55
@github-actions

github-actions Bot commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Internal error on mypy of dataclass with TypedDict as ClassVar

1 participant