Description
The type
object occupies a rather strange place in the type hierarchy:
>>> type(type) is type
True
(I'm pretty sure that's a flat lie, since you can't instantiate something from itself, but regardless...)
>>> isinstance(type, object)
True
>>> isinstance(object, type)
True
>>> isinstance(type, type)
True
In Java, the (very rough) equivalent is a class, specifically Class<T>
. It's also generic; the type variable refers to the instance. Java has it easy because they don't support metaclasses. Classes are not first class in Java, so their type hierarchy doesn't have to deal with the strange loops shown above.
I realize metaclasses in their full generality are out of scope (at least for now), but a generic Type[T]
like Java's would be nice to have. So far as I can tell from the Mypy documentation, it doesn't currently exist.
Here's some example code which might like to have this feature:
def make_foo(class_: Type[T]) -> T:
# Instantiate and return a T