Skip to content

Do not eliminate a TypedDict with extra items on a negative key check - #11663

Merged
Rich Chiodo (rchiodo) merged 1 commit into
microsoft:mainfrom
luantaraschi:fix/typeddict-extra-items-key-narrowing
Aug 24, 2026
Merged

Rich Chiodo (rchiodo) merged 1 commit into
microsoft:mainfrom
luantaraschi:fix/typeddict-extra-items-key-narrowing

Conversation

@luantaraschi

Copy link
Copy Markdown
Contributor

Fixes #11438.

narrowTypeForTypedDictKey looks the tested key up in the known items and falls back to the "extra items" entry when it is not there. That entry is built in typedDicts.ts with isProvided set to true, so on the negative side of an in check the key is treated as definitely present and the type is eliminated. Everything after the check becomes unreachable:

class Typed(TypedDict, extra_items=str):
    a: str

def test(d: Typed) -> None:
    if "b" in d:
        return
    print("here")  # not analyzed

The same function without extra_items keeps the last line reachable, which is what pointed at the fallback.

An extra_items declaration constrains the type of the extra keys, not which of them are present, so the absence of "b" is consistent with Typed and the subtype has to survive the negative branch. A known item behaves as before, and a closed TypedDict is unaffected because its synthesized entry has isProvided false.

typedDictClosed11.py already states the rule in a comment on func3, "Baz allows extra items, so it cannot be eliminated here", with reveal_type(u, expected_text="Baz") in the else branch. That assertion is vacuous today: I swapped the expected text for a string that cannot match and the test still passed, because the branch is unreachable and reveal_type is never evaluated. With this change the same probe fails with expected "..." but received "Baz", so the branch runs and reveals what the comment says it should.

The two functions added to that sample each carry one intentional error in the branch that has to be reachable, so the test fails on main with Expected 2 errors, got 0 and passes with the change. No existing expected type was weakened.

Full jest run here: 2696 passed. The 28 failures are all in languageServer.test.ts and fail the same way on a clean checkout on this machine, so they are my setup and not the change. check:prettier, check:eslint and tsc --noEmit are clean.

Failure classification: B (Pyright limitation).

narrowTypeForTypedDictKey falls back to the "extra items" entry when the
key being tested is not a known item. That entry is created with
isProvided set to true, so the negative branch of an "in" check treated
the key as definitely present and eliminated the type, which made the
code after the check unreachable.

An "extra items" declaration constrains the type of the extra keys, not
which of them are present, so the absence of a given key is consistent
with the type and the subtype has to be kept.

No expected type in an existing test was weakened. The sample gains two
new functions that each carry one intentional error in the branch that
must be reachable, which is why the expected error count for
TypedDictClosed11 goes from 0 to 2.
@luantaraschi

Copy link
Copy Markdown
Contributor Author

@microsoft-github-policy-service agree

@rchiodo

Copy link
Copy Markdown
Collaborator

🔒 Automated review in progress — Rich Chiodo (@rchiodo) is auto-reviewing this PR.

@rchiodo

Copy link
Copy Markdown
Collaborator

Verification: The relevant tests could not be fully run in the isolated environment; this review is not fully verified.

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

Approved via Review Center.

@rchiodo Rich Chiodo (rchiodo) added the review-auto:approved Automated review: no blocking findings (approval posted). label Aug 24, 2026
@rchiodo

Copy link
Copy Markdown
Collaborator

Verification: The relevant tests could not be fully run in the isolated environment; this review is not fully verified.

@luantaraschi

Copy link
Copy Markdown
Contributor Author

The review note says the tests could not be fully run there, so here is the run from my machine, against current main.

I rebased b91d350 onto bacb814 and it applied cleanly; none of the three files has an upstream change since I opened the PR. In packages/pyright-internal, with npx jest -w 3 --testPathIgnorePatterns languageServer.test.ts:

Test Suites: 1 skipped, 71 passed, 71 of 72 total
Tests:       8 skipped, 2696 passed, 2704 total
Time:        205.966 s

I leave languageServer.test.ts out because plain jest does not build the test server bundle it needs, and its 28 failures reproduce on a clean checkout here. check:prettier and check:eslint are clean, and tsc --noEmit in pyright-internal passes.

For the behavior, I built the CLI at both commits and ran the code from #11438 with a probe after the check and reportUnreachable on:

class Typed(TypedDict, extra_items=str):
    a: str

def test(d: Typed) -> None:
    if "b" in d:
        return
    reveal_type(d)
    probe: int = "here"

