Skip to content

fix: require source key to be required when target TypedDict key is required and ReadOnly - #11852

Open
Reginald Alfret V (reginaldalfret) wants to merge 3 commits into
microsoft:mainfrom
reginaldalfret:fix/issue-11851-typeddict-readonly-required
Open

Reginald Alfret V (reginaldalfret) wants to merge 3 commits into
microsoft:mainfrom
reginaldalfret:fix/issue-11851-typeddict-readonly-required

Conversation

@reginaldalfret

Copy link
Copy Markdown

Per PEP 705 and structural subtyping rules for TypedDict, 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.

Prior to this fix, assignTypedDictToTypedDict bypassed the requiredness check entirely when destEntry.isReadOnly was true (destEntry.isRequired !== srcEntry.isRequired && !destEntry.isReadOnly). This erroneously allowed a source TypedDict with a NotRequired key to be assigned to a destination TypedDict requiring that key as ReadOnly, leading to runtime KeyErrors when accessing the required field.

This change corrects the check so that:

  1. When destEntry.isRequired is true, srcEntry.isRequired must also be true.
  2. When destEntry.isRequired is false, srcEntry.isRequired being true is permitted only if destEntry.isReadOnly is true (as per PEP 705 covariance).

A test case covering this scenario has been added to typedDictReadOnly2.py.

Closes #11851.

…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>
@Pika-pikachu30

chill🙃 (Pika-pikachu30) commented Oct 27, 2026 •

Copy link
Copy Markdown

Bill Schnurr (@bschnurr) Rich Chiodo (@rchiodo) Please approve the automatic workflow test.

@heejaechang

Copy link
Copy Markdown
Collaborator

🟢 Pyright CLI QA — d663b2d2 — verdict: green

Automated CLI checks compared this change with its base revision.
8 focused check(s) completed.

No new behavior difference was detected.

@rchiodo Rich Chiodo (rchiodo) left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@bschnurr

Copy link
Copy Markdown
Member

Please update the TypedDictReadOnly2 test assertion. I integrated d663b2d2 into main (d95fe63): the focused test fails with Expected 17 errors, got 18; the three neighboring TypedDictReadOnly tests pass. The original fixture passes on main. Replaying the extended sample on main produces no diagnostic on the new assignment; the PR correctly adds "tag" is required in "TD_DstReqReadOnly" there. Please account for that new expected error and, if possible, assert its location so an unrelated diagnostic can't satisfy the test.

@bschnurr

Copy link
Copy Markdown
Member

Rechecked 254fb3bb against main (d95fe63b). The requiredness fix also works in overload resolution: a source with tag: NotRequired[str] now selects the object -> str fallback instead of the RequiredReadOnly -> Literal["required"] overload. A source with required tag still selects the specific overload. An optional ReadOnly target still accepts both sources, and the optional writable target still rejects the required source.

The earlier suite blocker is still present on this head: TypedDictReadOnly2 expects 17 errors and reports 18, while all three neighboring tests pass. The 18th diagnostic is the intended rejection on the new tag assignment. Please update the expectation and pin the new diagnostic to that assignment.

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>
@bschnurr

Copy link
Copy Markdown
Member

Pushed f14d349 to update TypedDictReadOnly2 from 17 to 18 expected errors and assert the new required-key diagnostic's location, rule, and reason. No existing diagnostic or type assertion was removed.

The previous head reproduced the failing count; after the correction, pnpm exec jest typeEvaluator5.test.ts --runInBand --forceExit passed all 72 tests. The core build, ESLint, and Prettier also passed. Current-main integration was checked for conflicts but not built locally.

The previously noted nested-overload comparison cost has not been timed on a representative corpus; the hosted benchmark and review are still needed.

@github-actions

Copy link
Copy Markdown
Contributor

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

@github-actions

Copy link
Copy Markdown
Contributor

Compared candidate 0bc636b52339bf2283bc2edebfe971c4af9b1b14 against its first parent 7e29199f7b3c5bddeda6956588a15552398a66ad.

Release history charts were not rendered for this run. See the workflow run for details.

Type checker benchmark

🟢 No performance regressions detected.

Regression threshold: 20.0%
Statistic: median
Variance guard: >1.0s time and >100.0 MB memory

Package Checker Files checked Time Time delta Peak memory Memory delta Status
ansible pyright 583 16.521s +0.0% 1081.1 MB -0.2% 🟢 Pass
click pyright 17 3.104s -1.0% 358.6 MB +0.3% 🟢 Pass
homeassistant pyright 9850 128.209s +3.1% 5988.7 MB +0.1% 🟢 Pass
numpy pyright 356 28.377s -1.2% 1351.6 MB +0.6% 🟢 Pass
pandas pyright 1459 143.841s +0.8% 3702.9 MB -5.3% 🟢 Pass
pytest pyright 243 12.807s -1.1% 863.9 MB -0.1% 🟢 Pass
requests pyright 19 2.468s +3.0% 346.7 MB -2.0% 🟢 Pass
torch pyright 2400 115.573s -7.8% 4637.0 MB -4.2% 🟢 Pass
transformers pyright 2901 126.984s -4.1% 4849.0 MB +0.2% 🟢 Pass

Pyright stats

Package Parsed/bound Checked Find Read Tokenize Parse Imports Bind Check Cycles
ansible 988 583 0.037s 0.067s 0.313s 0.567s 0.187s 0.847s 13.757s 0.000s
click 111 17 0.000s 0.017s 0.113s 0.200s 0.037s 0.263s 2.150s 0.000s
homeassistant 11154 9850 0.550s 0.817s 2.550s 4.810s 1.940s 6.203s 108.817s 0.000s
numpy 606 356 0.010s 0.050s 0.257s 0.513s 0.190s 0.670s 26.310s 0.000s
pandas 1899 1459 0.060s 0.243s 1.227s 2.227s 0.307s 2.790s 137.430s 0.000s
pytest 545 243 0.020s 0.060s 0.363s 0.507s 0.110s 0.730s 10.540s 0.000s
requests 167 19 0.000s 0.017s 0.120s 0.217s 0.050s 0.273s 1.433s 0.000s
torch 3078 2400 0.130s 0.293s 1.580s 2.930s 0.510s 3.880s 104.563s 0.000s
transformers 3536 2901 0.167s 0.410s 2.377s 3.583s 0.880s 9.080s 113.490s 0.000s

This branch has not been deployed

No deployments
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.

TypedDict subtyping : ReadOnly required incorrectly accepts NotRequired source key

5 participants