Closed
Description
- Are you reporting a bug, or opening a feature request? Bug
- Code that reproduces the issue:
from typing import Union, List, Iterable
from typing_extensions import Literal, TypedDict
# Goal here is to add mypy type so that
# if the dict's key is 'a' or 'b' mypy knows
# that the value should be int or str.
ABDictT = TypedDict('ABDictT', {
'key': Literal['a', 'b'],
'value': Union[str, int]
})
# If key is 'c', or 'd' then mypy should know
# value should be str, or int list.
CDDictT = TypedDict('CDDictT', {
'key': Literal['c', 'd'],
'value': Union[str, Iterable[int]],
})
ValueDictElementT = Union[ABDictT, CDDictT]
ValuesDictT = Iterable[ValueDictElementT]
def some_func(values: ValuesDictT) -> None:
for element in values:
if element['key'] == 'a':
# The reveal_type here show the type to be
# Union[str, int, Iterable[int]] since we
# did a check that element['key'] is 'a' mypy
# should be able to tell that the type is
# ABDictT here?
reveal_type(element['value'])
- What is the actual behavior/output?
$ mypy --strict repro.py
repro.py:30: note: Revealed type is 'Union[builtins.str, builtins.int, typing.Iterable[builtins.int]]'
- What is the behavior/output you expect?
mypy
should be able to tell that the type isABDictT
after checking that the key isa
and therefore the output on thereveal_type
should beUnion[str, int]
.
$ mypy --strict repro.py
repro.py:30: note: Revealed type is 'Union[builtins.str, builtins.int]'
- What are the versions of mypy and Python you are using? Python: 3.6.8, mypy: 0.720
Do you see the same issue after installing mypy from Git master? Yes - What are the mypy flags you are using? (For example --strict-optional) I did use
--strict
but it can also be reproduced with no flags.