]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-94499 Add test for private name mangling in class pattern matching (#94500)
authorda-woods <dw-git@d-woods.co.uk>
Wed, 13 Jul 2022 16:13:10 +0000 (17:13 +0100)
committerGitHub <noreply@github.com>
Wed, 13 Jul 2022 16:13:10 +0000 (09:13 -0700)
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>
Lib/test/test_patma.py

index db198f7715783142aa37037d121a466cefa01550..0ed54079c99b302525dcdab986895d4973118795 100644 (file)
@@ -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):