|
| 1 | +# The Liskov Substitution Principle |
| 2 | + |
| 3 | +The Liskov Substitution Principle provides the basis for many of the assumptions a type checker |
| 4 | +generally makes about types in Python: |
| 5 | + |
| 6 | +> Subtype Requirement: Let `ϕ(x)` be a property provable about objects `x` of type `T`. Then |
| 7 | +> `ϕ(y)` should be true for objects `y` of type `S` where `S` is a subtype of `T`. |
| 8 | +
|
| 9 | +In order for a type checker's assumptions to be sound, it is crucial for the type checker to enforce |
| 10 | +the Liskov Substitution Principle on code that it checks. In practice, this usually manifests as |
| 11 | +three checks for a type checker to perform when it checks a subclass `B` of a class `A`: |
| 12 | + |
| 13 | +1. Read-only attributes should only ever be overridden covariantly: if a property `A.p` resolves to |
| 14 | + `int` when accessed, accessing `B.p` should either resolve to `int` or a subtype of `int`. |
| 15 | +1. Method return types should only ever be overidden covariantly: if a method `A.f` returns `int` |
| 16 | + when called, calling `B.f` should also resolve to `int or a subtype of`int\`. |
| 17 | +1. Method parameters should only ever be overridden contravariantly: if a method `A.f` can be called |
| 18 | + with an argument of type `bool`, then the method `B.f` must also be callable with type `bool` |
| 19 | + (though it is permitted for the override to also accept other types) |
| 20 | +1. Mutable attributes should only ever be overridden invariantly: if a mutable attribute `A.attr` |
| 21 | + resolves to type `str`, it can only be overidden on a subclass with exactly the same type. |
| 22 | + |
| 23 | +## Method return types |
| 24 | + |
| 25 | +<!-- snapshot-diagnostics --> |
| 26 | + |
| 27 | +```pyi |
| 28 | +class Super: |
| 29 | + def method(self) -> int: ... |
| 30 | + |
| 31 | +class Sub1(Super): |
| 32 | + def method(self) -> int: ... # fine |
| 33 | + |
| 34 | +class Sub2(Super): |
| 35 | + def method(self) -> bool: ... # fine: `bool` is a subtype of `int` |
| 36 | + |
| 37 | +class Sub3(Super): |
| 38 | + def method(self) -> object: ... # error: [invalid-method-override] |
| 39 | + |
| 40 | +class Sub4(Super): |
| 41 | + def method(self) -> str: ... # error: [invalid-method-override] |
| 42 | +``` |
| 43 | + |
| 44 | +## Method parameters |
| 45 | + |
| 46 | +<!-- snapshot-diagnostics --> |
| 47 | + |
| 48 | +```pyi |
| 49 | +class Super: |
| 50 | + def method(self, x: int, /): ... |
| 51 | + |
| 52 | +class Sub1(Super): |
| 53 | + def method(self, x: int, /): ... # fine |
| 54 | + |
| 55 | +class Sub2(Super): |
| 56 | + def method(self, x: object, /): ... # fine: `method` still accepts any argument of type `bool` |
| 57 | + |
| 58 | +class Sub4(Super): |
| 59 | + def method(self, x: int | str, /): ... # fine |
| 60 | + |
| 61 | +class Sub5(Super): |
| 62 | + def method(self, x: int): ... # fine: `x` can still be passed positionally |
| 63 | + |
| 64 | +class Sub6(Super): |
| 65 | + # fine: `method()` can still be called with just a single argument |
| 66 | + def method(self, x: int, *args): ... |
| 67 | + |
| 68 | +class Sub7(Super): |
| 69 | + def method(self, x: int, **kwargs): ... # fine |
| 70 | + |
| 71 | +class Sub8(Super): |
| 72 | + def method(self, x: int, *args, **kwargs): ... # fine |
| 73 | + |
| 74 | +class Sub9(Super): |
| 75 | + def method(self, x: int, extra_positional_arg=42, /): ... # fine |
| 76 | + |
| 77 | +class Sub10(Super): |
| 78 | + def method(self, x: int, extra_pos_or_kw_arg=42): ... # fine |
| 79 | + |
| 80 | +class Sub11(Super): |
| 81 | + def method(self, x: int, *, extra_kw_only_arg=42): ... # fine |
| 82 | + |
| 83 | +class Sub12(Super): |
| 84 | + # Some calls permitted by the superclass are now no longer allowed |
| 85 | + # (the method can no longer be passed any arguments!) |
| 86 | + def method(self, /): ... # error: [invalid-method-override] |
| 87 | + |
| 88 | +class Sub13(Super): |
| 89 | + # Some calls permitted by the superclass are now no longer allowed |
| 90 | + # (the method can no longer be passed with exactly one argument!) |
| 91 | + def method(self, x, y, /): ... # error: [invalid-method-override] |
| 92 | + |
| 93 | +class Sub14(Super): |
| 94 | + # Some calls permitted by the superclass are now no longer allowed |
| 95 | + # (x can no longer be passed positionally!) |
| 96 | + def method(self, /, *, x): ... # error: [invalid-method-override] |
| 97 | + |
| 98 | +class Sub15(Super): |
| 99 | + # Some calls permitted by the superclass are now no longer allowed |
| 100 | + # (x can no longer be passed any integer -- it now requires a bool!) |
| 101 | + def method(self, x: bool, /): ... # error: [invalid-method-override] |
| 102 | + |
| 103 | +class Super2: |
| 104 | + def method2(self, x): ... |
| 105 | + |
| 106 | +class Sub16(Super2): |
| 107 | + def method2(self, x, /): ... # error: [invalid-method-override] |
| 108 | + |
| 109 | +class Sub17(Super2): |
| 110 | + def method2(self, *, x): ... # error: [invalid-method-override] |
| 111 | + |
| 112 | +class Super3: |
| 113 | + def method3(self, *, x): ... |
| 114 | + |
| 115 | +class Sub18(Super3): |
| 116 | + def method3(self, x): ... # fine: `x` can still be used as a keyword argument |
| 117 | + |
| 118 | +class Sub19(Super3): |
| 119 | + def method3(self, x, /): ... # error: [invalid-method-override] |
| 120 | + |
| 121 | +class Super4: |
| 122 | + def method(self, *args: int, **kwargs: str): ... |
| 123 | + |
| 124 | +class Sub20(Super4): |
| 125 | + def method(self, *args: object, **kwargs: object): ... # fine |
| 126 | + |
| 127 | +class Sub21(Super4): |
| 128 | + def method(self, *args): ... # error: [invalid-method-override] |
| 129 | + |
| 130 | +class Sub22(Super4): |
| 131 | + def method(self, **kwargs): ... # error: [invalid-method-override] |
| 132 | + |
| 133 | +class Sub23(Super4): |
| 134 | + def method(self, x, *args, y, **kwargs): ... # error: [invalid-method-override] |
| 135 | +``` |
| 136 | + |
| 137 | +## The entire class hierarchy is checked |
| 138 | + |
| 139 | +If a child class's method definition is Liskov-compatible with the method definition on its parent |
| 140 | +class, Liskov compatibility must also nonetheless be checked with respect to the method definition |
| 141 | +on its grandparent class. This is because type checkers will treat the child class as a subtype of |
| 142 | +the grandparent class just as much as they treat it as a subtype of the parent class, so |
| 143 | +substitutability with respect to the grandparent class is just as important: |
| 144 | + |
| 145 | +```pyi |
| 146 | +class Grandparent: |
| 147 | + def method(self, x: int) -> None: ... |
| 148 | + |
| 149 | +class Parent(Grandparent): |
| 150 | + def method(self, x: str) -> None: ... # error: [invalid-method-override] |
| 151 | + |
| 152 | +class Child(Parent): |
| 153 | + # compatible with the signature of `Parent.method`, but not with `Grandparent.method`: |
| 154 | + def method(self, x: str) -> None: ... # error: [invalid-method-override] |
| 155 | + |
| 156 | +class OtherChild(Parent): |
| 157 | + # compatible with the signature of `Grandparent.method`, but not with `Parent.method`: |
| 158 | + def method(self, x: int) -> None: ... # error: [invalid-method-override] |
| 159 | +``` |
| 160 | + |
| 161 | +## Excluded methods |
| 162 | + |
| 163 | +Certain special constructor methods are excluded from Liskov checks: `__new__` and `__init__`. None |
| 164 | +of the following classes cause us to emit any errors, therefore: |
| 165 | + |
| 166 | +```pyi |
| 167 | +from typing_extensions import Self |
| 168 | + |
| 169 | +class Grandparent: ... |
| 170 | +class Parent(Grandparent): |
| 171 | + def __new__(cls, x: int) -> Self: ... |
| 172 | + def __init__(self, x: int) -> None: ... |
| 173 | + |
| 174 | +class Child(Parent): |
| 175 | + def __new__(cls, x: str, y: str) -> Self: ... |
| 176 | + def __init__(self, x: str, y: str) -> Self: ... |
| 177 | +``` |
0 commit comments