10000 gh-94499 Add test for private name mangling in class pattern matching… · python/cpython@8d7089f · GitHub
[go: up one dir, main page]

Skip to content

Commit 8d7089f

Browse files
gh-94499 Add test for private name mangling in class pattern matching (#94500)
The current status quo is that private attribute names are not mangled when a class is matched. I've added a test to document/legimize this behaviour. Co-authored-by: Brandt Bucher <brandtbucher@gmail.com>
1 parent 4a6bb30 commit 8d7089f

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

Lib/test/test_patma.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2654,6 +2654,20 @@ def get(key, default=None):
26542654

26552655
self.assertEqual(y, 'bar')
26562656

2657+
def test_patma_249(self):
2658+
class C:
2659+
__attr = "eggs" # mangled to _C__attr
2660+
_Outer__attr = "bacon"
2661+
class Outer:
2662+
def f(self, x):
2663+
match x:
2664+
# looks up __attr, not _C__attr or _Outer__attr
2665+
case C(__attr=y):
2666+
return y
2667+
c = C()
2668+
setattr(c, "__attr", "spam") # setattr is needed because we're in a class scope
2669+
self.assertEqual(Outer().f(c), "spam")
2670+
26572671

26582672
class TestSyntaxErrors(unittest.TestCase):
26592673

0 commit comments

Comments
 (0)
0