Skip to content

Commit be7210f

Browse files
committed
Add test where we also try a union of typed dicts
1 parent ac0f317 commit be7210f

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

test-data/unit/check-literal.test

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2351,6 +2351,43 @@ UnicodeDict = TypedDict(b'UnicodeDict', {'key': int})
23512351
[builtins fixtures/dict.pyi]
23522352
[typing fixtures/typing-full.pyi]
23532353

2354+
[case testLiteralIntelligentIndexingMultiTypedDict]
2355+
from typing import Union
2356+
from typing_extensions import Literal
2357+
from mypy_extensions import TypedDict
2358+
2359+
class A: pass
2360+
class B: pass
2361+
class C: pass
2362+
class D: pass
2363+
2364+
class D1(TypedDict):
2365+
a: A
2366+
b: B
2367+
c: C
2368+
2369+
class D2(TypedDict):
2370+
b: B
2371+
c: C
2372+
d: D
2373+
2374+
x: Union[D1, D2]
2375+
bad_keys: Literal['a', 'b', 'c', 'd']
2376+
good_keys: Literal['b', 'c']
2377+
2378+
x[bad_keys] # E: TypedDict "D1" has no key 'd' \
2379+
# E: TypedDict "D2" has no key 'a'
2380+
x.get(bad_keys) # E: TypedDict "D1" has no key 'd' \
2381+
# E: TypedDict "D2" has no key 'a'
2382+
x.get(bad_keys, 3) # E: TypedDict "D1" has no key 'd' \
2383+
# E: TypedDict "D2" has no key 'a'
2384+
2385+
reveal_type(x[good_keys]) # N: Revealed type is 'Union[__main__.B, __main__.C]'
2386+
reveal_type(x.get(good_keys)) # N: Revealed type is 'Union[__main__.B, __main__.C]'
2387+
reveal_type(x.get(good_keys, 3)) # N: Revealed type is 'Union[__main__.B, builtins.int, __main__.C]'
2388+
2389+
[builtins fixtures/dict.pyi]
2390+
[typing fixtures/typing-full.pyi]
23542391

23552392
--
23562393
-- Interactions with 'Final'

0 commit comments

Comments
 (0)