However, I would argue that isn’t very accurate. tuple[type, ...] represents tuple[type[Any], ...], but B.__mro__ actually can only return a tuple of superclasses of B (tuple[type[<super B>], ...])
Proposal
Some sort of built-in way to represent a lower bound, or a contravariant option for type.
This is just a small idea I had to add more details to typing of a library I’m coding, obviously not a polished suggestion. Was wondering what thoughts are on this, and if there are any problems with it?
Yeah that’s what I remember. You could try to suggest it as a typing proposal and you’d probably have a good argument given that basedpython does it, and the authors of basedpython are many of the Python typing experts.
If you do that, you should collect good, real-world motivating examples of where and how this helps. This features adds a complexity to a type system that many Python programmers already barely understand. I think it needs good justification.
I guess the stubs could at least do something like
class type:
__mro__: Final[tuple[type[Self | Any], ...]]
PEP 673 doesn’t allow Self in metaclasses: PEP 673 – Self Type | peps.python.org. If it did, for type[Self] in int.__mro__, Self would bind to type[int] and mean “the metaclass of int”.
Right, what a silly mistake on my part. So if anything it would have to be tuple[Self | type, ...] rather than tuple[type[Self | Any], ...]. But since Self is disallowed, that is not supported either. Since Self is in principle just syntactic sugar for a method scoped type-variable, one could try to substitute that in, though since we are talking about an attribute, this would have to be emulated through __getattr__:
which is not part of the typing spec atm and not supported by all type checkers. Maybe overloading __getattr__, __setattr__ and __delattr__ should be made part of the spec.
I agree, __getattr__, __getattribute__, and __setattr__ should be exempt from overlapping overload checking by type checkers. It’s a gap in the spec and doesn’t match how attribute lookup works at runtime.
For this topic, I’ve also been thinking if we could add a new typing.Super special form. If Self is exactly the bound class, then Super is any class that the bound class inherits from (including itself). So Self is Super’s lower bound. The special form would reflect the super() builtin.
A subscripted Super[T] form would represent super(T). Where type[T] is covariant, Super[T] is contravariant. The example from the first post would be:
The bare Super form would only be valid in a metaclass, where it binds to the class that the attribute is accessed on. Unlike Self, it refers to the implementing class in every metaclass signature, including __new__, where bases: tuple[Super, ...] would be the supertypes of the class being created. It would be invalid in a metaclass’s classmethods and staticmethods, which have no implementing class. Outside a metaclass, Super[Self] would cover “the supertypes of the enclosing class”.