From 778d088b4440009a1a62410dd5e40983fa85f424 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Wed, 11 Oct 2023 11:29:41 +0300 Subject: [PATCH 1/2] gh-110686: Test pattern matching with `runtime_checkable` protocols --- Lib/test/test_patma.py | 143 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 143 insertions(+) diff --git a/Lib/test/test_patma.py b/Lib/test/test_patma.py index dedbc8287841846..93e982d2852e2d1 100644 --- a/Lib/test/test_patma.py +++ b/Lib/test/test_patma.py @@ -2760,6 +2760,120 @@ def test_patma_255(self): self.assertEqual(y, 1) self.assertIs(z, x) + def test_patma_256(self): + # Runtime-checkable protocol + from typing import Protocol, runtime_checkable + + @runtime_checkable + class P(Protocol): + x: int + y: int + + class A: + def __init__(self, x: int, y: int): + self.x = x + self.y = y + + class B(A): ... + + for cls in (A, B): + with self.subTest(cls=cls): + inst = cls(1, 2) + w = 0 + match inst: + case P() as p: + self.assertIsInstance(p, cls) + self.assertEqual(p.x, 1) + self.assertEqual(p.y, 2) + w = 1 + self.assertEqual(w, 1) + + q = 0 + match inst: + case P(x=x, y=y): + self.assertEqual(x, 1) + self.assertEqual(y, 2) + q = 1 + self.assertEqual(q, 1) + + + def test_patma_257(self): + # Runtime-checkable generic protocol + from typing import Generic, TypeVar, Protocol, runtime_checkable + + T = TypeVar('T') # not using PEP695 to be able to backport changes + + @runtime_checkable + class P(Protocol[T]): + a: T + b: T + + class A: + def __init__(self, x: int, y: int): + self.x = x + self.y = y + + class G(Generic[T]): + def __init__(self, x: T, y: T): + self.x = x + self.y = y + + for cls in (A, G): + with self.subTest(cls=cls): + inst = cls(1, 2) + w = 0 + match inst: + case P(): + w = 1 + self.assertEqual(w, 0) + + def test_patma_258(self): + # Runtime-checkable protocol with `__match_args__` + from typing import Protocol, runtime_checkable + + # Used to fail before + # https://github.com/python/cpython/issues/110682 + @runtime_checkable + class P(Protocol): + __match_args__ = ('x', 'y') + x: int + y: int + + class A: + def __init__(self, x: int, y: int): + self.x = x + self.y = y + + class B(A): ... + + for cls in (A, B): + with self.subTest(cls=cls): + inst = cls(1, 2) + w = 0 + match inst: + case P() as p: + self.assertIsInstance(p, cls) + self.assertEqual(p.x, 1) + self.assertEqual(p.y, 2) + w = 1 + self.assertEqual(w, 1) + + q = 0 + match inst: + case P(x=x, y=y): + self.assertEqual(x, 1) + self.assertEqual(y, 2) + q = 1 + self.assertEqual(q, 1) + + g = 0 + match inst: + case P(x, y): + self.assertEqual(x, 1) + self.assertEqual(y, 2) + g = 1 + self.assertEqual(g, 1) + class TestSyntaxErrors(unittest.TestCase): @@ -3198,6 +3312,35 @@ def test_class_pattern_not_type(self): w = 0 self.assertIsNone(w) + def test_regular_protocol(self): + from typing import Protocol + class P(Protocol): ... + msg = ( + 'Instance and class checks can only be used ' + 'with @runtime_checkable protocols' + ) + w = None + with self.assertRaisesRegex(TypeError, msg): + match 1: + case P(): + w = 0 + self.assertIsNone(w) + + def test_positional_patterns_with_regular_protocol(self): + from typing import Protocol + class P(Protocol): + x: int # no `__match_args__` + y: int + class A: + x = 1 + y = 2 + w = None + with self.assertRaises(TypeError): + match A(): + case P(x, y): + w = 0 + self.assertIsNone(w) + class TestValueErrors(unittest.TestCase): From d61513076f2b48a9627bf3d702e2a085a53b920d Mon Sep 17 00:00:00 2001 From: sobolevn Date: Tue, 31 Oct 2023 14:47:29 +0300 Subject: [PATCH 2/2] Address review --- Lib/test/test_patma.py | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/Lib/test/test_patma.py b/Lib/test/test_patma.py index 93e982d2852e2d1..298e78ccee38757 100644 --- a/Lib/test/test_patma.py +++ b/Lib/test/test_patma.py @@ -2760,7 +2760,7 @@ def test_patma_255(self): self.assertEqual(y, 1) self.assertIs(z, x) - def test_patma_256(self): + def test_patma_runtime_checkable_protocol(self): # Runtime-checkable protocol from typing import Protocol, runtime_checkable @@ -2777,7 +2777,7 @@ def __init__(self, x: int, y: int): class B(A): ... for cls in (A, B): - with self.subTest(cls=cls): + with self.subTest(cls=cls.__name__): inst = cls(1, 2) w = 0 match inst: @@ -2797,7 +2797,7 @@ class B(A): ... self.assertEqual(q, 1) - def test_patma_257(self): + def test_patma_generic_protocol(self): # Runtime-checkable generic protocol from typing import Generic, TypeVar, Protocol, runtime_checkable @@ -2819,7 +2819,7 @@ def __init__(self, x: T, y: T): self.y = y for cls in (A, G): - with self.subTest(cls=cls): + with self.subTest(cls=cls.__name__): inst = cls(1, 2) w = 0 match inst: @@ -2827,7 +2827,7 @@ def __init__(self, x: T, y: T): w = 1 self.assertEqual(w, 0) - def test_patma_258(self): + def test_patma_protocol_with_match_args(self): # Runtime-checkable protocol with `__match_args__` from typing import Protocol, runtime_checkable @@ -2847,7 +2847,7 @@ def __init__(self, x: int, y: int): class B(A): ... for cls in (A, B): - with self.subTest(cls=cls): + with self.subTest(cls=cls.__name__): inst = cls(1, 2) w = 0 match inst: @@ -2866,6 +2866,12 @@ class B(A): ... q = 1 self.assertEqual(q, 1) + j = 0 + match inst: + case P(x=1, y=2): + j = 1 + self.assertEqual(j, 1) + g = 0 match inst: case P(x, y): @@ -2874,6 +2880,12 @@ class B(A): ... g = 1 self.assertEqual(g, 1) + h = 0 + match inst: + case P(1, 2): + h = 1 + self.assertEqual(h, 1) + class TestSyntaxErrors(unittest.TestCase):