Contravariant Inputs and Covariant Outputs in Python Type Hints
Type variance describes the direction in which type substitutability is permitted when designing function signatures: input parameter types should be contravariant, meaning as generic or wide as possible (e.g., an abstract interface, protocol, or abstract base type rather than a concrete implementation), so that a function can accept the broadest range of compatible types. Return types should be covariant, meaning as specific as possible (the exact concrete type actually produced), so that callers retain access to the full set of operations available on that concrete type rather than being restricted to a narrower supertype's interface. This principle belongs to type theory and static type systems as applied within Python's gradual typing system (PEP 484 and its successors), and it relates to the broader object-oriented design discipline through its connection to the open/closed principle and interface-based (protocol/abstract-base-class) design.
Contravariant Inputs and Covariant Outputs in Python Type Hints
Type variance describes the direction in which type substitutability is permitted when designing function signatures: input parameter types should be contravariant, meaning as generic or wide as poss…