[ty] Add diagnostic hint for invalid assignments involving invariant generics#24032
[ty] Add diagnostic hint for invalid assignments involving invariant generics#24032
Conversation
Typing conformance resultsNo changes detected ✅Current numbersThe percentage of diagnostics emitted that were expected errors held steady at 85.29%. The percentage of expected errors that received a diagnostic held steady at 78.13%. The number of fully passing files held steady at 64/132. |
87e40a0 to
fa11478
Compare
...nvalid_assignment_d…_-_Invalid_assignment_d…_-_Invariant_generic_cl…_(4083c269b4d4746f).snap
Show resolved
Hide resolved
Memory usage reportMemory usage unchanged ✅ |
| 11 | | ||
| 12 | def _(source: dict[bool, str]): | ||
| | | ||
| info: `dict` is invariant in its second type parameter |
There was a problem hiding this comment.
I guess we could also invest a bit more effort and refer to the name of the type parameter (_VT@dict in this case), if that seems helpful.
| | | ||
| info: `dict` is invariant in its first type parameter | ||
| info: For more information, see https://docs.astral.sh/ty/reference/typing-faq/#invariant-generics | ||
| info: rule `invalid-assignment` is enabled by default |
There was a problem hiding this comment.
Here, we test that we do not emit the "Consider using … collections.abc.Mapping" hint, because Mapping is still invariant in its key type, so that wouldn't help here.
|
| 41 | | ||
| 42 | def _(source: MutableSequence[bool]): | ||
| | | ||
| info: `Counter` is invariant in its type parameter |
There was a problem hiding this comment.
There is no covariant supertype suggestion for Counter because it's type parameter corresponds to a key-like parameter of dict.
| error[invalid-assignment]: Object of type `MutableSequence[bool]` is not assignable to `MutableSequence[int]` | ||
| --> src/mdtest_snippet.py:43:13 | ||
| | | ||
| 42 | def _(source: MutableSequence[bool]): | ||
| 43 | target: MutableSequence[int] = source # error: [invalid-assignment] | ||
| | -------------------- ^^^^^^ Incompatible value of type `MutableSequence[bool]` | ||
| | | | ||
| | Declared type | ||
| 44 | | ||
| 45 | def _(source: MutableSet[bool]): | ||
| | | ||
| info: `MutableSequence` is invariant in its type parameter | ||
| info: Consider using the covariant supertype `collections.abc.Sequence` | ||
| info: For more information, see https://docs.astral.sh/ty/reference/typing-faq/#invariant-generics | ||
| info: rule `invalid-assignment` is enabled by default | ||
|
|
There was a problem hiding this comment.
I wasn't sure about these. It seems a bit silly. If you explicitly annotated something with MutableSequence, you probably meant it?
| 63 | target: list[str] = source # error: [invalid-assignment] | ||
| | | ||
| info: `MyContainer` is invariant in its type parameter | ||
| info: For more information, see https://docs.astral.sh/ty/reference/typing-faq/#invariant-generics |
There was a problem hiding this comment.
I think it's fine to refer to that FAQ entry even for user types.
| error[invalid-assignment]: Object of type `list[int]` is not assignable to `list[str]` | ||
| --> src/mdtest_snippet.py:63:13 | ||
| | | ||
| 61 | target: MyContainer[int] = source # error: [invalid-assignment] | ||
| 62 | def _(source: list[int]): | ||
| 63 | target: list[str] = source # error: [invalid-assignment] | ||
| | --------- ^^^^^^ Incompatible value of type `list[int]` | ||
| | | | ||
| | Declared type | ||
| 64 | from collections.abc import Sequence | ||
| | | ||
| info: rule `invalid-assignment` is enabled by default |
There was a problem hiding this comment.
Note that there is no hint at all here. Something is more fundamentally wrong with your code if you're trying to assign a list[str] to a list[int]. And it's unlikely that we can help you by teaching you about invariant generics.
fa11478 to
1850d53
Compare
Summary
Relates to this FAQ entry. Consider
With this change, we now emit some
infohints in theinvalid-assignmentdiagnostic:Test Plan
New snapshot tests