Check validity of overrides [override]

Mypy checks that an overridden method or attribute is compatible withthe base class. A method in a subclass must accept all argumentsthat the base class method accepts, and the return type must conformto the return type in the base class (Liskov substitution principle).

Argument types can be more general is a subclass (i.e., they can varycontravariantly). The return type can be narrowed in a subclass(i.e., it can vary covariantly). It’s okay to define additionalarguments in a subclass method, as long all extra arguments have defaultvalues or can be left out (*args, for example).

Example:

  1. from typing import Optional, Union
  2.  
  3. class Base:
  4. def method(self,
  5. arg: int) -> Optional[int]:
  6. ...
  7.  
  8. class Derived(Base):
  9. def method(self,
  10. arg: Union[int, str]) -> int: # OK
  11. ...
  12.  
  13. class DerivedBad(Base):
  14. # Error: Argument 1 of "method" is incompatible with "Base" [override]
  15. def method(self,
  16. arg: bool) -> int:
  17. ...