Repository navigation
Drop inherited dataclass default when an override assigns a field specifier - #11664
Nilesh Patil (nileshpatil6) wants to merge 2 commits into
Conversation
…cifier
When a dataclass field overrides an inherited field that has a default,
pyright always re-inherits that default. That matches the runtime only
for a bare annotation. If the override assigns a field specifier that
supplies no default, the runtime replaces the entry outright and the
parameter becomes required:
@DataClass
class Base:
x: int = 1
@DataClass
class Foo(Base):
x: int = field()
inspect.signature(Foo.__init__) # (self, x: int) -> None
Foo() # TypeError: missing argument 'x'
pyright synthesized "(self: Foo, a: int = 0) -> None" instead, so the
call above was not reported, and the override itself was flagged with
"overrides a field of the same name but is missing a default value"
even though nothing is inherited in that case.
Track whether the entry had an assigned value and only inherit the base
default for bare annotations. The bare form is unchanged: it still
inherits and still reports the diagnostic. Verified against CPython for
all three forms, including that the default is dropped for
"x: int = field()", kept for "x: int", and that the parameter is removed
for "x: int = field(init=False)".
Fixes microsoft#11660
|
🔒 Automated review in progress — Rich Chiodo (@rchiodo) is auto-reviewing this PR. |
|
The sample disables Verification: The relevant tests could not be fully run in the isolated environment; this review is not fully verified. |
Rich Chiodo (rchiodo)
left a comment
There was a problem hiding this comment.
Approved via Review Center.
|
|
||
| @dataclass | ||
| class DC15(DC14): | ||
| # This should generate an error because "b" drops its inherited default |
There was a problem hiding this comment.
Info · Optional note
📍 packages/pyright-internal/src/tests/samples/dataclass4.py:141
[unverified] Add the documented field(init=False) override case and pin its no-parameter constructor signature and inherited-member behavior. The current tests cover only field() with init=True, leaving this distinct branch unprotected.
[verified]
|
Verification: The relevant tests could not be fully run in the isolated environment; this review is not fully verified. |
An assigned field specifier with init=False removes the parameter from __init__ rather than dropping the inherited default, so the attribute keeps the base class value. Pin both the constructor signature and the attribute type.
|
Thanks for the review. Added the @dataclass
class DC16(DC12):
a: int = field(init=False)
reveal_type(DC16.__init__, expected_text="(self: DC16) -> None")
reveal_type(DC16().a, expected_text="int")That matches CPython, where On the other point, I think the rule named there is not the one this PR affects. The erroneous diagnostic being removed is evaluator.addDiagnostic(
DiagnosticRule.reportGeneralTypeIssues,
LocMessage.dataClassFieldInheritedDefault().format({ fieldName: variableName }),
variableNameNode
);
Happy to add a separate sample with |
|
Closing this in favour of #11665, which landed the same fix while this was in review. For the record the two changes are equivalent: #11665 tracks Two cases from this PR's tests are not covered by the merged sample, including the |
Fixes #11660.
The bug
When a dataclass field overrides an inherited field that has a default,
dataClasses.tsunconditionally re-inherits that default:That is correct for a bare annotation, but not when the override assigns a field specifier that supplies no default. I checked the three forms against CPython 3.11 rather than relying on the issue report:
So
x: int = field()replaces the inherited entry and the parameter becomes required. pyright instead synthesized(self: Foo, a: int = 0) -> None, which produced two wrong results at once:Foo()was accepted, though it raisesTypeErrorat runtime."x" overrides a field of the same name but is missing a default value, even though in this form nothing is inherited.There is a knock-on ordering case too. When a defaulted field precedes the override, dropping the default makes a non-default parameter follow a default one, which CPython rejects at class creation:
pyright did not report that either. With this change the existing "Fields without default values cannot appear after fields with default values" check fires on that line.
The change
Track whether the entry had an assigned value, and only inherit the base default for bare annotations. Behavior for
x: intis unchanged: it still inherits the default and still reports the existing diagnostic.Tests
Added to
dataclass4.py, the sample for inherited dataclasses. The new cases pin the synthesized signature so they actually discriminate:Without the fix that reports
(self: DC13, a: int = 0) -> Noneand the suite fails; with it, it passes. The expected error count forDataClass4goes from 6 to 8, covering the now-reportedDC13()call and the ordering case.npx jest typeEvaluatorpasses 1212/1212 across all 8 suites, and Prettier reports the changed files clean.