Repository navigation
fix: require source key to be required when target TypedDict key is required and ReadOnly - #11852
Conversation
…equired and ReadOnly Per PEP 705, when a key is required in the destination TypedDict, it must also be required in the source TypedDict regardless of whether the destination key is marked ReadOnly. The previous check bypassed the requiredness check entirely whenever destEntry.isReadOnly was true, incorrectly allowing NotRequired source keys to be assigned to required ReadOnly destination keys. Closes microsoft#11851. Signed-off-by: reginaldalfret <reginaldalfret@gmail.com>
|
Bill Schnurr (@bschnurr) Rich Chiodo (@rchiodo) Please approve the automatic workflow test. |
🟢 Pyright CLI QA —
|
Rich Chiodo (rchiodo)
left a comment
There was a problem hiding this comment.
The focused Jest test fails with Expected 17 errors, got 18. The new tag case in typedDictReadOnly2.py is correct: an optional source key can’t satisfy a required ReadOnly key. The same sample reports 17 errors on the baseline and 18 on this head. Can we update the expectation in typeEvaluator5.test.ts to 18 and keep the new diagnostic?
Separately, there’s a non-blocking performance question with main’s TypedDict assignment cache. This is a valid overload fallback with shared nested TypedDicts:
from typing import NotRequired, ReadOnly, TypedDict, overload
class A0(TypedDict): value: ReadOnly[int]
class B0(TypedDict): value: NotRequired[int]
class A1(TypedDict): a: ReadOnly[A0]; b: ReadOnly[A0]; c: ReadOnly[A0]
class B1(TypedDict): a: ReadOnly[B0]; b: ReadOnly[B0]; c: ReadOnly[B0]
class A2(TypedDict): a: ReadOnly[A1]; b: ReadOnly[A1]; c: ReadOnly[A1]
class B2(TypedDict): a: ReadOnly[B1]; b: ReadOnly[B1]; c: ReadOnly[B1]
class A3(TypedDict): a: ReadOnly[A2]; b: ReadOnly[A2]; c: ReadOnly[A2]
class B3(TypedDict): a: ReadOnly[B2]; b: ReadOnly[B2]; c: ReadOnly[B2]
@overload
def choose(value: A3) -> int: ...
@overload
def choose(value: object) -> int: ...
def choose(value: object) -> int: return 0
def use(value: B3) -> int: return choose(value)With the fix, the A3 overload is rejected because B0.value is optional where A0.value is required, then the object overload accepts the call. Neither revision reports errors. Instrumentation shows comparison-wrapper calls going from 40 to 80 for this case. A width-3/depth-4 variant goes from 52 to 242 calls and from 108 to 1,128 internal diagnostic addenda.
The comparison was main 317abe4 vs a synthetic merge with d663b2d. Current main ff00b031 differs from that baseline only in workflow and doc files, so the production code is unchanged. The failure-cache behavior predates this PR, but the new rejection sends this valid fallback through it.
These are counts, not timings, so I can’t tell whether there’s a real-world slowdown. Could we consider a bounded-work regression test or a follow-up to look at bounding repeated failure-diagnostic construction? I don’t think an optimization needs to block this PR.
|
Please update the |
|
Rechecked The earlier suite blocker is still present on this head: |
Class C: the sample expectation did not account for the intended new diagnostic. Preserve all prior diagnostics and assert the new assignment error by location, rule, and reason. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
|
Pushed f14d349 to update The previous head reproduced the failing count; after the correction, The previously noted nested-overload comparison cost has not been timed on a representative corpus; the hosted benchmark and review are still needed. |
|
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
|
Per PEP 705 and structural subtyping rules for
TypedDict, when a key is required in the destinationTypedDict, it must also be required in the sourceTypedDict, regardless of whether the destination key is markedReadOnly.Prior to this fix,
assignTypedDictToTypedDictbypassed the requiredness check entirely whendestEntry.isReadOnlywas true (destEntry.isRequired !== srcEntry.isRequired && !destEntry.isReadOnly). This erroneously allowed a sourceTypedDictwith aNotRequiredkey to be assigned to a destinationTypedDictrequiring that key asReadOnly, leading to runtimeKeyErrors when accessing the required field.This change corrects the check so that:
destEntry.isRequiredis true,srcEntry.isRequiredmust also be true.destEntry.isRequiredis false,srcEntry.isRequiredbeing true is permitted only ifdestEntry.isReadOnlyis true (as per PEP 705 covariance).A test case covering this scenario has been added to
typedDictReadOnly2.py.Closes #11851.