Skip to content

Commit 5dd6cab

Browse files
added test cases for mirroring isinstance behavior
1 parent 052f885 commit 5dd6cab

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

Lib/test/test_patma.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3469,6 +3469,48 @@ def test_patma_legacy_union_type(self):
34693469
w = 0
34703470
self.assertIsNone(w)
34713471

3472+
def test_union_mirrors_isinstance_success(self):
3473+
t = int | list[int]
3474+
3475+
try: # get the isinstance result
3476+
reference = isinstance(1, t)
3477+
except TypeError as exc:
3478+
reference = exc
3479+
3480+
try: # get the match-case result
3481+
match 1:
3482+
case t():
3483+
result = True
3484+
case _:
3485+
result = False
3486+
except TypeError as exc:
3487+
result = exc
3488+
3489+
# we should ge the same result
3490+
self.assertIs(result, True)
3491+
self.assertIs(reference, True)
3492+
3493+
def test_union_mirrors_isinstance_failure(self):
3494+
t = list[int] | int
3495+
3496+
try: # get the isinstance result
3497+
reference = isinstance(1, t)
3498+
except TypeError as exc:
3499+
reference = exc
3500+
3501+
try: # get the match-case result
3502+
match 1:
3503+
case t():
3504+
result = True
3505+
case _:
3506+
result = False
3507+
except TypeError as exc:
3508+
result = exc
3509+
3510+
# we should ge the same result
3511+
self.assertIsInstance(result, TypeError)
3512+
self.assertIsInstance(reference, TypeError)
3513+
34723514
def test_generic_union_type(self):
34733515
from collections.abc import Sequence, Set
34743516
t = Sequence[str] | Set[str]

0 commit comments

Comments
 (0)