Repository navigation
Fix dataclass_transform base frozen inheritance - #11993
自分 (2Zest) wants to merge 4 commits into
Conversation
|
自分 (@2Zest) please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.
Contributor License AgreementContribution License AgreementThis Contribution License Agreement (“Agreement”) is agreed to by the party signing below (“You”),
|
🟢 Pyright CLI QA —
|
…rm5 sample Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
|
I pushed d9c672c to this branch. It adds 🤖 AI-assisted review posted on behalf of Bill Schnurr (@bschnurr). |
Rich Chiodo (rchiodo)
left a comment
There was a problem hiding this comment.
Skipping the direct neutral provider seems to also skip a concrete frozen ancestor:
from dataclasses import dataclass
from typing import cast, dataclass_transform
@dataclass_transform(frozen_default=True)
class Meta(type):
def __new__(cls, name, bases, namespace, *, frozen=True, provider=False):
result = super().__new__(cls, name, bases, namespace)
if not provider:
dataclass(cast(type, result), frozen=frozen)
return result
@dataclass(frozen=True)
class Frozen:
value: int = 0
class Neutral(Frozen, metaclass=Meta, provider=True): pass
class Mutable(Neutral, frozen=False): passOn base, the error at Mutable comes from treating Neutral as frozen, not from walking through it to Frozen. Head (d9c672c) reports no error, while CPython 3.14.3 raises TypeError: cannot inherit non-frozen dataclass from a frozen one. Neutral should be neutral, but Frozen still matters.
The no-concrete-ancestor control is rejected by base and accepted by head (the intended benefit), and a frozen descendant of a frozen ancestor passes on both. Could we keep those behaviors while checking concrete ancestors through the neutral class? I don't have a tested fix.
The reverse direction is an existing gap, not a new regression:
from dataclasses import dataclass
from typing import dataclass_transform
@dataclass_transform()
class Meta(type): pass
@dataclass
class Mutable:
value: int
class Neutral(Mutable, metaclass=Meta):
def __init_subclass__(cls, *, frozen: bool = False) -> None:
super().__init_subclass__()
dataclass(cls, frozen=frozen)
class Bad(Neutral, frozen=True): passBoth revisions accept this, but CPython raises TypeError: cannot inherit frozen dataclass from a non-frozen one. Could we cover both ancestry directions while making this change?
The provider class itself is another existing case:
from dataclasses import dataclass
from typing import dataclass_transform
@dataclass_transform()
class Meta(type): pass
@dataclass(frozen=True)
class Frozen:
value: int
class Neutral(Frozen, metaclass=Meta): pass
print(Neutral(1).value)Both revisions reject this, but CPython constructs it and prints 1. Per the typing spec, a class directly specifying a transform-decorated metaclass is neither frozen nor non-frozen. Could we consider respecting that neutrality for the class itself, not just descendants?
|
I independently replayed the frozen-ancestor finding on |
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
|
Compared candidate Release history charts were not rendered for this run. See the workflow run for details. Type checker benchmark🟢 No performance regressions detected. Regression threshold:
Pyright stats
|
Fixes #11988
Summary
Classes decorated with
dataclass_transformare neither frozen nor non-frozen according to the typing specification. When such a class was used as a base, Pyright still treated its effectivefrozen_defaultas an actual frozen dataclass and rejected a non-frozen subclass.This change excludes dataclass-transform classes (including classes whose metaclass carries the transform) from frozen-base compatibility checks while preserving the existing checks for concrete generated dataclasses.
Tests
dataclassTransform5.pycovering both non-frozen and frozen subclasses of nesteddataclass_transformclasses.DataclassTransform5type-evaluator test.Local static validation:
git diff --checkpasses. The repository's Node dependencies were not installed in this environment, so the TypeScript test and lint commands could not be run locally.AI assistance: OpenAI Codex assisted with investigation, implementation, and test authoring.