From: da-woods Date: Wed, 13 Jul 2022 16:13:10 +0000 (+0100) Subject: gh-94499 Add test for private name mangling in class pattern matching (#94500) X-Git-Tag: v3.12.0a1~965 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=8d7089f0a016f905fa7ae04ae1bdb9b14c6aeef6;p=thirdparty%2FPython%2Fcpython.git 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 --- diff --git a/Lib/test/test_patma.py b/Lib/test/test_patma.py index db198f771578..0ed54079c99b 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):