Repository navigation
Do not eliminate a TypedDict with extra items on a negative key check - #11663
Conversation
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.
|
@microsoft-github-policy-service agree |
|
🔒 Automated review in progress — Rich Chiodo (@rchiodo) is auto-reviewing this PR. |
|
Verification: The relevant tests could not be fully run in the isolated environment; this review is not fully verified. |
Rich Chiodo (rchiodo)
left a comment
There was a problem hiding this comment.
Approved via Review Center.
|
Verification: The relevant tests could not be fully run in the isolated environment; this review is not fully verified. |
|
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 I leave For the behavior, I built the CLI at both commits and ran the code from #11438 with a probe after the check and 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 Reverting only I have not pushed the rebase, the branch is still at b91d350. |
|
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) ...
|
Fixes #11438.
narrowTypeForTypedDictKeylooks 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 intypedDicts.tswithisProvidedset to true, so on the negative side of anincheck the key is treated as definitely present and the type is eliminated. Everything after the check becomes unreachable:The same function without
extra_itemskeeps the last line reachable, which is what pointed at the fallback.An
extra_itemsdeclaration constrains the type of the extra keys, not which of them are present, so the absence of"b"is consistent withTypedand the subtype has to survive the negative branch. A known item behaves as before, and a closed TypedDict is unaffected because its synthesized entry hasisProvidedfalse.typedDictClosed11.pyalready states the rule in a comment onfunc3, "Baz allows extra items, so it cannot be eliminated here", withreveal_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 andreveal_typeis never evaluated. With this change the same probe fails withexpected "..." 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 0and passes with the change. No existing expected type was weakened.Full jest run here: 2696 passed. The 28 failures are all in
languageServer.test.tsand fail the same way on a clean checkout on this machine, so they are my setup and not the change.check:prettier,check:eslintandtsc --noEmitare clean.