MCPcopy Create free account
hub / github.com/python/cpython / test_patma_protocol_with_match_args

Method test_patma_protocol_with_match_args

Lib/test/test_patma.py:2832–2889  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

2830 self.assertEqual(w, 0)
2831
2832 def test_patma_protocol_with_match_args(self):
2833 # Runtime-checkable protocol with `__match_args__`
2834 from typing import Protocol, runtime_checkable
2835
2836 # Used to fail before
2837 # https://github.com/python/cpython/issues/110682
2838 @runtime_checkable
2839 class P(Protocol):
2840 __match_args__ = ('x', 'y')
2841 x: int
2842 y: int
2843
2844 class A:
2845 def __init__(self, x: int, y: int):
2846 self.x = x
2847 self.y = y
2848
2849 class B(A): ...
2850
2851 for cls in (A, B):
2852 with self.subTest(cls=cls.__name__):
2853 inst = cls(1, 2)
2854 w = 0
2855 match inst:
2856 case P() as p:
2857 self.assertIsInstance(p, cls)
2858 self.assertEqual(p.x, 1)
2859 self.assertEqual(p.y, 2)
2860 w = 1
2861 self.assertEqual(w, 1)
2862
2863 q = 0
2864 match inst:
2865 case P(x=x, y=y):
2866 self.assertEqual(x, 1)
2867 self.assertEqual(y, 2)
2868 q = 1
2869 self.assertEqual(q, 1)
2870
2871 j = 0
2872 match inst:
2873 case P(x=1, y=2):
2874 j = 1
2875 self.assertEqual(j, 1)
2876
2877 g = 0
2878 match inst:
2879 case P(x, y):
2880 self.assertEqual(x, 1)
2881 self.assertEqual(y, 2)
2882 g = 1
2883 self.assertEqual(g, 1)
2884
2885 h = 0
2886 match inst:
2887 case P(1, 2):
2888 h = 1
2889 self.assertEqual(h, 1)

Callers

nothing calls this directly

Calls 4

assertIsInstanceMethod · 0.80
clsClass · 0.50
subTestMethod · 0.45
assertEqualMethod · 0.45

Tested by

no test coverage detected