python / python/mypy

Support `types.new_class`

Open
#11,302 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature priority-2-low
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:
Снимок экрана 2021-10-09 в 11 32 38

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:

  1. Via plugin get_dynamic_class_hook(), which looks like it was created for this case! (my preference)
  2. Via custom semanal_new_class helper

Any tips / preferences from maintainers here?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.