MCPcopy Index your code
hub / github.com/RustPython/RustPython / test_patma_protocol_with_match_args

Method test_patma_protocol_with_match_args

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

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 4

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

Tested by

no test coverage detected