From b507514b3a421b0b86b8ff8cd958a5160bc0da1d Mon Sep 17 00:00:00 2001 From: Henry Su Date: Thu, 5 Nov 2026 20:47:16 -0600 Subject: [PATCH 1/3] Add test for LiteralString keys on TypedDicts --- .../src/tests/samples/typedDictClosed12.py | 41 +++++++++++++++++++ .../src/tests/typeEvaluator5.test.ts | 5 +++ 2 files changed, 46 insertions(+) create mode 100644 packages/pyright-internal/src/tests/samples/typedDictClosed12.py diff --git a/packages/pyright-internal/src/tests/samples/typedDictClosed12.py b/packages/pyright-internal/src/tests/samples/typedDictClosed12.py new file mode 100644 index 000000000000..81ab94b6db96 --- /dev/null +++ b/packages/pyright-internal/src/tests/samples/typedDictClosed12.py @@ -0,0 +1,41 @@ +# This sample tests that a LiteralString key on a TypedDict is treated +# like a str key whose value is not known statically. + +from typing import Literal, LiteralString, TypedDict, reveal_type + + +class TD1(TypedDict, extra_items=str): + known: int + + +class TD2(TypedDict): + known: int + + +class TD3(TypedDict, closed=True): + known: int + + +def func1( + td1: TD1, + td2: TD2, + td3: TD3, + k: LiteralString, + s: str, + lit: Literal["known"], +) -> None: + reveal_type(td1[k], expected_text="Unknown") + reveal_type(td1[s], expected_text="Unknown") + reveal_type(td2[k], expected_text="Unknown") + reveal_type(td3[k], expected_text="Unknown") + reveal_type(td1[lit], expected_text="int") + + td1[k] = "a" + td2[k] = 1 + td3[k] = 1 + + del td1[k] + del td2[k] + + # This should generate an error because "other" is not a defined key. + td3["other"] diff --git a/packages/pyright-internal/src/tests/typeEvaluator5.test.ts b/packages/pyright-internal/src/tests/typeEvaluator5.test.ts index e3c32819dbab..2a6af506eb78 100644 --- a/packages/pyright-internal/src/tests/typeEvaluator5.test.ts +++ b/packages/pyright-internal/src/tests/typeEvaluator5.test.ts @@ -444,6 +444,11 @@ test('TypedDictClosed11', () => { TestUtils.validateResults(analysisResults, 2); }); +test('TypedDictClosed12', () => { + const analysisResults = TestUtils.typeAnalyzeSampleFiles(['typedDictClosed12.py']); + TestUtils.validateResults(analysisResults, 1); +}); + test('DataclassTransform1', () => { const analysisResults = TestUtils.typeAnalyzeSampleFiles(['dataclassTransform1.py']); From a39d4391b4ca464c1cea7529a8a64f6f8e68b480 Mon Sep 17 00:00:00 2001 From: Henry Su Date: Thu, 5 Nov 2026 20:47:16 -0600 Subject: [PATCH 2/3] Treat LiteralString TypedDict keys like str keys --- packages/pyright-internal/src/analyzer/typedDicts.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/pyright-internal/src/analyzer/typedDicts.ts b/packages/pyright-internal/src/analyzer/typedDicts.ts index 23ddc89a8bd5..8d543ddb359f 100644 --- a/packages/pyright-internal/src/analyzer/typedDicts.ts +++ b/packages/pyright-internal/src/analyzer/typedDicts.ts @@ -1724,7 +1724,7 @@ export function getTypeOfIndexedTypedDict( return subtype; } - if (isClassInstance(subtype) && ClassType.isBuiltIn(subtype, 'str')) { + if (isClassInstance(subtype) && ClassType.isBuiltIn(subtype, ['str', 'LiteralString'])) { if (subtype.priv.literalValue === undefined) { // If it's a plain str with no literal value, we can't // make any determination about the resulting type. From dac47755a11234e30734534499d3440b269cd268 Mon Sep 17 00:00:00 2001 From: Bill Schnurr Date: Fri, 20 Nov 2026 10:46:34 -0700 Subject: [PATCH 3/3] Reject unsafe LiteralString TypedDict mutations Classification: B (Pyright limitation). Nonliteral keys can target required, read-only, or narrowed fields, so retain the existing write and delete diagnostics until those mutations can be checked soundly. LiteralString reads and known literal-key precision are unchanged. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../src/analyzer/typedDicts.ts | 10 +++- .../tests/samples/typedDictLiteralString1.py | 47 +++++++++++++++---- .../src/tests/typeEvaluator5.test.ts | 12 ++++- 3 files changed, 57 insertions(+), 12 deletions(-) diff --git a/packages/pyright-internal/src/analyzer/typedDicts.ts b/packages/pyright-internal/src/analyzer/typedDicts.ts index 9dfb074b9ebc..e09910b43f29 100644 --- a/packages/pyright-internal/src/analyzer/typedDicts.ts +++ b/packages/pyright-internal/src/analyzer/typedDicts.ts @@ -1739,9 +1739,15 @@ export function getTypeOfIndexedTypedDict( return subtype; } - if (isClassInstance(subtype) && ClassType.isBuiltIn(subtype, ['str', 'LiteralString'])) { + // The existing non-literal str mutation path does not validate possible + // keys, so do not extend it to LiteralString mutations. + if ( + isClassInstance(subtype) && + (ClassType.isBuiltIn(subtype, 'str') || + (usage.method === 'get' && ClassType.isBuiltIn(subtype, 'LiteralString'))) + ) { if (subtype.priv.literalValue === undefined) { - // If it's a plain str with no literal value, we can't + // If it's a string with no literal value, we can't // make any determination about the resulting type. return UnknownType.create(); } diff --git a/packages/pyright-internal/src/tests/samples/typedDictLiteralString1.py b/packages/pyright-internal/src/tests/samples/typedDictLiteralString1.py index 81ab94b6db96..bf9d08c28dbc 100644 --- a/packages/pyright-internal/src/tests/samples/typedDictLiteralString1.py +++ b/packages/pyright-internal/src/tests/samples/typedDictLiteralString1.py @@ -1,7 +1,8 @@ -# This sample tests that a LiteralString key on a TypedDict is treated -# like a str key whose value is not known statically. +# This sample tests reads and unsafe mutations using a LiteralString key +# whose value is not known statically. from typing import Literal, LiteralString, TypedDict, reveal_type +from typing_extensions import ReadOnly # pyright: ignore[reportMissingModuleSource] class TD1(TypedDict, extra_items=str): @@ -16,10 +17,21 @@ class TD3(TypedDict, closed=True): known: int +class TDReadOnly(TypedDict): + protected: ReadOnly[int] + + +class TDMixed(TypedDict, extra_items=int | str): + value: int | str + + def func1( td1: TD1, td2: TD2, td3: TD3, + readonly: TDReadOnly, + mixed: TDMixed, + plain_dict: dict[str, int], k: LiteralString, s: str, lit: Literal["known"], @@ -29,13 +41,30 @@ def func1( reveal_type(td2[k], expected_text="Unknown") reveal_type(td3[k], expected_text="Unknown") reveal_type(td1[lit], expected_text="int") + reveal_type(readonly["protected"], expected_text="int") + + td1[lit] = 1 + known: LiteralString = "known" + td1[known] = 1 + plain_dict[k] = 1 + del plain_dict[k] + + td1[k] = "a" # This should generate an error. + td2[k] = 1 # This should generate an error. + td3[k] = 1 # This should generate an error. + + del td1[k] # This should generate an error. + del td2[k] # This should generate an error. + del td3[k] # This should generate an error. + + readonly[k] = 1 # This should generate an error. + del readonly[k] # This should generate an error. - td1[k] = "a" - td2[k] = 1 - td3[k] = 1 + if isinstance(mixed["value"], str): + mixed[k] = 1 # This should generate an error. - del td1[k] - del td2[k] + td2[k] += 1 # This should generate an error. - # This should generate an error because "other" is not a defined key. - td3["other"] + td3["other"] # This should generate an error. + td1["known"] = "a" # This should generate an error. + del td1["known"] # This should generate an error. diff --git a/packages/pyright-internal/src/tests/typeEvaluator5.test.ts b/packages/pyright-internal/src/tests/typeEvaluator5.test.ts index 26f4fff897a7..cf92212b8b45 100644 --- a/packages/pyright-internal/src/tests/typeEvaluator5.test.ts +++ b/packages/pyright-internal/src/tests/typeEvaluator5.test.ts @@ -454,7 +454,17 @@ test('TypedDictClosed12', () => { test('TypedDictLiteralString1', () => { const analysisResults = TestUtils.typeAnalyzeSampleFiles(['typedDictLiteralString1.py']); - TestUtils.validateResults(analysisResults, 1); + const expectedLines = TestUtils.readSampleFile('typedDictLiteralString1.py') + .split(/\r?\n/) + .flatMap((line, index) => (line.includes('# This should generate an error.') ? [index] : [])); + TestUtils.validateResults(analysisResults, expectedLines.length); + assert.deepStrictEqual( + analysisResults[0].errors.map((diagnostic) => diagnostic.range.start.line), + expectedLines + ); + analysisResults[0].errors.forEach((diagnostic) => + assert.strictEqual(diagnostic.getRule(), DiagnosticRule.reportGeneralTypeIssues) + ); }); test('DataclassTransform1', () => {