From d663b2d26bb21db0bdb19f13bc5b2c9b644f3e98 Mon Sep 17 00:00:00 2001 From: reginaldalfret Date: Fri, 9 Oct 2026 15:41:10 +0530 Subject: [PATCH 1/2] fix: require source key to be required when target TypedDict key is required 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 #11851. Signed-off-by: reginaldalfret --- .../pyright-internal/src/analyzer/typedDicts.ts | 6 +++++- .../src/tests/samples/typedDictReadOnly2.py | 14 ++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/packages/pyright-internal/src/analyzer/typedDicts.ts b/packages/pyright-internal/src/analyzer/typedDicts.ts index 5c6108c93086..81c6eb66c9c4 100644 --- a/packages/pyright-internal/src/analyzer/typedDicts.ts +++ b/packages/pyright-internal/src/analyzer/typedDicts.ts @@ -1212,7 +1212,11 @@ export function assignTypedDictToTypedDict( } } } else { - if (destEntry.isRequired !== srcEntry.isRequired && !destEntry.isReadOnly) { + const isRequiredMismatch = destEntry.isRequired + ? !srcEntry.isRequired + : srcEntry.isRequired && !destEntry.isReadOnly; + + if (isRequiredMismatch) { const message = destEntry.isRequired ? LocAddendum.typedDictFieldRequired() : LocAddendum.typedDictFieldNotRequired(); diff --git a/packages/pyright-internal/src/tests/samples/typedDictReadOnly2.py b/packages/pyright-internal/src/tests/samples/typedDictReadOnly2.py index 12e446b96cbe..01b7905e4058 100644 --- a/packages/pyright-internal/src/tests/samples/typedDictReadOnly2.py +++ b/packages/pyright-internal/src/tests/samples/typedDictReadOnly2.py @@ -246,3 +246,17 @@ class TD_B2(TypedDict): # This should generate an error for x but not y. class TD_B(TD_B1, TD_B2): ... + + +class TD_SrcNotReq(TypedDict): + tag: NotRequired[str] + + +class TD_DstReqReadOnly(TypedDict): + tag: ReadOnly[str] + + +def test_not_required_to_readonly_required(s: TD_SrcNotReq) -> None: + # This should generate an error because tag is required in TD_DstReqReadOnly. + d: TD_DstReqReadOnly = s + From f14d349f0d425f9ca53f4a9451b55bf8208c8336 Mon Sep 17 00:00:00 2001 From: Bill Schnurr Date: Fri, 20 Nov 2026 11:55:30 -0700 Subject: [PATCH 2/2] Assert required read-only key diagnostic 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> --- packages/pyright-internal/src/tests/typeEvaluator5.test.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/pyright-internal/src/tests/typeEvaluator5.test.ts b/packages/pyright-internal/src/tests/typeEvaluator5.test.ts index dba285a01535..66c0eb83b8f6 100644 --- a/packages/pyright-internal/src/tests/typeEvaluator5.test.ts +++ b/packages/pyright-internal/src/tests/typeEvaluator5.test.ts @@ -366,7 +366,12 @@ test('TypedDictReadOnly1', () => { test('TypedDictReadOnly2', () => { const analysisResults = TestUtils.typeAnalyzeSampleFiles(['typedDictReadOnly2.py']); - TestUtils.validateResults(analysisResults, 17); + TestUtils.validateResults(analysisResults, 18); + const requiredKeyError = analysisResults[0].errors.find((diagnostic) => diagnostic.range.start.line === 260); + assert.ok(requiredKeyError); + assert.strictEqual(requiredKeyError.range.start.character, 27); + assert.strictEqual(requiredKeyError.getRule(), DiagnosticRule.reportAssignmentType); + assert.match(requiredKeyError.message, /"tag" is required in "TD_DstReqReadOnly"/); }); test('TypedDictReadOnly3', () => {