You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Contrary to the TODO comment there (not sure who wrote that), this is accurate -- all classes are indeed instances of builtins.type. However, it could be a lot more precise, since some classes have a custom metaclass!
For the Foo class here, Type::to_meta_type should return a type representing the class object Meta rather than a type representing the class object builtins.type.
Miscellaneous details on semantics
If a class is a subclass of a class with a custom metaclass, then the subclass will also have that metaclass:
And if the metaclasses of the bases conflict, then this can happen (we'll need to detect this and emit a diagnostic):
>>> classA(type): ...
...
>>> classB(type): ...
...
>>> classC(metaclass=A): ...
...
>>> classD(metaclass=B): ...
...
>>> classE(C, D): ...
...
Traceback (most recent call last):
File "<python-input-4>", line 1, in <module>
class E(C, D): ...
TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases
We'll probably need to add a method to ClassType to retrieve the type of the metaclass. It will probably be best to wait until #14087 has landed before working on this.
The text was updated successfully, but these errors were encountered:
Currently we infer that all class objects are instances of
builtins.type
:ruff/crates/red_knot_python_semantic/src/types.rs
Lines 1211 to 1212 in b7e32b0
Contrary to the TODO comment there (not sure who wrote that), this is accurate -- all classes are indeed instances of
builtins.type
. However, it could be a lot more precise, since some classes have a custom metaclass!For the
Foo
class here,Type::to_meta_type
should return a type representing the class objectMeta
rather than a type representing the class objectbuiltins.type
.Miscellaneous details on semantics
If a class is a subclass of a class with a custom metaclass, then the subclass will also have that metaclass:
And if the metaclasses of the bases conflict, then this can happen (we'll need to detect this and emit a diagnostic):
We'll probably need to add a method to
ClassType
to retrieve the type of the metaclass. It will probably be best to wait until #14087 has landed before working on this.The text was updated successfully, but these errors were encountered: