Skip to content

Commit 176c97a

Browse files
committed
Fix narrowing for AbstractSet and Mapping
This builds on the tech I added a few days ago in #21281
1 parent be46935 commit 176c97a

2 files changed

Lines changed: 24 additions & 0 deletions

File tree

mypy/checker.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9731,6 +9731,8 @@ def visit_starred_pattern(self, p: StarredPattern) -> None:
97319731
"builtins.bytes": "builtins.bytes",
97329732
"builtins.bytearray": "builtins.bytes",
97339733
"builtins.memoryview": "builtins.bytes",
9734+
"typing.Mapping": "typing.Mapping",
9735+
"typing.AbstractSet": "typing.AbstractSet",
97349736
}
97359737

97369738
VALUE_EQUALITY_DOMAINS: Final = {**OPEN_VALUE_EQUALITY_DOMAINS, **CLOSED_VALUE_EQUALITY_DOMAINS}

test-data/unit/pythoneval.test

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2212,3 +2212,25 @@ d3: A = d1 | d2
22122212
reveal_type(d1 | d2)
22132213
[out]
22142214
_testDictOrUnionEdgeCases.py:10: note: Revealed type is "dict[str, dict[str, ... | int] | int]"
2215+
2216+
[case testNarrowingMappingAndAbstractSet]
2217+
from typing import Mapping, AbstractSet
2218+
2219+
def f1(x: Mapping[str, int]) -> None:
2220+
if isinstance(x, dict):
2221+
reveal_type(x)
2222+
2223+
if x == {}:
2224+
reveal_type(x)
2225+
2226+
def f2(x: AbstractSet[int]) -> None:
2227+
if isinstance(x, set):
2228+
reveal_type(x)
2229+
2230+
if x == set():
2231+
reveal_type(x)
2232+
[out]
2233+
_testNarrowingMappingAndAbstractSet.py:5: note: Revealed type is "dict[Any, Any]"
2234+
_testNarrowingMappingAndAbstractSet.py:8: note: Revealed type is "typing.Mapping[str, int]"
2235+
_testNarrowingMappingAndAbstractSet.py:12: note: Revealed type is "set[Any]"
2236+
_testNarrowingMappingAndAbstractSet.py:15: note: Revealed type is "typing.AbstractSet[int]"

0 commit comments

Comments
 (0)