Python typing what does TypeVar(A, B, covariant=True) mean?

Covariance and contra-variance are terms that relate to the intersection between object orientation and generics.

Here’s the question this concept is trying to answer:

  1. We have a couple of “regular”, “object-oriented” classes, Base and Derived.
  2. We also have some generic type – let’s say List[T].
  3. We know that Derived can be used anywhere Base can – does that mean that List[Derived] can be used wherever List[Base] can?
  4. Could it be the other way around? Maybe it’s the reverse direction and now List[Base] can be used wherever List[Derived] can?

If the answer to (3) is yes, it’s called covariance and we’ll say declare List as having covariance=True. If the answer to (4) is true, it’s called contra-variance. If none is true, it’s invariant.

Bounds also come from the intersection of OO and generics. When we define a generic type MyType<T> – does it mean that T can be any type at all? Or, may I impose some limitations on what T might be? Bounds allow me to state that the upper bound of T is, for example, the class Derived. In that case, Base can’t be used with MyType – but Derived and all its subclasses can.

The definition of covariance and contravariance can be found in this section of PEP-484.

Leave a Comment