Skip to content

How to dynamically create PEP695 classes? Let's add a test for it #118569

@sobolevn

Description

@sobolevn

Feature or enhancement

Right now we don't have a single test to dynamically create a PEP-695 class.
It is quite complex, because, for example, it cannot be done via type(), you have to use types.new_class. Example:

import typing

T = typing.TypeVar('T', infer_variance=True)

Klass = type(
    'Klass',
    (typing.Generic[T],),
    {'__type_params__': (T,)},
)

Produces:

Traceback (most recent call last):
  File "/Users/sobolev/Desktop/cpython2/ex.py", line 9, in <module>
    Klass = type(
        'Klass',
    ...<2 lines>...
        # set_type_params,
    )
TypeError: type() doesn't support MRO entry resolution; use types.new_class()

This is fine (I guess?)

Since types.new_class is mentioned and it is there to create classes with complex MROs, we need to be sure that it works the same way as regular class My[T]: ...

I will send a PR.

Linked PRs

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions