Skip to content

fix super().__call__ calls in constructors_call_metaclass#2252

Merged
carljm merged 1 commit into
python:mainfrom
carljm:cjm/fixmeta
Apr 8, 2026
Merged

fix super().__call__ calls in constructors_call_metaclass#2252
carljm merged 1 commit into
python:mainfrom
carljm:cjm/fixmeta

Conversation

@carljm

@carljm carljm commented Apr 8, 2026

Copy link
Copy Markdown
Member

These calls were incorrect as written, they wrongly included the explicit cls arg, even though this is bound into the descriptor lookup; __call__ is a regular method, not a static method like __new__. At runtime, the examples as written would succeed with no argument (cls would be passed as x) and fail with a single argument (two arguments passed where only one is expected).

This wasn't caught by type checkers (and has no impact on the conformance results) because the use of *args, **kwargs means type checkers can't track the correct arity or argument types through the metaclass __call__.

Incorrect version (main branch):

>>> class Meta3(type):
...     def __call__(cls, *args, **kwargs):
...         return super().__call__(cls, *args, **kwargs)
...
>>> class Class3(metaclass=Meta3):
...     def __new__(cls, x: int):
...         return super().__new__(cls)
...
>>> Class3()
<__main__.Class3 object at 0x102890ad0>
>>> Class3(1)
Traceback (most recent call last):
  File "<python-input-5>", line 1, in <module>
    Class3(1)
    ~~~~~~^^^
  File "<python-input-2>", line 3, in __call__
    return super().__call__(cls, *args, **kwargs)
           ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^
TypeError: Class3.__new__() takes 2 positional arguments but 3 were given

Fixed version (this PR):

>>> class Meta3(type):
...     def __call__(cls, *args, **kwargs):
...         return super().__call__(*args, **kwargs)
...
>>> class Class3(metaclass=Meta3):
...     def __new__(cls, x: int):
...         return super().__new__(cls)
...
>>> Class3()
Traceback (most recent call last):
  File "<python-input-8>", line 1, in <module>
    Class3()
    ~~~~~~^^
  File "<python-input-6>", line 3, in __call__
    return super().__call__(*args, **kwargs)
           ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^
TypeError: Class3.__new__() missing 1 required positional argument: 'x'
>>> Class3(1)
<__main__.Class3 object at 0x102890c20>

@carljm carljm added the topic: conformance tests Issues with the conformance test suite label Apr 8, 2026
@carljm carljm merged commit 7bf2409 into python:main Apr 8, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

topic: conformance tests Issues with the conformance test suite

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants