|
| 1 | +--- |
| 2 | +source: crates/ty_test/src/lib.rs |
| 3 | +expression: snapshot |
| 4 | +--- |
| 5 | +--- |
| 6 | +mdtest name: instance_layout_conflict.md - Tests for ty's `instance-layout-conflict` error code - Built-ins with implicit layouts |
| 7 | +mdtest path: crates/ty_python_semantic/resources/mdtest/instance_layout_conflict.md |
| 8 | +--- |
| 9 | + |
| 10 | +# Python source files |
| 11 | + |
| 12 | +## mdtest_snippet.py |
| 13 | + |
| 14 | +``` |
| 15 | + 1 | # fmt: off |
| 16 | + 2 | |
| 17 | + 3 | class A( # error: [instance-layout-conflict] |
| 18 | + 4 | int, |
| 19 | + 5 | str |
| 20 | + 6 | ): ... |
| 21 | + 7 | |
| 22 | + 8 | class B: |
| 23 | + 9 | __slots__ = ("b",) |
| 24 | +10 | |
| 25 | +11 | class C( # error: [instance-layout-conflict] |
| 26 | +12 | int, |
| 27 | +13 | B, |
| 28 | +14 | ): ... |
| 29 | +15 | class D(int): ... |
| 30 | +16 | |
| 31 | +17 | class E( # error: [instance-layout-conflict] |
| 32 | +18 | D, |
| 33 | +19 | str |
| 34 | +20 | ): ... |
| 35 | +21 | |
| 36 | +22 | class F(int, str, bytes, bytearray): ... # error: [instance-layout-conflict] |
| 37 | +23 | |
| 38 | +24 | # fmt: on |
| 39 | +25 | class Foo(range, str): ... # error: [subclass-of-final-class] |
| 40 | +``` |
| 41 | + |
| 42 | +# Diagnostics |
| 43 | + |
| 44 | +``` |
| 45 | +error[instance-layout-conflict]: Class will raise `TypeError` at runtime due to incompatible bases |
| 46 | + --> src/mdtest_snippet.py:3:7 |
| 47 | + | |
| 48 | +1 | # fmt: off |
| 49 | +2 | |
| 50 | +3 | class A( # error: [instance-layout-conflict] |
| 51 | + | _______^ |
| 52 | +4 | | int, |
| 53 | +5 | | str |
| 54 | +6 | | ): ... |
| 55 | + | |_^ Bases `int` and `str` cannot be combined in multiple inheritance |
| 56 | +7 | |
| 57 | +8 | class B: |
| 58 | + | |
| 59 | +info: Two classes cannot coexist in a class's MRO if they create objects with incompatible memory layouts when they are instantiated |
| 60 | + --> src/mdtest_snippet.py:4:5 |
| 61 | + | |
| 62 | +3 | class A( # error: [instance-layout-conflict] |
| 63 | +4 | int, |
| 64 | + | --- `int` instances have a distinct memory layout because of the way `int` is implemented in a C extension |
| 65 | +5 | str |
| 66 | + | --- `str` instances have a distinct memory layout because of the way `str` is implemented in a C extension |
| 67 | +6 | ): ... |
| 68 | + | |
| 69 | +info: rule `instance-layout-conflict` is enabled by default |
| 70 | +
|
| 71 | +``` |
| 72 | + |
| 73 | +``` |
| 74 | +error[instance-layout-conflict]: Class will raise `TypeError` at runtime due to incompatible bases |
| 75 | + --> src/mdtest_snippet.py:11:7 |
| 76 | + | |
| 77 | + 9 | __slots__ = ("b",) |
| 78 | +10 | |
| 79 | +11 | class C( # error: [instance-layout-conflict] |
| 80 | + | _______^ |
| 81 | +12 | | int, |
| 82 | +13 | | B, |
| 83 | +14 | | ): ... |
| 84 | + | |_^ Bases `int` and `B` cannot be combined in multiple inheritance |
| 85 | +15 | class D(int): ... |
| 86 | + | |
| 87 | +info: Two classes cannot coexist in a class's MRO if they create objects with incompatible memory layouts when they are instantiated |
| 88 | + --> src/mdtest_snippet.py:12:5 |
| 89 | + | |
| 90 | +11 | class C( # error: [instance-layout-conflict] |
| 91 | +12 | int, |
| 92 | + | --- `int` instances have a distinct memory layout because of the way `int` is implemented in a C extension |
| 93 | +13 | B, |
| 94 | + | - `B` instances have a distinct memory layout because `B` defines non-empty `__slots__` |
| 95 | +14 | ): ... |
| 96 | +15 | class D(int): ... |
| 97 | + | |
| 98 | +info: rule `instance-layout-conflict` is enabled by default |
| 99 | +
|
| 100 | +``` |
| 101 | + |
| 102 | +``` |
| 103 | +error[instance-layout-conflict]: Class will raise `TypeError` at runtime due to incompatible bases |
| 104 | + --> src/mdtest_snippet.py:17:7 |
| 105 | + | |
| 106 | +15 | class D(int): ... |
| 107 | +16 | |
| 108 | +17 | class E( # error: [instance-layout-conflict] |
| 109 | + | _______^ |
| 110 | +18 | | D, |
| 111 | +19 | | str |
| 112 | +20 | | ): ... |
| 113 | + | |_^ Bases `D` and `str` cannot be combined in multiple inheritance |
| 114 | +21 | |
| 115 | +22 | class F(int, str, bytes, bytearray): ... # error: [instance-layout-conflict] |
| 116 | + | |
| 117 | +info: Two classes cannot coexist in a class's MRO if they create objects with incompatible memory layouts when they are instantiated |
| 118 | + --> src/mdtest_snippet.py:18:5 |
| 119 | + | |
| 120 | +17 | class E( # error: [instance-layout-conflict] |
| 121 | +18 | D, |
| 122 | + | - |
| 123 | + | | |
| 124 | + | `D` instances have a distinct memory layout because `D` inherits from `int` |
| 125 | + | `int` instances have a distinct memory layout because of the way `int` is implemented in a C extension |
| 126 | +19 | str |
| 127 | + | --- `str` instances have a distinct memory layout because of the way `str` is implemented in a C extension |
| 128 | +20 | ): ... |
| 129 | + | |
| 130 | +info: rule `instance-layout-conflict` is enabled by default |
| 131 | +
|
| 132 | +``` |
| 133 | + |
| 134 | +``` |
| 135 | +error[instance-layout-conflict]: Class will raise `TypeError` at runtime due to incompatible bases |
| 136 | + --> src/mdtest_snippet.py:22:7 |
| 137 | + | |
| 138 | +20 | ): ... |
| 139 | +21 | |
| 140 | +22 | class F(int, str, bytes, bytearray): ... # error: [instance-layout-conflict] |
| 141 | + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Bases `int`, `str`, `bytes` and `bytearray` cannot be combined in multiple inheritance |
| 142 | +23 | |
| 143 | +24 | # fmt: on |
| 144 | + | |
| 145 | +info: Two classes cannot coexist in a class's MRO if they create objects with incompatible memory layouts when they are instantiated |
| 146 | + --> src/mdtest_snippet.py:22:9 |
| 147 | + | |
| 148 | +20 | ): ... |
| 149 | +21 | |
| 150 | +22 | class F(int, str, bytes, bytearray): ... # error: [instance-layout-conflict] |
| 151 | + | --- --- ----- --------- `bytearray` instances have a distinct memory layout because of the way `bytearray` is implemented in a C extension |
| 152 | + | | | | |
| 153 | + | | | `bytes` instances have a distinct memory layout because of the way `bytes` is implemented in a C extension |
| 154 | + | | `str` instances have a distinct memory layout because of the way `str` is implemented in a C extension |
| 155 | + | `int` instances have a distinct memory layout because of the way `int` is implemented in a C extension |
| 156 | +23 | |
| 157 | +24 | # fmt: on |
| 158 | + | |
| 159 | +info: rule `instance-layout-conflict` is enabled by default |
| 160 | +
|
| 161 | +``` |
| 162 | + |
| 163 | +``` |
| 164 | +error[subclass-of-final-class]: Class `Foo` cannot inherit from final class `range` |
| 165 | + --> src/mdtest_snippet.py:25:11 |
| 166 | + | |
| 167 | +24 | # fmt: on |
| 168 | +25 | class Foo(range, str): ... # error: [subclass-of-final-class] |
| 169 | + | ^^^^^ |
| 170 | + | |
| 171 | +info: rule `subclass-of-final-class` is enabled by default |
| 172 | +
|
| 173 | +``` |
0 commit comments