[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
* main: [`flake8-bandit`] Check tuple arguments for partial paths in `S607` (#24080) [ty] Update Salsa (#24081) Update Rust toolchain to 1.94 and MSRV to 1.92 (#24076) [ty] Move ruffen-docs formatting config to a `ruff.toml` config file (#24074) [ty] `reveal_type` diagnostics in unreachable code (#24070) [ty] Improve keyword argument narrowing for nested dictionaries (#24010) [ty] Preserve blank lines between comments and imports in add-import action (#24066) [ty] Add diagnostic hint for invalid assignments involving invariant generics (#24032) Clarify `extend-ignore` and `extend-select` settings documentation (#24064) [ty] Batch changes to watched paths (#24045) replace deprecated `std::f64::EPSILON` with `f64::EPSILON` (#24067) [ty] Fix untracked reads in Salsa queries that can lead to backdating panics (#24051) [ty] Unions/intersections of gradual types should be assignable to `Never` (#24056) Fix incorrect path for ty_python_semantic in fuzzer (#24052) Bump 0.15.7 (#24049) [ty] ecosystem-analyzer: Fail on newly panicking projects (#24043) Don't show noqa hover for non-Python documents (#24040)
Summary
Relates to this FAQ entry. Consider
With this change, we now emit some
infohints in theinvalid-assignmentdiagnostic:Test Plan
New snapshot tests