Skip to content

Add section about metaclass constructors - #2326

Closed
Viicos wants to merge 2 commits into
python:mainfrom
Viicos:vp/metaclass-constructors
Closed

Add section about metaclass constructors#2326
Viicos wants to merge 2 commits into
python:mainfrom
Viicos:vp/metaclass-constructors

Conversation

@Viicos

@Viicos Viicos commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

No description provided.

Comment on lines +670 to +674
Regardless of the evaluated return type of the implied metaclass call, a
:keyword:`class` statement defines a class, and type checkers should evaluate the
type of the bound name accordingly (``type[MyClass1]`` in the example above).
The implied metaclass call is evaluated only for the purpose of validating
its arguments.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't represent the runtime behavior:

class Meta(type):
    def __new__(cls, *args, **kwargs):
        return 1

class Class(metaclass=Meta): ...

Class
#> 1

But all type checkers currently model this. It is pretty unusual for something like this to happen, and type checkers may rely on the assumption that a class statements always results in a type object.

@srittau srittau added the topic: typing spec For improving the typing spec label Jul 31, 2026
class MyClass4(other=1): # Type error: MyClass4.__init_subclass__() takes no keyword arguments
pass

A metaclass :meth:`!__init__` method has no effect on this rule: when the

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps type checkers could also enforce consistency between __init_subclass__() and __new__()/__init__() in some way?

@Viicos Viicos closed this Jul 31, 2026
@Viicos

Viicos commented Jul 31, 2026

Copy link
Copy Markdown
Contributor Author

Apologies, I keep facing a Github bug opening PRs on the upstream repo. Meant to open this on my fork.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

topic: typing spec For improving the typing spec

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants