Skip to content

Commit 329e38e

Browse files
authored
Revert "Narrow based on collection containment (#17344)" (#17865)
This reverts commit ed0cd4a. See #17864 for context. The implementation has some issues, and I'm reverting the PR to unblock the 1.12 release. Closes #17864. Closes #17841.
1 parent 87b005a commit 329e38e

File tree

3 files changed

+8
-128
lines changed

3 files changed

+8
-128
lines changed

mypy/checker.py

+5-10
Original file line numberDiff line numberDiff line change
@@ -6023,16 +6023,11 @@ def has_no_custom_eq_checks(t: Type) -> bool:
60236023
if_map, else_map = {}, {}
60246024

60256025
if left_index in narrowable_operand_index_to_hash:
6026-
collection_item_type = get_proper_type(builtin_item_type(iterable_type))
6027-
# Narrow if the collection is a subtype
6028-
if (
6029-
collection_item_type is not None
6030-
and collection_item_type != item_type
6031-
and is_subtype(collection_item_type, item_type)
6032-
):
6033-
if_map[operands[left_index]] = collection_item_type
6034-
# Try and narrow away 'None'
6035-
elif is_overlapping_none(item_type):
6026+
# We only try and narrow away 'None' for now
6027+
if is_overlapping_none(item_type):
6028+
collection_item_type = get_proper_type(
6029+
builtin_item_type(iterable_type)
6030+
)
60366031
if (
60376032
collection_item_type is not None
60386033
and not is_overlapping_none(collection_item_type)

test-data/unit/check-narrowing.test

+2-110
Original file line numberDiff line numberDiff line change
@@ -1392,13 +1392,13 @@ else:
13921392
reveal_type(val) # N: Revealed type is "Union[__main__.A, None]"
13931393

13941394
if val in (None,):
1395-
reveal_type(val) # N: Revealed type is "None"
1395+
reveal_type(val) # N: Revealed type is "Union[__main__.A, None]"
13961396
else:
13971397
reveal_type(val) # N: Revealed type is "Union[__main__.A, None]"
13981398
if val not in (None,):
13991399
reveal_type(val) # N: Revealed type is "Union[__main__.A, None]"
14001400
else:
1401-
reveal_type(val) # N: Revealed type is "None"
1401+
reveal_type(val) # N: Revealed type is "Union[__main__.A, None]"
14021402
[builtins fixtures/primitives.pyi]
14031403

14041404
[case testNarrowingWithTupleOfTypes]
@@ -2130,111 +2130,3 @@ else:
21302130

21312131
[typing fixtures/typing-medium.pyi]
21322132
[builtins fixtures/ops.pyi]
2133-
2134-
2135-
[case testTypeNarrowingStringInLiteralUnion]
2136-
from typing import Literal, Tuple
2137-
typ: Tuple[Literal['a', 'b'], ...] = ('a', 'b')
2138-
x: str = "hi!"
2139-
if x in typ:
2140-
reveal_type(x) # N: Revealed type is "Union[Literal['a'], Literal['b']]"
2141-
else:
2142-
reveal_type(x) # N: Revealed type is "builtins.str"
2143-
[builtins fixtures/tuple.pyi]
2144-
[typing fixtures/typing-medium.pyi]
2145-
2146-
[case testTypeNarrowingStringInLiteralUnionSubset]
2147-
from typing import Literal, Tuple
2148-
typeAlpha: Tuple[Literal['a', 'b', 'c'], ...] = ('a', 'b')
2149-
strIn: str = "b"
2150-
strOut: str = "c"
2151-
if strIn in typeAlpha:
2152-
reveal_type(strIn) # N: Revealed type is "Union[Literal['a'], Literal['b'], Literal['c']]"
2153-
else:
2154-
reveal_type(strIn) # N: Revealed type is "builtins.str"
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 testNarrowingStringNotInLiteralUnion]
2163-
from typing import Literal, Tuple
2164-
typeAlpha: Tuple[Literal['a', 'b', 'c'],...] = ('a', 'b', 'c')
2165-
strIn: str = "c"
2166-
strOut: str = "d"
2167-
if strIn not in typeAlpha:
2168-
reveal_type(strIn) # N: Revealed type is "builtins.str"
2169-
else:
2170-
reveal_type(strIn) # N: Revealed type is "Union[Literal['a'], Literal['b'], Literal['c']]"
2171-
if strOut in typeAlpha:
2172-
reveal_type(strOut) # N: Revealed type is "Union[Literal['a'], Literal['b'], Literal['c']]"
2173-
else:
2174-
reveal_type(strOut) # N: Revealed type is "builtins.str"
2175-
[builtins fixtures/primitives.pyi]
2176-
[typing fixtures/typing-medium.pyi]
2177-
2178-
[case testNarrowingStringInLiteralUnionDontExpand]
2179-
from typing import Literal, Tuple
2180-
typeAlpha: Tuple[Literal['a', 'b', 'c'], ...] = ('a', 'b', 'c')
2181-
strIn: Literal['c'] = "c"
2182-
reveal_type(strIn) # N: Revealed type is "Literal['c']"
2183-
#Check we don't expand a Literal into the Union type
2184-
if strIn not in typeAlpha:
2185-
reveal_type(strIn) # N: Revealed type is "Literal['c']"
2186-
else:
2187-
reveal_type(strIn) # N: Revealed type is "Literal['c']"
2188-
[builtins fixtures/primitives.pyi]
2189-
[typing fixtures/typing-medium.pyi]
2190-
2191-
[case testTypeNarrowingStringInMixedUnion]
2192-
from typing import Literal, Tuple
2193-
typ: Tuple[Literal['a', 'b'], ...] = ('a', 'b')
2194-
x: str = "hi!"
2195-
if x in typ:
2196-
reveal_type(x) # N: Revealed type is "Union[Literal['a'], Literal['b']]"
2197-
else:
2198-
reveal_type(x) # N: Revealed type is "builtins.str"
2199-
[builtins fixtures/tuple.pyi]
2200-
[typing fixtures/typing-medium.pyi]
2201-
2202-
[case testTypeNarrowingStringInSet]
2203-
from typing import Literal, Set
2204-
typ: Set[Literal['a', 'b']] = {'a', 'b'}
2205-
x: str = "hi!"
2206-
if x in typ:
2207-
reveal_type(x) # N: Revealed type is "Union[Literal['a'], Literal['b']]"
2208-
else:
2209-
reveal_type(x) # N: Revealed type is "builtins.str"
2210-
if x not in typ:
2211-
reveal_type(x) # N: Revealed type is "builtins.str"
2212-
else:
2213-
reveal_type(x) # N: Revealed type is "Union[Literal['a'], Literal['b']]"
2214-
[builtins fixtures/narrowing.pyi]
2215-
[typing fixtures/typing-medium.pyi]
2216-
2217-
[case testTypeNarrowingStringInList]
2218-
from typing import Literal, List
2219-
typ: List[Literal['a', 'b']] = ['a', 'b']
2220-
x: str = "hi!"
2221-
if x in typ:
2222-
reveal_type(x) # N: Revealed type is "Union[Literal['a'], Literal['b']]"
2223-
else:
2224-
reveal_type(x) # N: Revealed type is "builtins.str"
2225-
if x not in typ:
2226-
reveal_type(x) # N: Revealed type is "builtins.str"
2227-
else:
2228-
reveal_type(x) # N: Revealed type is "Union[Literal['a'], Literal['b']]"
2229-
[builtins fixtures/narrowing.pyi]
2230-
[typing fixtures/typing-medium.pyi]
2231-
2232-
[case testTypeNarrowingUnionStringFloat]
2233-
from typing import Union
2234-
def foobar(foo: Union[str, float]):
2235-
if foo in ['a', 'b']:
2236-
reveal_type(foo) # N: Revealed type is "builtins.str"
2237-
else:
2238-
reveal_type(foo) # N: Revealed type is "Union[builtins.str, builtins.float]"
2239-
[builtins fixtures/primitives.pyi]
2240-
[typing fixtures/typing-medium.pyi]

test-data/unit/fixtures/narrowing.pyi

+1-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Builtins stub used in check-narrowing test cases.
2-
from typing import Generic, Sequence, Tuple, Type, TypeVar, Union, Iterable
2+
from typing import Generic, Sequence, Tuple, Type, TypeVar, Union
33

44

55
Tco = TypeVar('Tco', covariant=True)
@@ -15,13 +15,6 @@ class function: pass
1515
class ellipsis: pass
1616
class int: pass
1717
class str: pass
18-
class float: pass
1918
class dict(Generic[KT, VT]): pass
2019

2120
def isinstance(x: object, t: Union[Type[object], Tuple[Type[object], ...]]) -> bool: pass
22-
23-
class list(Sequence[Tco]):
24-
def __contains__(self, other: object) -> bool: pass
25-
class set(Iterable[Tco], Generic[Tco]):
26-
def __init__(self, iterable: Iterable[Tco] = ...) -> None: ...
27-
def __contains__(self, item: object) -> bool: pass

0 commit comments

Comments
 (0)