Commit ac9cb56
Fix Self type not bound in class body expressions
Summary:
When `Self` was used in a class body expression (not an annotation), it was
not being properly bound to the current class. For example:
```python
class SomeClass:
cache = dict[int, Self]() # Self was unbound here
def get_instance(self) -> Self:
x = self.cache[0]
if x:
return x # Error: Self not assignable to Self@SomeClass
raise RuntimeError()
```
The root cause was that `ensure_expr` (used for regular expressions) did not
call `track_potential_typing_self()`, while `ensure_type_internal` (used for
annotations) did. This meant that `Self` usages in expressions never got a
`SelfTypeLiteral` binding created, so during solving the `Self` remained as
an unbound `SpecialForm::SelfType` instead of being resolved to
`Type::SelfType(ClassType)`.
The fix adds the call to `track_potential_typing_self()` in `ensure_expr`.
Fixes #2250
Reviewed By: migeed-z
Differential Revision: D91745916
fbshipit-source-id: d6e6aee90b451784de33a29fe0391edc5807f44a1 parent c7f3338 commit ac9cb56
2 files changed
+24
-0
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
538 | 538 | | |
539 | 539 | | |
540 | 540 | | |
| 541 | + | |
| 542 | + | |
| 543 | + | |
| 544 | + | |
541 | 545 | | |
542 | 546 | | |
543 | 547 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
209 | 209 | | |
210 | 210 | | |
211 | 211 | | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
0 commit comments