gh-115775: Compiler adds __static_attributes__ field to classes (#115913)

This commit is contained in:
Irit Katriel 2024-03-26 15:18:17 +00:00 committed by GitHub
parent 70969d53a7
commit 79be75735c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 136 additions and 11 deletions

View file

@ -1960,6 +1960,64 @@ class TestSourcePositions(unittest.TestCase):
)
class TestExpectedAttributes(unittest.TestCase):
def test_basic(self):
class C:
def f(self):
self.a = self.b = 42
self.assertIsInstance(C.__static_attributes__, tuple)
self.assertEqual(sorted(C.__static_attributes__), ['a', 'b'])
def test_nested_function(self):
class C:
def f(self):
self.x = 1
self.y = 2
self.x = 3 # check deduplication
def g(self, obj):
self.y = 4
self.z = 5
def h(self, a):
self.u = 6
self.v = 7
obj.self = 8
self.assertEqual(sorted(C.__static_attributes__), ['u', 'v', 'x', 'y', 'z'])
def test_nested_class(self):
class C:
def f(self):
self.x = 42
self.y = 42
class D:
def g(self):
self.y = 42
self.z = 42
self.assertEqual(sorted(C.__static_attributes__), ['x', 'y'])
self.assertEqual(sorted(C.D.__static_attributes__), ['y', 'z'])
def test_subclass(self):
class C:
def f(self):
self.x = 42
self.y = 42
class D(C):
def g(self):
self.y = 42
self.z = 42
self.assertEqual(sorted(C.__static_attributes__), ['x', 'y'])
self.assertEqual(sorted(D.__static_attributes__), ['y', 'z'])
class TestExpressionStackSize(unittest.TestCase):
# These tests check that the computed stack size for a code object
# stays within reasonable bounds (see issue #21523 for an example