Lower Bounds / Contravariant Type

I couldn’t find any discussion on here about this, so was wondering about the implementation of lower bounds.

Take for example:

class A: ...
class B(A): ...

>>> B.__mro__
(B, A, object)

The typing of B.__mro__ according to typeshed is: tuple[type, ...]. typeshed/stdlib/builtins.pyi at main · python/typeshed · GitHub

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.

e.g.

def find_supers[Cls: type](cls: Cls) -> tuple[<super Cls>, ...]:
    return cls.__mro__

OR

def find_supers[Cls](cls: type[Cls, invariant=True]) -> tuple[type[Cls, contravariant=True], ...]:
    return cls.__mro__

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?

You may want to check if basedpython has something like this.

As far as I can tell it does: type parameter bound ranges - basedpython

1 Like

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.

Alternatively, you could just use basedpython.

1 Like

Lower bounds can sometimes be emulated using slack variables: Lower bound for TypeVars · Issue #674 · python/typing · GitHub

In fact, both mypy and tyrecently updated their solvers to handle these cases, and open issues exist for pyrefly and pyright.

I guess the stubs could at least do something like

class type:
    __mro__: Final[tuple[type[Self | Any], ...]]
3 Likes

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”.

I opened Fix `type.__mro__` resolving to `property` by crypt0lith · Pull Request #16549 · python/typeshed · GitHub a month or so ago, which changed __mro__ to the current Final[tuple[type, ...]]. The stubs for type are tricky because type is in its own MRO. Nothing narrower than tuple[type, ...] can annotate a tuple of instances of type under the current type system.

The typing module would probably need a dedicated construct in order to write “supertypes of the receiver” as an annotation.

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__:

class type:
    @overload
    def __getattr__[Cls: type](cls: Cls, name: Literal["__mro__"], /) -> tuple[Cls | type, ...]: ...

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.

Honestly I think it would be kind of useful since it also in principle models type qualifiers like the proposed typing.ReadOnly for attributes. (PEP 767: Annotating Read-Only Attributes - #13 by randolf-scholz, PEP 767: Annotating Read-Only Attributes - #98 by randolf-scholz)

1 Like

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:

def find_supers[T](cls: type[T]) -> tuple[Super[T], ...]:
    return cls.__mro__

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”.

The type stubs would change to:

class type:
    __mro__: Final[tuple[Super, ...]]
    __base__: Final[Super | None]
    def mro(self) -> list[Super]: ...

so that int.__mro__ is tuple[Super[int], ...] and type(self).__mro__ is tuple[Super[Self@Foo], ...] inside a method of implementing class Foo.

1 Like