[ty] Make special cases for subscript inference exhaustive#22035
[ty] Make special cases for subscript inference exhaustive#22035AlexWaygood merged 1 commit intomainfrom
Conversation
Diagnostic diff on typing conformance testsNo changes detected when running ty on typing conformance tests ✅ |
|
02d589d to
e1a77f0
Compare
|
| Lint rule | Added | Removed | Changed |
|---|---|---|---|
invalid-argument-type |
7 | 23 | 59 |
not-subscriptable |
0 | 23 | 0 |
invalid-assignment |
0 | 9 | 1 |
possibly-missing-attribute |
2 | 6 | 1 |
unused-ignore-comment |
5 | 2 | 0 |
invalid-key |
3 | 0 | 3 |
call-non-callable |
0 | 5 | 0 |
invalid-return-type |
0 | 2 | 2 |
unresolved-attribute |
0 | 4 | 0 |
not-iterable |
0 | 2 | 0 |
index-out-of-bounds |
1 | 0 | 0 |
no-matching-overload |
0 | 1 | 0 |
unsupported-operator |
0 | 0 | 1 |
| Total | 18 | 77 | 67 |
| ```toml | ||
| [environment] | ||
| python-version = "3.12" | ||
| ``` | ||
|
|
||
| ```py | ||
| from typing import Protocol | ||
|
|
||
| class SupportsLessThan(Protocol): | ||
| def __lt__(self, other, /) -> bool: ... | ||
|
|
||
| def f[K: SupportsLessThan](dictionary: dict[K, int], key: K): | ||
| reveal_type(dictionary[key]) # revealed: int | ||
| ``` |
There was a problem hiding this comment.
the first version of this PR broke this case by recursing into TypeVar bounds/constraints too eagerly.
7305674 to
68bdf9f
Compare
|
The new ecosystem hit on homeassistant here is kind-of an epic true positive (though it will unfortunately be broken with some of our upcoming changes to get rid of Here's a minimal repro of what's going on with this branch: GLOBAL_CONSTANT = (1, 2, 3, 4, 5)
class Foo:
def __init__(self):
self.x = 0
def method(self):
reveal_type(self.x) # revealed: Unknown | Literal[0]
reveal_type(len(GLOBAL_CONSTANT)) # revealed: Literal[5]
index = min(self.x, len(GLOBAL_CONSTANT))
reveal_type(index) # revealed: Unknown | Literal[0, 5] (!!)
GLOBAL_CONSTANT[index] # error: [index-out-of-bounds] (tuple has length 5, index has type `Literal[5]`!!)But once we stop unioning with |
|
The new ecosystem hit on The same thing is going on with the new diagnostics on All other ecosystem changes are false-positive errors going away or diagnostics moving around/changing their message slightly. |
Summary
Fixes astral-sh/ty#2015. We weren't recursing into the value of a type alias when we should have been.
There are situations where we should also be recursing into the bounds/constraints of a typevar. I initially tried to do that as well in this PR, but that seems... trickier. For now I'm cutting scope; this PR does, however, add several failing tests for those cases.
Test Plan
added mdtests