@@ -1376,13 +1376,13 @@ else: |
1376 | 1376 | reveal_type(val) # N: Revealed type is "Union[__main__.A, None]" |
1377 | 1377 |
|
1378 | 1378 | if val in (None,): |
1379 | | - reveal_type(val) # N: Revealed type is "Union[__main__.A, None]" |
| 1379 | + reveal_type(val) # N: Revealed type is "None" |
1380 | 1380 | else: |
1381 | 1381 | reveal_type(val) # N: Revealed type is "Union[__main__.A, None]" |
1382 | 1382 | if val not in (None,): |
1383 | 1383 | reveal_type(val) # N: Revealed type is "Union[__main__.A, None]" |
1384 | 1384 | else: |
1385 | | - reveal_type(val) # N: Revealed type is "Union[__main__.A, None]" |
| 1385 | + reveal_type(val) # N: Revealed type is "None" |
1386 | 1386 | [builtins fixtures/primitives.pyi] |
1387 | 1387 |
|
1388 | 1388 | [case testNarrowingWithTupleOfTypes] |
@@ -2114,3 +2114,111 @@ else: |
2114 | 2114 |
|
2115 | 2115 | [typing fixtures/typing-medium.pyi] |
2116 | 2116 | [builtins fixtures/ops.pyi] |
| 2117 | + |
| 2118 | + |
| 2119 | +[case testTypeNarrowingStringInLiteralUnion] |
| 2120 | +from typing import Literal, Tuple |
| 2121 | +typ: Tuple[Literal['a', 'b'], ...] = ('a', 'b') |
| 2122 | +x: str = "hi!" |
| 2123 | +if x in typ: |
| 2124 | + reveal_type(x) # N: Revealed type is "Union[Literal['a'], Literal['b']]" |
| 2125 | +else: |
| 2126 | + reveal_type(x) # N: Revealed type is "builtins.str" |
| 2127 | +[builtins fixtures/tuple.pyi] |
| 2128 | +[typing fixtures/typing-medium.pyi] |
| 2129 | + |
| 2130 | +[case testTypeNarrowingStringInLiteralUnionSubset] |
| 2131 | +from typing import Literal, Tuple |
| 2132 | +typeAlpha: Tuple[Literal['a', 'b', 'c'], ...] = ('a', 'b') |
| 2133 | +strIn: str = "b" |
| 2134 | +strOut: str = "c" |
| 2135 | +if strIn in typeAlpha: |
| 2136 | + reveal_type(strIn) # N: Revealed type is "Union[Literal['a'], Literal['b'], Literal['c']]" |
| 2137 | +else: |
| 2138 | + reveal_type(strIn) # N: Revealed type is "builtins.str" |
| 2139 | +if strOut in typeAlpha: |
| 2140 | + reveal_type(strOut) # N: Revealed type is "Union[Literal['a'], Literal['b'], Literal['c']]" |
| 2141 | +else: |
| 2142 | + reveal_type(strOut) # N: Revealed type is "builtins.str" |
| 2143 | +[builtins fixtures/primitives.pyi] |
| 2144 | +[typing fixtures/typing-medium.pyi] |
| 2145 | + |
| 2146 | +[case testNarrowingStringNotInLiteralUnion] |
| 2147 | +from typing import Literal, Tuple |
| 2148 | +typeAlpha: Tuple[Literal['a', 'b', 'c'],...] = ('a', 'b', 'c') |
| 2149 | +strIn: str = "c" |
| 2150 | +strOut: str = "d" |
| 2151 | +if strIn not in typeAlpha: |
| 2152 | + reveal_type(strIn) # N: Revealed type is "builtins.str" |
| 2153 | +else: |
| 2154 | + reveal_type(strIn) # N: Revealed type is "Union[Literal['a'], Literal['b'], Literal['c']]" |
| 2155 | +if strOut in typeAlpha: |
| 2156 | + reveal_type(strOut) # N: Revealed type is "Union[Literal['a'], Literal['b'], Literal['c']]" |
| 2157 | +else: |
| 2158 | + reveal_type(strOut) # N: Revealed type is "builtins.str" |
| 2159 | +[builtins fixtures/primitives.pyi] |
| 2160 | +[typing fixtures/typing-medium.pyi] |
| 2161 | + |
| 2162 | +[case testNarrowingStringInLiteralUnionDontExpand] |
| 2163 | +from typing import Literal, Tuple |
| 2164 | +typeAlpha: Tuple[Literal['a', 'b', 'c'], ...] = ('a', 'b', 'c') |
| 2165 | +strIn: Literal['c'] = "c" |
| 2166 | +reveal_type(strIn) # N: Revealed type is "Literal['c']" |
| 2167 | +#Check we don't expand a Literal into the Union type |
| 2168 | +if strIn not in typeAlpha: |
| 2169 | + reveal_type(strIn) # N: Revealed type is "Literal['c']" |
| 2170 | +else: |
| 2171 | + reveal_type(strIn) # N: Revealed type is "Literal['c']" |
| 2172 | +[builtins fixtures/primitives.pyi] |
| 2173 | +[typing fixtures/typing-medium.pyi] |
| 2174 | + |
| 2175 | +[case testTypeNarrowingStringInMixedUnion] |
| 2176 | +from typing import Literal, Tuple |
| 2177 | +typ: Tuple[Literal['a', 'b'], ...] = ('a', 'b') |
| 2178 | +x: str = "hi!" |
| 2179 | +if x in typ: |
| 2180 | + reveal_type(x) # N: Revealed type is "Union[Literal['a'], Literal['b']]" |
| 2181 | +else: |
| 2182 | + reveal_type(x) # N: Revealed type is "builtins.str" |
| 2183 | +[builtins fixtures/tuple.pyi] |
| 2184 | +[typing fixtures/typing-medium.pyi] |
| 2185 | + |
| 2186 | +[case testTypeNarrowingStringInSet] |
| 2187 | +from typing import Literal, Set |
| 2188 | +typ: Set[Literal['a', 'b']] = {'a', 'b'} |
| 2189 | +x: str = "hi!" |
| 2190 | +if x in typ: |
| 2191 | + reveal_type(x) # N: Revealed type is "Union[Literal['a'], Literal['b']]" |
| 2192 | +else: |
| 2193 | + reveal_type(x) # N: Revealed type is "builtins.str" |
| 2194 | +if x not in typ: |
| 2195 | + reveal_type(x) # N: Revealed type is "builtins.str" |
| 2196 | +else: |
| 2197 | + reveal_type(x) # N: Revealed type is "Union[Literal['a'], Literal['b']]" |
| 2198 | +[builtins fixtures/narrowing.pyi] |
| 2199 | +[typing fixtures/typing-medium.pyi] |
| 2200 | + |
| 2201 | +[case testTypeNarrowingStringInList] |
| 2202 | +from typing import Literal, List |
| 2203 | +typ: List[Literal['a', 'b']] = ['a', 'b'] |
| 2204 | +x: str = "hi!" |
| 2205 | +if x in typ: |
| 2206 | + reveal_type(x) # N: Revealed type is "Union[Literal['a'], Literal['b']]" |
| 2207 | +else: |
| 2208 | + reveal_type(x) # N: Revealed type is "builtins.str" |
| 2209 | +if x not in typ: |
| 2210 | + reveal_type(x) # N: Revealed type is "builtins.str" |
| 2211 | +else: |
| 2212 | + reveal_type(x) # N: Revealed type is "Union[Literal['a'], Literal['b']]" |
| 2213 | +[builtins fixtures/narrowing.pyi] |
| 2214 | +[typing fixtures/typing-medium.pyi] |
| 2215 | + |
| 2216 | +[case testTypeNarrowingUnionStringFloat] |
| 2217 | +from typing import Union |
| 2218 | +def foobar(foo: Union[str, float]): |
| 2219 | + if foo in ['a', 'b']: |
| 2220 | + reveal_type(foo) # N: Revealed type is "builtins.str" |
| 2221 | + else: |
| 2222 | + reveal_type(foo) # N: Revealed type is "Union[builtins.str, builtins.float]" |
| 2223 | +[builtins fixtures/primitives.pyi] |
| 2224 | +[typing fixtures/typing-medium.pyi] |