Skip to content

Commit 0bd69c0

Browse files
added test cases for mirroring isinstance behavior
1 parent 767f5dc commit 0bd69c0

1 file changed

Lines changed: 42 additions & 0 deletions

File tree

Lib/test/test_patma.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3460,6 +3460,48 @@ def test_patma_legacy_union_type(self):
34603460
w = 0
34613461
self.assertIsNone(w)
34623462

3463+
def test_union_mirrors_isinstance_success(self):
3464+
t = int | list[int]
3465+
3466+
try: # get the isinstance result
3467+
reference = isinstance(1, t)
3468+
except TypeError as exc:
3469+
reference = exc
3470+
3471+
try: # get the match-case result
3472+
match 1:
3473+
case t():
3474+
result = True
3475+
case _:
3476+
result = False
3477+
except TypeError as exc:
3478+
result = exc
3479+
3480+
# we should ge the same result
3481+
self.assertIs(result, True)
3482+
self.assertIs(reference, True)
3483+
3484+
def test_union_mirrors_isinstance_failure(self):
3485+
t = list[int] | int
3486+
3487+
try: # get the isinstance result
3488+
reference = isinstance(1, t)
3489+
except TypeError as exc:
3490+
reference = exc
3491+
3492+
try: # get the match-case result
3493+
match 1:
3494+
case t():
3495+
result = True
3496+
case _:
3497+
result = False
3498+
except TypeError as exc:
3499+
result = exc
3500+
3501+
# we should ge the same result
3502+
self.assertIsInstance(result, TypeError)
3503+
self.assertIsInstance(reference, TypeError)
3504+
34633505
def test_generic_union_type(self):
34643506
from collections.abc import Sequence, Set
34653507
t = Sequence[str] | Set[str]

0 commit comments

Comments
 (0)