Commit 1cb7601
committed
fix(parametric-is): resolve a subclass's arguments down its base chain
the probe assumed a subclass's type arguments line up positionally with its
base's, so `class Odd[T](list[int])` reported `T` as list's argument:
`Odd[str]() is list[str]` answered True even though `Odd` is a `list[int]`.
a reordering base (`class Swap[A, B](dict[B, A])`) was wrong the same way.
arguments are now resolved down the declared base chain, substituting the
type parameters into each base, so a base that fixes, reorders or nests its
arguments is followed faithfully. this subsumes both default patches: a bare
class starts from its own pep 696 defaults and the same walk carries them
through, so the special cases for own-identity and base-recorded parameters
collapse into one mechanism.
the positional rule survives only as a last resort, after resolution fails,
for a builtin registered as a *virtual* subclass of an abc (`list` for
`Sequence`) which has no base to walk. because it now runs on the resolved
base rather than the original subclass, `Odd[str]` reaches it as `list[int]`
and still answers `Sequence[int]` correctly.
fix(parametric-is): resolve own-identity type-param defaults
a type parameter that appears only in the class's own identity
(`class A[T = Never]`) is recorded in no base, so the mro walk had nothing
to resolve and the probe found no arguments at all. read it straight off
`__type_params__` instead.
this is precisely the case constructor reification cannot cover: it fills
defaults by injecting `A[int]()`, but only when the default has a runtime
spelling, so an unspellable one like `Never` leaves the constructor bare.
the two mechanisms are complementary — injection covers own-identity
parameters with spellable defaults, this covers the rest.
restricted to `klass is origin`: a subclass's parameters have no positional
relationship to a base's, so `class Odd[T = str](list[int])` must not report
`str` as list's argument.
fix(parametric-is): resolve pep 696 defaults in the runtime probe
a class records its generic bases *unsubstituted* — `class L[T = Never]
(list[T])` stores `list[T]`, never `list[Never]` — so the probe was
comparing a bare TypeVar against the target and never matching. a
parameter left at its default now resolves to that default, so
`L() is list[Never]` answers True.
the substitution is applied only when the value records no explicit
`__orig_class__`: an `L[int]()` fixes `T` itself, and reading the class
default over the top of that would report an argument the value never had
(`L[str]() is list[int]` must stay False).1 parent 808e22a commit 1cb7601
4 files changed
Lines changed: 294 additions & 72 deletions
File tree
- crates
- by_transforms
- src/transforms
- tests
- ty_python_semantic/resources/mdtest
- ty/docs
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
85 | 85 | | |
86 | 86 | | |
87 | 87 | | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
88 | 172 | | |
89 | | - | |
90 | | - | |
91 | | - | |
92 | | - | |
93 | | - | |
94 | | - | |
95 | | - | |
96 | | - | |
97 | | - | |
98 | | - | |
99 | | - | |
100 | | - | |
101 | | - | |
102 | | - | |
103 | | - | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
104 | 176 | | |
105 | | - | |
106 | | - | |
107 | | - | |
108 | | - | |
109 | | - | |
110 | | - | |
| 177 | + | |
| 178 | + | |
111 | 179 | | |
112 | 180 | | |
113 | 181 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
27 | | - | |
| 27 | + | |
28 | 28 | | |
29 | 29 | | |
30 | 30 | | |
| |||
159 | 159 | | |
160 | 160 | | |
161 | 161 | | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
162 | 223 | | |
163 | 224 | | |
164 | 225 | | |
| |||
0 commit comments