Support `types.new_class`
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
Feature
Currently, types.new_class is not supported.
Source: https://github.com/python/cpython/blob/9fa930dd4859441d36e39ec6db1a6ebdc046d5b1/Lib/types.py#L69
Typeshed: https://github.com/python/typeshed/blob/ca983cd319cb58b0a8fa3a086fad5e75332e0450/stdlib/types.pyi#L344-L359
Pitch
It creates inconsistency with CPython for mypy:

Example:
from types import new_class
X = new_class('X', (int,))
def sum_two(a: X, b: X) -> X:
return a + b
print(sum_two(X(2), X(3)))
There are two ways we can do this:
- Via plugin
get_dynamic_class_hook(), which looks like it was created for this case! (my preference) - Via custom
semanal_new_classhelper
Any tips / preferences from maintainers here?
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by comparing the referenced CPython Lib/types.py and typeshed types.pyi definitions for new_class. Then investigate mypy's get_dynamic_class_hook() entry point and the proposed semanal_new_class helper. Done means the provided example type-checks consistently with CPython behavior, with the implementation approach resolved.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- devtools
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100