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>
This commit is contained in:
da-woods 2022-07-13 17:13:10 +01:00 committed by GitHub
parent 4a6bb30eb6
commit 8d7089f0a0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View 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):