|
| 1 | +--- |
| 2 | +source: crates/ty_test/src/lib.rs |
| 3 | +expression: snapshot |
| 4 | +--- |
| 5 | +--- |
| 6 | +mdtest name: liskov.md - The Liskov Substitution Principle - The entire class hierarchy is checked |
| 7 | +mdtest path: crates/ty_python_semantic/resources/mdtest/liskov.md |
| 8 | +--- |
| 9 | + |
| 10 | +# Python source files |
| 11 | + |
| 12 | +## stub.pyi |
| 13 | + |
| 14 | +``` |
| 15 | + 1 | class Grandparent: |
| 16 | + 2 | def method(self, x: int) -> None: ... |
| 17 | + 3 | |
| 18 | + 4 | class Parent(Grandparent): |
| 19 | + 5 | def method(self, x: str) -> None: ... # error: [invalid-method-override] |
| 20 | + 6 | |
| 21 | + 7 | class Child(Parent): |
| 22 | + 8 | # compatible with the signature of `Parent.method`, but not with `Grandparent.method`: |
| 23 | + 9 | def method(self, x: str) -> None: ... # error: [invalid-method-override] |
| 24 | +10 | |
| 25 | +11 | class OtherChild(Parent): |
| 26 | +12 | # compatible with the signature of `Grandparent.method`, but not with `Parent.method`: |
| 27 | +13 | def method(self, x: int) -> None: ... # error: [invalid-method-override] |
| 28 | +``` |
| 29 | + |
| 30 | +## other_stub.pyi |
| 31 | + |
| 32 | +``` |
| 33 | + 1 | class A: |
| 34 | + 2 | def get(self, default): ... |
| 35 | + 3 | |
| 36 | + 4 | class B(A): |
| 37 | + 5 | def get(self, default, /): ... # error: [invalid-method-override] |
| 38 | + 6 | |
| 39 | + 7 | get = 56 |
| 40 | + 8 | |
| 41 | + 9 | class C(B): |
| 42 | +10 | # `get` appears in the symbol table of `C`, |
| 43 | +11 | # but that doesn't confuse our diagnostic... |
| 44 | +12 | foo = get |
| 45 | +13 | |
| 46 | +14 | class D(C): |
| 47 | +15 | # compatible with `C.get` and `B.get`, but not with `A.get` |
| 48 | +16 | def get(self, my_default): ... # error: [invalid-method-override] |
| 49 | +``` |
| 50 | + |
| 51 | +# Diagnostics |
| 52 | + |
| 53 | +``` |
| 54 | +error[invalid-method-override]: Invalid override of method `method` |
| 55 | + --> src/stub.pyi:2:9 |
| 56 | + | |
| 57 | +1 | class Grandparent: |
| 58 | +2 | def method(self, x: int) -> None: ... |
| 59 | + | ---------------------------- `Grandparent.method` defined here |
| 60 | +3 | |
| 61 | +4 | class Parent(Grandparent): |
| 62 | +5 | def method(self, x: str) -> None: ... # error: [invalid-method-override] |
| 63 | + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Definition is incompatible with `Grandparent.method` |
| 64 | +6 | |
| 65 | +7 | class Child(Parent): |
| 66 | + | |
| 67 | +info: This violates the Liskov Substitution Principle |
| 68 | +info: rule `invalid-method-override` is enabled by default |
| 69 | + |
| 70 | +``` |
| 71 | + |
| 72 | +``` |
| 73 | +error[invalid-method-override]: Invalid override of method `method` |
| 74 | + --> src/stub.pyi:9:9 |
| 75 | + | |
| 76 | + 7 | class Child(Parent): |
| 77 | + 8 | # compatible with the signature of `Parent.method`, but not with `Grandparent.method`: |
| 78 | + 9 | def method(self, x: str) -> None: ... # error: [invalid-method-override] |
| 79 | + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Definition is incompatible with `Grandparent.method` |
| 80 | +10 | |
| 81 | +11 | class OtherChild(Parent): |
| 82 | + | |
| 83 | + ::: src/stub.pyi:2:9 |
| 84 | + | |
| 85 | + 1 | class Grandparent: |
| 86 | + 2 | def method(self, x: int) -> None: ... |
| 87 | + | ---------------------------- `Grandparent.method` defined here |
| 88 | + 3 | |
| 89 | + 4 | class Parent(Grandparent): |
| 90 | + | |
| 91 | +info: This violates the Liskov Substitution Principle |
| 92 | +info: rule `invalid-method-override` is enabled by default |
| 93 | + |
| 94 | +``` |
| 95 | + |
| 96 | +``` |
| 97 | +error[invalid-method-override]: Invalid override of method `method` |
| 98 | + --> src/stub.pyi:13:9 |
| 99 | + | |
| 100 | +11 | class OtherChild(Parent): |
| 101 | +12 | # compatible with the signature of `Grandparent.method`, but not with `Parent.method`: |
| 102 | +13 | def method(self, x: int) -> None: ... # error: [invalid-method-override] |
| 103 | + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Definition is incompatible with `Parent.method` |
| 104 | + | |
| 105 | + ::: src/stub.pyi:5:9 |
| 106 | + | |
| 107 | + 4 | class Parent(Grandparent): |
| 108 | + 5 | def method(self, x: str) -> None: ... # error: [invalid-method-override] |
| 109 | + | ---------------------------- `Parent.method` defined here |
| 110 | + 6 | |
| 111 | + 7 | class Child(Parent): |
| 112 | + | |
| 113 | +info: This violates the Liskov Substitution Principle |
| 114 | +info: rule `invalid-method-override` is enabled by default |
| 115 | + |
| 116 | +``` |
| 117 | + |
| 118 | +``` |
| 119 | +error[invalid-method-override]: Invalid override of method `get` |
| 120 | + --> src/other_stub.pyi:2:9 |
| 121 | + | |
| 122 | +1 | class A: |
| 123 | +2 | def get(self, default): ... |
| 124 | + | ------------------ `A.get` defined here |
| 125 | +3 | |
| 126 | +4 | class B(A): |
| 127 | +5 | def get(self, default, /): ... # error: [invalid-method-override] |
| 128 | + | ^^^^^^^^^^^^^^^^^^^^^ Definition is incompatible with `A.get` |
| 129 | +6 | |
| 130 | +7 | get = 56 |
| 131 | + | |
| 132 | +info: This violates the Liskov Substitution Principle |
| 133 | +info: rule `invalid-method-override` is enabled by default |
| 134 | + |
| 135 | +``` |
| 136 | + |
| 137 | +``` |
| 138 | +error[invalid-method-override]: Invalid override of method `get` |
| 139 | + --> src/other_stub.pyi:16:9 |
| 140 | + | |
| 141 | +14 | class D(C): |
| 142 | +15 | # compatible with `C.get` and `B.get`, but not with `A.get` |
| 143 | +16 | def get(self, my_default): ... # error: [invalid-method-override] |
| 144 | + | ^^^^^^^^^^^^^^^^^^^^^ Definition is incompatible with `A.get` |
| 145 | + | |
| 146 | + ::: src/other_stub.pyi:2:9 |
| 147 | + | |
| 148 | + 1 | class A: |
| 149 | + 2 | def get(self, default): ... |
| 150 | + | ------------------ `A.get` defined here |
| 151 | + 3 | |
| 152 | + 4 | class B(A): |
| 153 | + | |
| 154 | +info: This violates the Liskov Substitution Principle |
| 155 | +info: rule `invalid-method-override` is enabled by default |
| 156 | + |
| 157 | +``` |
0 commit comments