Closed as not planned
Description
I hope I haven't made a mistake by asking here instead of in the mypy repository.
Is there a rationale for the discrepancy in the types in this two cases? In the first case (if_stmt
), reveal_type
correctly identifies the types, while in the second case (match_stmt
), None
seemingly appears out of nowhere. As I understand, is the issue related to type narrowing?
Code
from typing import Literal, TypedDict, Union, reveal_type
IntArrayElements = TypedDict("IntArrayElements", {"elements": list[int]})
IntArrayStruct = TypedDict(
"IntArrayStruct",
{"data": IntArrayElements, "type": Literal["INT_ARRAY"]},
)
NoneStruct = TypedDict(
"NoneStruct",
{"data": None, "type": Literal["NONE"]},
)
response: list[Union[IntArrayStruct, NoneStruct]] = []
for item in response:
if item['type'] == 'INT_ARRAY':
_0_data = item["data"]
reveal_type(_0_data)
_0_elements = _0_data['elements']
reveal_type(_0_elements)
else:
pass
for item in response:
match item['type']:
case 'INT_ARRAY':
_1_data = item["data"]
reveal_type(_1_data) # Line:35
_1_elements = _1_data['elements']
reveal_type(_1_elements) # Line:38
case _:
pass
Expected Behavior
...
main.py:35: note: Revealed type is "TypedDict('__main__.IntArrayElements', {'elements': builtins.list[builtins.int]})"
main.py:38: note: Revealed type is "builtins.list[builtins.int]"
...
Actual Behavior
...
main.py:35: note: Revealed type is "Union[TypedDict('__main__.IntArrayElements', {'elements': builtins.list[builtins.int]}), None]"
...
main.py:38: note: Revealed type is "Union[builtins.list[builtins.int], Any]"
...
Metadata
Metadata
Assignees
Labels
No labels