On main the reveal_type line comes back as unreachable, and neither it nor the bad assignment is evaluated. With the change there is no unreachable report, reveal_type(d) gives Typed, and the assignment error is reported.

Reverting only typeGuards.ts while keeping the new sample makes TypedDictClosed11 fail with Expected 2 errors, got 0, so the two functions I added do cover the fix.

I have not pushed the rebase, the branch is still at b91d350.

@github-actions

Copy link
Copy Markdown
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

sympy (https://github.com/sympy/sympy)
- An internal error occurred while type checking file ".../projects/sympy/sympy/solvers/benchmarks/bench_solvers.py": TypeError: Cannot read properties of undefined (reading 'isStubFile')
+ An internal error occurred while type checking file ".../projects/sympy/sympy/solvers/diophantine/tests/test_diophantine.py": TypeError: Cannot read properties of undefined (reading 'isStubFile')

core (https://github.com/home-assistant/core)
- An internal error occurred while type checking file ".../projects/core/homeassistant/components/panasonic_viera/__init__.py": TypeError: Cannot read properties of undefined (reading 'isStubFile')
-     at t.isAnnotationEvaluationPostponed (...pyright_to_test/packages/pyright/dist/pyright-internal/src/analyzer/analyzerFileInfo.ts:69:18)
+ An internal error occurred while type checking file ".../projects/core/homeassistant/components/paj_gps/entity.py": Error: Debug Failure. Did not find evaluation scope
+     at w (...pyright_to_test/packages/pyright/dist/pyright-internal/src/analyzer/parseTreeUtils.ts:1035:5)
+     at Object.t.getExecutionScopeNode (...pyright_to_test/packages/pyright/dist/pyright-internal/src/analyzer/parseTreeUtils.ts:1079:27)
-     at ns (...pyright_to_test/packages/pyright/dist/pyright-internal/src/analyzer/typeEvaluator.ts:23101:14)
+     at nt (...pyright_to_test/packages/pyright/dist/pyright-internal/src/analyzer/typeEvaluator.ts:22416:46)
-     at ...pyright_to_test/packages/pyright/dist/pyright-internal/src/analyzer/typeEvaluator.ts:14183:29
+     at ...pyright_to_test/packages/pyright/dist/pyright-internal/src/analyzer/typeEvaluator.ts:5231:44
-     at ...pyright_to_test/packages/pyright/dist/pyright-internal/src/analyzer/typeEvaluator.ts:14320:5
+     at ...pyright_to_test/packages/pyright/dist/pyright-internal/src/analyzer/typeEvaluator.ts:5312:5
-     at ...pyright_to_test/packages/pyright/dist/pyright-internal/src/analyzer/typeEvaluator.ts:11875:5
-     at ny.allowDiagnostics (...pyright_to_test/packages/pyright/dist/pyright-internal/src/analyzer/typeEvaluator.ts:11292:5)
-     at ny (...pyright_to_test/packages/pyright/dist/pyright-internal/src/analyzer/typeEvaluator.ts:23419:20)
-     at ...pyright_to_test/packages/pyright/dist/pyright-internal/src/analyzer/typeEvaluator.ts:11114:24
-     at ...pyright_to_test/packages/pyright/dist/pyright-internal/src/analyzer/typeEvaluator.ts:4663:43
-     at m (...pyright_to_test/packages/pyright/dist/pyright-internal/src/analyzer/typeUtils.ts:782:9)
-     at s (...pyright_to_test/packages/pyright/dist/pyright-internal/src/analyzer/typeEvaluator.ts:4647:13)
-     at tv (...pyright_to_test/packages/pyright/dist/pyright-internal/src/analyzer/typeEvaluator.ts:4705:13)
-     at t4 (...pyright_to_test/packages/pyright/dist/pyright-internal/src/analyzer/typeEvaluator.ts:11110:26)
-     at ...pyright_to_test/packages/pyright/dist/pyright-internal/src/analyzer/typeEvaluator.ts:9060:36
-     at ...pyright_to_test/packages/pyright/dist/pyright-internal/src/analyzer/typeEvaluator.ts:9130:5
-     at nu (...pyright_to_test/packages/pyright/dist/pyright-internal/src/analyzer/typeEvaluator.ts:23378:28)
-     at ...pyright_to_test/packages/pyright/dist/pyright-internal/src/analyzer/typeEvaluator.ts:1425:30
+     at ...pyright_to_test/packages/pyright/dist/pyright-internal/src/analyzer/typeEvaluator.ts:5987:32
+     at ...pyright_to_test/packages/pyright/dist/pyright-internal/src/analyzer/typeEvaluator.ts:6095:5
-     at aD (...pyright_to_test/packages/pyright/dist/pyright-internal/src/analyzer/typeEvaluator.ts:18379:35)
+     at eB (...pyright_to_test/packages/pyright/dist/pyright-internal/src/analyzer/typeEvaluator.ts:1617:5)
-     at a7 (...pyright_to_test/packages/pyright/dist/pyright-internal/src/analyzer/typeEvaluator.ts:22220:25)
+     at n.timeOperation (...pyright_to_test/packages/pyright/dist/pyright-internal/src/common/timing.ts:40:20)
-     at ...pyright_to_test/packages/pyright/dist/pyright-internal/src/analyzer/typeEvaluator.ts:24390:17
-     at a9 (...pyright_to_test/packages/pyright/dist/pyright-internal/src/analyzer/typeEvaluator.ts:22358:9)
-     at a8 (...pyright_to_test/packages/pyright/dist/pyright-internal/src/analyzer/typeEvaluator.ts:22327:16)
+     at A (...pyright_to_test/packages/pyright/dist/pyright-internal/src/analyzer/codeFlowEngine.ts:1784:46)
+     at t (...pyright_to_test/packages/pyright/dist/pyright-internal/src/analyzer/codeFlowEngine.ts:517:29)
+     at Object.getTypeFromCodeFlow (...pyright_to_test/packages/pyright/dist/pyright-internal/src/analyzer/codeFlowEngine.ts:941:13)
-     at nI (...pyright_to_test/packages/pyright/dist/pyright-internal/src/analyzer/typeEvaluator.ts:24389:32)
+     at nt (...pyright_to_test/packages/pyright/dist/pyright-internal/src/analyzer/typeEvaluator.ts:22453:25)
+     at ...pyright_to_test/packages/pyright/dist/pyright-internal/src/analyzer/typeEvaluator.ts:5231:44
+     at ...pyright_to_test/packages/pyright/dist/pyright-internal/src/analyzer/typeEvaluator.ts:5312:5
+     at eB (...pyright_to_test/packages/pyright/dist/pyright-internal/src/analyzer/typeEvaluator.ts:1617:5)
+     at nI (...pyright_to_test/packages/pyright/dist/pyright-internal/src/analyzer/typeEvaluator.ts:24342:44)
-     at nS (...pyright_to_test/packages/pyright/dist/pyright-internal/src/analyzer/typeEvaluator.ts:24527:16)
-     at no (...pyright_to_test/packages/pyright/dist/pyright-internal/src/analyzer/typeEvaluator.ts:23174:24)
-     at aB (...pyright_to_test/packages/pyright/dist/pyright-internal/src/analyzer/typeEvaluator.ts:19812:35)
-     at ...pyright_to_test/packages/pyright/dist/pyright-internal/src/analyzer/typeEvaluator.ts:19351:40
-     at t.invalidateTypeCacheIfCanceled (...pyright_to_test/packages/pyright/dist/pyright-internal/src/common/cancellationUtils.ts:38:16)
- Error performing analysis: TypeError: Cannot read properties of undefined (reading 'isStubFile')
-     at t.isAnnotationEvaluationPostponed (...pyright_to_test/packages/pyright/dist/pyright-internal/src/analyzer/analyzerFileInfo.ts:69:18)
-     at ns (...pyright_to_test/packages/pyright/dist/pyright-internal/src/analyzer/typeEvaluator.ts:23101:14)
-     at ...pyright_to_test/packages/pyright/dist/pyright-internal/src/analyzer/typeEvaluator.ts:14183:29
+     at ...pyright_to_test/packages/pyright/dist/pyright-internal/src/analyzer/typeEvaluator.ts:5173:39
-     at ...pyright_to_test/packages/pyright/dist/pyright-internal/src/analyzer/typeEvaluator.ts:14320:5
+     at ...pyright_to_test/packages/pyright/dist/pyright-internal/src/analyzer/typeEvaluator.ts:5312:5
-     at ...pyright_to_test/packages/pyright/dist/pyright-internal/src/analyzer/typeEvaluator.ts:11875:5
-     at ny.allowDiagnostics (...pyright_to_test/packages/pyright/dist/pyright-internal/src/analyzer/typeEvaluator.ts:11292:5)
-     at ny (...pyright_to_test/packages/pyright/dist/pyright-internal/src/analyzer/typeEvaluator.ts:23419:20)
-     at ...pyright_to_test/packages/pyright/dist/pyright-internal/src/analyzer/typeEvaluator.ts:11114:24
-     at ...pyright_to_test/packages/pyright/dist/pyright-internal/src/analyzer/typeEvaluator.ts:4663:43
-     at m (...pyright_to_test/packages/pyright/dist/pyright-internal/src/analyzer/typeUtils.ts:782:9)
-     at s (...pyright_to_test/packages/pyright/dist/pyright-internal/src/analyzer/typeEvaluator.ts:4647:13)
-     at tv (...pyright_to_test/packages/pyright/dist/pyright-internal/src/analyzer/typeEvaluator.ts:4705:13)
-     at t4 (...pyright_to_test/packages/pyright/dist/pyright-internal/src/analyzer/typeEvaluator.ts:11110:26)
-     at ...pyright_to_test/packages/pyright/dist/pyright-internal/src/analyzer/typeEvaluator.ts:9060:36
-     at ...pyright_to_test/packages/pyright/dist/pyright-internal/src/analyzer/typeEvaluator.ts:9130:5
-     at nu (...pyright_to_test/packages/pyright/dist/pyright-internal/src/analyzer/typeEvaluator.ts:23378:28)
-     at ...pyright_to_test/packages/pyright/dist/pyright-internal/src/analyzer/typeEvaluator.ts:1425:30
+     at n.timeOperation (...pyright_to_test/packages/pyright/dist/pyright-internal/src/common/timing.ts:40:20)
-     at aD (...pyright_to_test/packages/pyright/dist/pyright-internal/src/analyzer/typeEvaluator.ts:18379:35)
+     at e (...pyright_to_test/packages/pyright/dist/pyright-internal/src/analyzer/operations.ts:339:39)
-     at a7 (...pyright_to_test/packages/pyright/dist/pyright-internal/src/analyzer/typeEvaluator.ts:22220:25)
-     at ...pyright_to_test/packages/pyright/dist/pyright-internal/src/analyzer/typeEvaluator.ts:24390:17
+     at ...pyright_to_test/packages/pyright/dist/pyright-internal/src/analyzer/typeEvaluator.ts:1474:30
-     at a9 (...pyright_to_test/packages/pyright/dist/pyright-internal/src/analyzer/typeEvaluator.ts:22358:9)
+     at eB (...pyright_to_test/packages/pyright/dist/pyright-internal/src/analyzer/typeEvaluator.ts:1617:5)
+     at n.timeOperation (...pyright_to_test/packages/pyright/dist/pyright-internal/src/common/timing.ts:40:20)
+     at e (...pyright_to_test/packages/pyright/dist/pyright-internal/src/analyzer/operations.ts:302:38)
+     at ...pyright_to_test/packages/pyright/dist/pyright-internal/src/analyzer/typeEvaluator.ts:1474:30
+     at eB (...pyright_to_test/packages/pyright/dist/pyright-internal/src/analyzer/typeEvaluator.ts:1617:5)
+ Error performing analysis: Error: Debug Failure. Did not find evaluation scope
+     at w (...pyright_to_test/packages/pyright/dist/pyright-internal/src/analyzer/parseTreeUtils.ts:1035:5)
+     at Object.t.getExecutionScopeNode (...pyright_to_test/packages/pyright/dist/pyright-internal/src/analyzer/parseTreeUtils.ts:1079:27)
-     at a8 (...pyright_to_test/packages/pyright/dist/pyright-internal/src/analyzer/typeEvaluator.ts:22327:16)
+     at nt (...pyright_to_test/packages/pyright/dist/pyright-internal/src/analyzer/typeEvaluator.ts:22416:46)
+     at ...pyright_to_test/packages/pyright/dist/pyright-internal/src/analyzer/typeEvaluator.ts:5231:44
+     at ...pyright_to_test/packages/pyright/dist/pyright-internal/src/analyzer/typeEvaluator.ts:5312:5
+     at eB (...pyright_to_test/packages/pyright/dist/pyright-internal/src/analyzer/typeEvaluator.ts:1617:5)
+     at ...pyright_to_test/packages/pyright/dist/pyright-internal/src/analyzer/typeEvaluator.ts:5987:32
+     at ...pyright_to_test/packages/pyright/dist/pyright-internal/src/analyzer/typeEvaluator.ts:6095:5

... (truncated 27 lines) ...

@rchiodo
Rich Chiodo (rchiodo) merged commit 5b448bb into microsoft:main Aug 24, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

review-auto:approved Automated review: no blocking findings (approval posted).

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TypedDict with extra_items breaks control flow analyis of in operator

2 participants