Commit dac4e35
authored
[ty] Use all reachable bindings for instance attributes and deferred lookups (#18955)
## Summary
Remove a hack in control flow modeling that was treating `return`
statements at the end of function bodies in a special way (basically
considering the state *just before* the `return` statement as the
end-of-scope state). This is not needed anymore now that #18750 has been
merged.
In order to make this work, we now use *all reachable bindings* for
purposes of finding implicit instance attribute assignments as well as
for deferred lookups of symbols. Both would otherwise be affected by
this change:
```py
def C:
def f(self):
self.x = 1 # a reachable binding that is not visible at the end of the scope
return
```
```py
def f():
class X: ... # a reachable binding that is not visible at the end of the scope
x: "X" = X() # deferred use of `X`
return
```
Implicit instance attributes also required another change. We previously
kept track of possibly-unbound instance attributes in some cases, but we
now give up on that completely and always consider *implicit* instance
attributes to be bound if we see a reachable binding in a reachable
method. The previous behavior was somewhat inconsistent anyway because
we also do not consider attributes possibly-unbound in other scenarios:
we do not (and can not) keep track of whether or not methods are called
that define these attributes.
closes astral-sh/ty#711
## Ecosystem analysis
I think this looks very positive!
* We see an unsurprising drop in `possibly-unbound-attribute`
diagnostics (599), mostly for classes that define attributes in `try …
except` blocks, `for` loops, or `if … else: raise …` constructs. There
might obviously also be true positives that got removed, but the vast
majority should be false positives.
* There is also a drop in `possibly-unresolved-reference` /
`unresolved-reference` diagnostics (279+13) from the change to deferred
lookups.
* Some `invalid-type-form` false positives got resolved (13), because we
can now properly look up the names in the annotations.
* There are some new *true* positives in `attrs`, since we understand
the `Attribute` annotation that was previously inferred as `Unknown`
because of a re-assignment after the class definition.
## Test Plan
The existing attributes.md test suite has sufficient coverage here.1 parent ebf59e2 commit dac4e35
File tree
7 files changed
+30
-70
lines changed- crates/ty_python_semantic
- resources/mdtest
- src
- semantic_index
- types
7 files changed
+30
-70
lines changedLines changed: 11 additions & 15 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
43 | 43 | | |
44 | 44 | | |
45 | 45 | | |
46 | | - | |
47 | 46 | | |
48 | 47 | | |
49 | 48 | | |
| |||
265 | 264 | | |
266 | 265 | | |
267 | 266 | | |
268 | | - | |
| 267 | + | |
269 | 268 | | |
270 | 269 | | |
271 | 270 | | |
| |||
342 | 341 | | |
343 | 342 | | |
344 | 343 | | |
345 | | - | |
346 | | - | |
347 | 344 | | |
348 | | - | |
349 | 345 | | |
350 | 346 | | |
351 | 347 | | |
| |||
453 | 449 | | |
454 | 450 | | |
455 | 451 | | |
456 | | - | |
457 | | - | |
| 452 | + | |
| 453 | + | |
458 | 454 | | |
459 | 455 | | |
460 | 456 | | |
| |||
472 | 468 | | |
473 | 469 | | |
474 | 470 | | |
475 | | - | |
476 | 471 | | |
477 | | - | |
478 | 472 | | |
479 | | - | |
480 | 473 | | |
481 | | - | |
482 | 474 | | |
483 | 475 | | |
484 | 476 | | |
| |||
620 | 612 | | |
621 | 613 | | |
622 | 614 | | |
623 | | - | |
624 | | - | |
| 615 | + | |
| 616 | + | |
| 617 | + | |
| 618 | + | |
625 | 619 | | |
626 | 620 | | |
627 | 621 | | |
| |||
1289 | 1283 | | |
1290 | 1284 | | |
1291 | 1285 | | |
| 1286 | + | |
| 1287 | + | |
| 1288 | + | |
| 1289 | + | |
1292 | 1290 | | |
1293 | 1291 | | |
1294 | 1292 | | |
| |||
1320 | 1318 | | |
1321 | 1319 | | |
1322 | 1320 | | |
1323 | | - | |
1324 | 1321 | | |
1325 | 1322 | | |
1326 | | - | |
1327 | 1323 | | |
1328 | 1324 | | |
1329 | 1325 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
62 | 62 | | |
63 | 63 | | |
64 | 64 | | |
65 | | - | |
66 | | - | |
67 | | - | |
68 | | - | |
69 | 65 | | |
70 | 66 | | |
71 | 67 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
118 | 118 | | |
119 | 119 | | |
120 | 120 | | |
121 | | - | |
| 121 | + | |
122 | 122 | | |
123 | 123 | | |
124 | 124 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1122 | 1122 | | |
1123 | 1123 | | |
1124 | 1124 | | |
1125 | | - | |
1126 | | - | |
1127 | | - | |
1128 | | - | |
1129 | | - | |
1130 | | - | |
1131 | | - | |
1132 | | - | |
1133 | | - | |
1134 | | - | |
1135 | | - | |
1136 | | - | |
1137 | | - | |
1138 | | - | |
1139 | | - | |
1140 | | - | |
1141 | | - | |
1142 | | - | |
1143 | | - | |
1144 | | - | |
1145 | | - | |
1146 | | - | |
1147 | | - | |
| 1125 | + | |
1148 | 1126 | | |
1149 | 1127 | | |
1150 | 1128 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1740 | 1740 | | |
1741 | 1741 | | |
1742 | 1742 | | |
1743 | | - | |
| 1743 | + | |
1744 | 1744 | | |
1745 | 1745 | | |
1746 | 1746 | | |
| |||
1771 | 1771 | | |
1772 | 1772 | | |
1773 | 1773 | | |
1774 | | - | |
| 1774 | + | |
1775 | 1775 | | |
1776 | 1776 | | |
1777 | 1777 | | |
| |||
1806 | 1806 | | |
1807 | 1807 | | |
1808 | 1808 | | |
1809 | | - | |
1810 | | - | |
1811 | | - | |
1812 | | - | |
1813 | | - | |
1814 | | - | |
1815 | | - | |
| 1809 | + | |
| 1810 | + | |
1816 | 1811 | | |
1817 | 1812 | | |
1818 | 1813 | | |
| |||
1832 | 1827 | | |
1833 | 1828 | | |
1834 | 1829 | | |
1835 | | - | |
| 1830 | + | |
1836 | 1831 | | |
1837 | 1832 | | |
1838 | 1833 | | |
| |||
1849 | 1844 | | |
1850 | 1845 | | |
1851 | 1846 | | |
1852 | | - | |
1853 | | - | |
1854 | | - | |
1855 | | - | |
1856 | | - | |
1857 | | - | |
1858 | | - | |
1859 | | - | |
1860 | | - | |
1861 | | - | |
| 1847 | + | |
| 1848 | + | |
1862 | 1849 | | |
| 1850 | + | |
| 1851 | + | |
| 1852 | + | |
1863 | 1853 | | |
1864 | 1854 | | |
1865 | 1855 | | |
| |||
1995 | 1985 | | |
1996 | 1986 | | |
1997 | 1987 | | |
1998 | | - | |
1999 | | - | |
2000 | | - | |
2001 | | - | |
| 1988 | + | |
| 1989 | + | |
| 1990 | + | |
| 1991 | + | |
2002 | 1992 | | |
2003 | 1993 | | |
2004 | 1994 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5665 | 5665 | | |
5666 | 5666 | | |
5667 | 5667 | | |
5668 | | - | |
| 5668 | + | |
5669 | 5669 | | |
5670 | 5670 | | |
5671 | 5671 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1650 | 1650 | | |
1651 | 1651 | | |
1652 | 1652 | | |
1653 | | - | |
1654 | | - | |
| 1653 | + | |
| 1654 | + | |
1655 | 1655 | | |
1656 | 1656 | | |
1657 | 1657 | | |
| |||
0 commit comments