Skip to content

[ty] Support infer_variance for legacy TypeVar#24930

Merged
charliermarsh merged 2 commits into
mainfrom
charlie/infer-variance
Apr 29, 2026
Merged

[ty] Support infer_variance for legacy TypeVar#24930
charliermarsh merged 2 commits into
mainfrom
charlie/infer-variance

Conversation

@charliermarsh
Copy link
Copy Markdown
Member

@charliermarsh charliermarsh commented Apr 29, 2026

Summary

See: #24927 (comment).

@astral-sh-bot astral-sh-bot Bot added the ty Multi-file analysis & type inference label Apr 29, 2026
@astral-sh-bot
Copy link
Copy Markdown

astral-sh-bot Bot commented Apr 29, 2026

Typing conformance results improved 🎉

The percentage of diagnostics emitted that were expected errors increased from 88.79% to 89.34%. The percentage of expected errors that received a diagnostic increased from 84.63% to 84.82%. The number of fully passing files held steady at 84/134.

Summary

How are test cases classified?

Each test case represents one expected error annotation or a group of annotations sharing a tag. Counts are per test case, not per diagnostic — multiple diagnostics on the same line count as one. Required annotations (E) are true positives when ty flags the expected location and false negatives when it does not. Optional annotations (E?) are true positives when flagged but true negatives (not false negatives) when not. Tagged annotations (E[tag]) require ty to flag exactly one of the tagged lines; tagged multi-annotations (E[tag+]) allow any number up to the tag count. Flagging unexpected locations counts as a false positive.

Metric Old New Diff Outcome
True Positives 903 905 +2 ⏫ (✅)
False Positives 114 108 -6 ⏬ (✅)
False Negatives 164 162 -2 ⏬ (✅)
Total Diagnostics 1067 1063 -4
Precision 88.79% 89.34% +0.55% ⏫ (✅)
Recall 84.63% 84.82% +0.19% ⏫ (✅)
Passing Files 84/134 84/134 +0

Test file breakdown

1 file altered
File True Positives False Positives False Negatives Status
generics_syntax_infer_variance.py 18 (+2) ✅ 0 (-6) ✅ 1 (-2) ✅ 📈 Improving
Total (all files) 905 (+2) ✅ 108 (-6) ✅ 162 (-2) ✅ 84/134

True positives added (2)

2 diagnostics
Test case Diff

generics_syntax_infer_variance.py:15

+error[invalid-legacy-type-variable] A `TypeVar` cannot specify variance when `infer_variance=True`

generics_syntax_infer_variance.py:17

+error[invalid-legacy-type-variable] A `TypeVar` cannot specify variance when `infer_variance=True`

False positives removed (6)

6 diagnostics
Test case Diff

generics_syntax_infer_variance.py:166

-error[invalid-assignment] Object of type `ShouldBeContravariant1[int | float]` is not assignable to `ShouldBeContravariant1[int]`

generics_syntax_infer_variance.py:28

-error[invalid-assignment] Object of type `ShouldBeCovariant1[int]` is not assignable to `ShouldBeCovariant1[int | float]`

generics_syntax_infer_variance.py:46

-error[invalid-assignment] Object of type `ShouldBeCovariant2[int]` is not assignable to `ShouldBeCovariant2[int | float]`

generics_syntax_infer_variance.py:55

-error[invalid-assignment] Object of type `ShouldBeCovariant3[int]` is not assignable to `ShouldBeCovariant3[int | float]`

generics_syntax_infer_variance.py:84

-error[invalid-assignment] Object of type `ShouldBeCovariant5[int]` is not assignable to `ShouldBeCovariant5[int | float]`

generics_syntax_infer_variance.py:95

-error[invalid-assignment] Object of type `ShouldBeCovariant6[int]` is not assignable to `ShouldBeCovariant6[int | float]`

@astral-sh-bot
Copy link
Copy Markdown

astral-sh-bot Bot commented Apr 29, 2026

ecosystem-analyzer results

No diagnostic changes detected ✅

Full report with detailed diff (timing results)

@astral-sh-bot
Copy link
Copy Markdown

astral-sh-bot Bot commented Apr 29, 2026

Memory usage report

Summary

Project Old New Diff Outcome
prefect 676.59MB 676.87MB +0.04% (284.59kB)
flake8 47.78MB 47.78MB -
sphinx 260.02MB 260.02MB -
trio 117.07MB 117.07MB -

Significant changes

Click to expand detailed breakdown

prefect

Name Old New Diff Outcome
infer_expression_type_impl 7.41MB 7.46MB +0.74% (55.76kB)
infer_expression_types_impl 55.89MB 55.94MB +0.10% (55.63kB)
StaticClassLiteral<'db>::implicit_attribute_inner_ 6.58MB 6.62MB +0.57% (38.36kB)
infer_definition_types 85.73MB 85.77MB +0.04% (32.53kB)
Type<'db>::member_lookup_with_policy_ 15.77MB 15.80MB +0.19% (31.12kB)
Type<'db>::class_member_with_policy_ 16.85MB 16.88MB +0.16% (27.77kB)
StaticClassLiteral<'db>::variance_of_ 53.30kB 64.93kB +21.82% (11.63kB)
all_negative_narrowing_constraints_for_expression 1.93MB 1.94MB +0.42% (8.24kB)
all_narrowing_constraints_for_expression 5.99MB 6.00MB +0.13% (8.24kB)
infer_scope_types_impl 55.39MB 55.40MB +0.01% (5.74kB)
StaticClassLiteral<'db>::variance_of_::interned_arguments 47.88kB 50.84kB +6.17% (2.95kB)
Type<'db>::apply_specialization_ 3.61MB 3.62MB +0.06% (2.18kB)
GenericAlias<'db>::variance_of_ 40.72kB 42.29kB +3.86% (1.57kB)
GenericAlias<'db>::variance_of_::interned_arguments 34.03kB 35.30kB +3.72% (1.27kB)
when_constraint_set_assignable_to_owned_impl 1.53MB 1.53MB -0.05% (736.00B)
... 16 more

@charliermarsh charliermarsh force-pushed the charlie/infer-variance branch from 0ec17c7 to 9a95778 Compare April 29, 2026 15:50
@charliermarsh charliermarsh marked this pull request as ready for review April 29, 2026 15:50
@AlexWaygood
Copy link
Copy Markdown
Member

This is obviously a huge improvement as-is, but do we know what the last remaining false negative is here?

image

@charliermarsh
Copy link
Copy Markdown
Member Author

Oh I didn't notice it was just one more! I'll look.

@charliermarsh
Copy link
Copy Markdown
Member Author

Oh no!

# vo4_4: ShouldBeCovariant4[int] = ShouldBeCovariant4[float](1.0)  # E

@charliermarsh
Copy link
Copy Markdown
Member Author

(I'll put up a separate PR for that after confirming.)

Comment thread crates/ty_python_semantic/resources/mdtest/generics/legacy/variables.md Outdated
@charliermarsh charliermarsh force-pushed the charlie/infer-variance branch from 11c6d62 to 11f5e2d Compare April 29, 2026 16:43
@astral-sh-bot
Copy link
Copy Markdown

astral-sh-bot Bot commented Apr 29, 2026

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

Formatter (stable)

✅ ecosystem check detected no format changes.

Formatter (preview)

✅ ecosystem check detected no format changes.

@charliermarsh charliermarsh merged commit e652850 into main Apr 29, 2026
57 checks passed
@charliermarsh charliermarsh deleted the charlie/infer-variance branch April 29, 2026 16:49
thejchap pushed a commit to thejchap/ruff that referenced this pull request May 23, 2026
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.

3 participants