[ty] Add help message to invalid-generic-class variance diagnostic#25385
Conversation
Typing conformance resultsNo changes detected ✅Current numbersThe percentage of diagnostics emitted that were expected errors held steady at 89.70%. The percentage of expected errors that received a diagnostic held steady at 86.99%. The number of fully passing files held steady at 91/134. |
Memory usage reportMemory usage unchanged ✅ |
|
|
The only remaining failure is a mdformat blank line issue caused by GitHub's web editor mangling the formatting. Happy for a maintainer to fix it directly or I can push a local fix if needed. |
sharkdp
left a comment
There was a problem hiding this comment.
Thank you. I made a small modification to the tests, so that we can see the new help messages.
| static_assert(not is_assignable_to(GoodInferredInvariant[B], GoodInferredInvariant[A])) | ||
| static_assert(not is_assignable_to(GoodInferredInvariant[A], GoodInferredInvariant[B])) |
There was a problem hiding this comment.
Why did these get removed?
|
Thank you for the review and for fixing up the formatting issues caused by the GitHub web editor! |
…stral-sh#25385) ## Summary When a generic subclass uses an explicitly-declared TypeVar in a base class position where the variance is incompatible, the existing diagnostic only reported which type variable and base class were involved: Variance of type variable `T_co` is incompatible with base class `Invariant` This adds a `.help()` sub-diagnostic that explains *why* it is incompatible and what the base class actually requires: Type variable `T_co` is declared as covariant, but base class `Invariant` requires it to be invariant ## Test Plan Added a new mdtest section in `crates/ty_python_semantic/resources/mdtest/generics/legacy/variance.md` covering all four incompatible variance combinations, verifying the exact diagnostic message text. --------- Co-authored-by: David Peter <[email protected]>
Summary
When a generic subclass uses an explicitly-declared TypeVar in a base
class position where the variance is incompatible, the existing
diagnostic only reported which type variable and base class were
involved:
Variance of type variable
T_cois incompatible with base classInvariantThis adds a
.help()sub-diagnostic that explains why it isincompatible and what the base class actually requires:
Type variable
T_cois declared as covariant, but base classInvariantrequires it to be invariantChanges:
TypeVarVariance::as_str()invariance.rsto producehuman-readable variance names (
"covariant","contravariant","invariant").find()closure instatic_class.rsto.find_map()so that both the declared and required variance valuesare available when building the diagnostic
.help()message to the diagnostic using those valuesTest Plan
Added a new mdtest section in
crates/ty_python_semantic/resources/mdtest/generics/legacy/variance.mdcovering all four incompatible variance combinations, verifying the
exact diagnostic message text.