Skip to content

Commit

Permalink
pythongh-118569: Add a test for dynamic PEP695 classes (python#118570)
Browse files Browse the repository at this point in the history
  • Loading branch information
sobolevn authored May 4, 2024
1 parent f34e965 commit 5f54758
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions Lib/test/test_type_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,31 @@ class D[U](T):
self.assertIn(int, C.D.__bases__)
self.assertIs(C.D.x, str)


class DynamicClassTest(unittest.TestCase):
def _set_type_params(self, ns, params):
ns['__type_params__'] = params

def test_types_new_class_with_callback(self):
T = TypeVar('T', infer_variance=True)
Klass = types.new_class('Klass', (Generic[T],), {},
lambda ns: self._set_type_params(ns, (T,)))

self.assertEqual(Klass.__bases__, (Generic,))
self.assertEqual(Klass.__orig_bases__, (Generic[T],))
self.assertEqual(Klass.__type_params__, (T,))
self.assertEqual(Klass.__parameters__, (T,))

def test_types_new_class_no_callback(self):
T = TypeVar('T', infer_variance=True)
Klass = types.new_class('Klass', (Generic[T],), {})

self.assertEqual(Klass.__bases__, (Generic,))
self.assertEqual(Klass.__orig_bases__, (Generic[T],))
self.assertEqual(Klass.__type_params__, ()) # must be explicitly set
self.assertEqual(Klass.__parameters__, (T,))


class TypeParamsManglingTest(unittest.TestCase):
def test_mangling(self):
class Foo[__T]:
Expand Down

0 comments on commit 5f54758

Please sign in to comment.