Skip to content

Conformance suite: fix classes with metaclasses to not fail at runtime#2258

Merged
carljm merged 1 commit intomainfrom
alex/fix-metaclasses
Apr 10, 2026
Merged

Conformance suite: fix classes with metaclasses to not fail at runtime#2258
carljm merged 1 commit intomainfrom
alex/fix-metaclasses

Conversation

@AlexWaygood
Copy link
Copy Markdown
Member

The creation of the class ModelBase in dataclass_transform_meta.py fails at runtime:

>>> from typing import Any, dataclass_transform
... 
... class ModelField:
...     def __init__(self, *, init: bool = True, default: Any | None = None) -> None:
...         ...
... 
... 
... def model_field(
...     *, init: bool = True, default: Any | None = None, alias: str | None = None
... ) -> Any:
...     raise NotImplementedError
... 
... 
... @dataclass_transform(
...     kw_only_default=True,
...     field_specifiers=(ModelField, model_field),
... )
... class ModelMeta(type):
...     not_a_field: str
... 
...     def __init__(self, not_a_field: str) -> None:
...         self.not_a_field = not_a_field
... 
... 
... class ModelBase(metaclass=ModelMeta):
...     def __init_subclass__(
...         cls,
...         *,
...         frozen: bool = False,
...         kw_only: bool = True,
...         order: bool = True,
...     ) -> None:
...         ...
...         
Traceback (most recent call last):
  File "<python-input-0>", line 25, in <module>
    class ModelBase(metaclass=ModelMeta):
    ...<7 lines>...
            ...
TypeError: ModelMeta.__init__() takes 2 positional arguments but 4 were given

This is because the class statement in Python desugars to an implicit call to the constructor of the class's metaclass. A metaclass's __init__ and __new__ methods are expected to receive four parameters: cls, name, bases, and namespace. ModelMeta.__init__ doesn't, so the class creation fails. I have a ty PR that detects these errors, and it causes ty to regress its conformance score because of this invalidly defined class in the conformance suite.

The same issue exists with Concrete3 and CMeta in protocol_class_objects.py.

The precise signature of the metaclass __init__ methods appears irrelevant in both cases to what the tests are trying to assert. This PR alters the signatures so that the class creation now succeeds at runtime:

>>> from typing import Any, dataclass_transform
... 
... class ModelField:
...     def __init__(self, *, init: bool = True, default: Any | None = None) -> None:
...         ...
... 
... 
... def model_field(
...     *, init: bool = True, default: Any | None = None, alias: str | None = None
... ) -> Any:
...     return 42
... 
... 
... @dataclass_transform(
...     kw_only_default=True,
...     field_specifiers=(ModelField, model_field),
... )
... class ModelMeta(type):
...     not_a_field: str
... 
...     def __init__(self, *args, **kwargs) -> None:
...         self.not_a_field: str = "not a field"
... 
... 
... class ModelBase(metaclass=ModelMeta):
...     def __init_subclass__(
...         cls,
...         *,
...         frozen: bool = False,
...         kw_only: bool = True,
...         order: bool = True,
...     ) -> None:
...         ...
... 
... 
... class Customer1(ModelBase, frozen=True):
...     id: int = model_field()
...     name: str = model_field()
...     name2: str = model_field(alias="other_name", default="None")
...     
>>> class CMeta(type):
...     attr1: int
... 
...     def __init__(self, *args, **kwargs) -> None:
...         self.attr1: int = 1
... 
... 
... class ConcreteC3(metaclass=CMeta):
...     pass
... 
>>> 

@AlexWaygood AlexWaygood added the topic: conformance tests Issues with the conformance test suite label Apr 10, 2026
@AlexWaygood AlexWaygood changed the title Fix classes with metaclasses to not fail at runtime Conformance suite: fix classes with metaclasses to not fail at runtime Apr 10, 2026
@carljm carljm merged commit 4c02514 into main Apr 10, 2026
7 checks passed
@carljm carljm deleted the alex/fix-metaclasses branch April 10, 2026 20:57
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.

2 participants