Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[ty] Add help message to invalid-generic-class variance diagnostic#25385

Merged
sharkdp merged 5 commits into
astral-sh:mainfrom
Dev-X25874:feat/variance-diagnostic-detail
May 27, 2026
Merged

[ty] Add help message to invalid-generic-class variance diagnostic#25385
sharkdp merged 5 commits into
astral-sh:mainfrom
Dev-X25874:feat/variance-diagnostic-detail

Conversation

@Dev-X25874

Copy link
Copy Markdown
Contributor

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

Changes:

  • Added TypeVarVariance::as_str() in variance.rs to produce
    human-readable variance names ("covariant", "contravariant",
    "invariant")
  • Refactored the .find() closure in static_class.rs to
    .find_map() so that both the declared and required variance values
    are available when building the diagnostic
  • Added a .help() message to the diagnostic using those values

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.

@astral-sh-bot astral-sh-bot Bot added the ty Multi-file analysis & type inference label May 26, 2026
@astral-sh-bot

astral-sh-bot Bot commented May 26, 2026

Copy link
Copy Markdown

Typing conformance results

No changes detected ✅

Current numbers
The 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.

@astral-sh-bot

astral-sh-bot Bot commented May 26, 2026

Copy link
Copy Markdown

Memory usage report

Memory usage unchanged ✅

@astral-sh-bot

astral-sh-bot Bot commented May 26, 2026

Copy link
Copy Markdown

ecosystem-analyzer results

No diagnostic changes detected ✅

Full report with detailed diff (timing results)

@Dev-X25874

Copy link
Copy Markdown
Contributor Author

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 sharkdp left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you. I made a small modification to the tests, so that we can see the new help messages.

Comment on lines -383 to -384
static_assert(not is_assignable_to(GoodInferredInvariant[B], GoodInferredInvariant[A]))
static_assert(not is_assignable_to(GoodInferredInvariant[A], GoodInferredInvariant[B]))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did these get removed?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(fixed)

@sharkdp sharkdp enabled auto-merge (squash) May 27, 2026 12:21
@sharkdp sharkdp merged commit dfb5e92 into astral-sh:main May 27, 2026
58 of 59 checks passed
@Dev-X25874

Copy link
Copy Markdown
Contributor Author

Thank you for the review and for fixing up the formatting issues caused by the GitHub web editor!
Really appreciate you taking the time to restore the static_assert lines and cleaning up the
mdformat issue. Looking forward to contributing more to ruff! 🙏

@Dev-X25874 Dev-X25874 deleted the feat/variance-diagnostic-detail branch May 27, 2026 14:32
anishgirianish pushed a commit to anishgirianish/ruff that referenced this pull request May 28, 2026
…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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants