From 8d7089f0a016f905fa7ae04ae1bdb9b14c6aeef6 Mon Sep 17 00:00:00 2001 From: da-woods Date: Wed, 13 Jul 2022 17:13:10 +0100 Subject: 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 --- Lib/test/test_patma.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/Lib/test/test_patma.py b/Lib/test/test_patma.py index db198f7..0ed5407 100644 --- a/Lib/test/test_patma.py +++ b/Lib/test/test_patma.py @@ -2654,6 +2654,20 @@ class TestPatma(unittest.TestCase): self.assertEqual(y, 'bar') + def test_patma_249(self): + class C: + __attr = "eggs" # mangled to _C__attr + _Outer__attr = "bacon" + class Outer: + def f(self, x): + match x: + # looks up __attr, not _C__attr or _Outer__attr + case C(__attr=y): + return y + c = C() + setattr(c, "__attr", "spam") # setattr is needed because we're in a class scope + self.assertEqual(Outer().f(c), "spam") + class TestSyntaxErrors(unittest.TestCase): -- cgit v0.12