Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-118569: Add a test for dynamic PEP695 classes #118570

Merged
merged 1 commit into from
May 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading