@@ -2022,12 +2022,14 @@ class D(TypedDict):
2022
2022
foo: int
2023
2023
2024
2024
2025
- d : D | list[str]
2025
+ d_or_list : D | list[str]
2026
2026
2027
- if 'foo' in d:
2028
- assert_type(d, Union[D, list[str]])
2027
+ if 'foo' in d_or_list:
2028
+ assert_type(d_or_list, Union[D, list[str]])
2029
+ elif 'bar' in d_or_list:
2030
+ assert_type(d_or_list, list[str])
2029
2031
else:
2030
- assert_type(d , list[str])
2032
+ assert_type(d_or_list , list[str])
2031
2033
2032
2034
[builtins fixtures/dict.pyi]
2033
2035
[typing fixtures/typing-typeddict.pyi]
@@ -2046,12 +2048,6 @@ class D1(TypedDict):
2046
2048
class D2(TypedDict):
2047
2049
bar: int
2048
2050
2049
- d_or_list: D1 | list[str]
2050
-
2051
- if 'foo' in d_or_list:
2052
- assert_type(d_or_list, Union[D1, list[str]])
2053
- else:
2054
- assert_type(d_or_list, list[str])
2055
2051
2056
2052
d: D1 | D2
2057
2053
@@ -2077,11 +2073,9 @@ TD = TypeVar('TD', D1, D2)
2077
2073
def f(arg: TD) -> None:
2078
2074
value: int
2079
2075
if 'foo' in arg:
2080
- assert_type(d, Union[D1, D2]) # strangely enough it's seen as a union
2081
- value = arg['foo'] # but acts here as D1
2076
+ assert_type(arg['foo'], int)
2082
2077
else:
2083
- assert_type(d, Union[D1, D2]) # ditto here, but D2
2084
- value = arg['bar']
2078
+ assert_type(arg['bar'], int)
2085
2079
2086
2080
2087
2081
[builtins fixtures/dict.pyi]
0 commit comments