Skip to content

Commit 4e4826f

Browse files
authored
Add regression test cases (#17869)
#17864
1 parent 16ba7f4 commit 4e4826f

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

test-data/unit/check-narrowing.test

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2291,3 +2291,45 @@ def f4(x: SE) -> None:
22912291
else:
22922292
reveal_type(x) # N: Revealed type is "Literal[__main__.SE.B]"
22932293
[builtins fixtures/primitives.pyi]
2294+
2295+
[case testConsistentNarrowingEqAndIn]
2296+
# flags: --python-version 3.10
2297+
2298+
# https://github.com/python/mypy/issues/17864
2299+
def f(x: str | int) -> None:
2300+
if x == "x":
2301+
reveal_type(x) # N: Revealed type is "Union[builtins.str, builtins.int]"
2302+
y = x
2303+
2304+
if x in ["x"]:
2305+
# TODO: we should fix this reveal https://github.com/python/mypy/issues/3229
2306+
reveal_type(x) # N: Revealed type is "Union[builtins.str, builtins.int]"
2307+
y = x
2308+
z = x
2309+
z = y
2310+
[builtins fixtures/primitives.pyi]
2311+
2312+
[case testConsistentNarrowingInWithCustomEq]
2313+
# flags: --python-version 3.10
2314+
2315+
# https://github.com/python/mypy/issues/17864
2316+
class C:
2317+
def __init__(self, x: int) -> None:
2318+
self.x = x
2319+
2320+
def __eq__(self, other: object) -> bool:
2321+
raise
2322+
# Example implementation:
2323+
# if isinstance(other, C) and other.x == self.x:
2324+
# return True
2325+
# return NotImplemented
2326+
2327+
class D(C):
2328+
pass
2329+
2330+
def f(x: C) -> None:
2331+
if x in [D(5)]:
2332+
reveal_type(x) # D # N: Revealed type is "__main__.C"
2333+
2334+
f(C(5))
2335+
[builtins fixtures/primitives.pyi]

0 commit comments

Comments
 (0)