Skip to content

[ty] impl VarianceInferable for KnownInstanceType#20924

Merged
sharkdp merged 2 commits intoastral-sh:mainfrom
mtshiba:type-alias-variance
Oct 17, 2025
Merged

[ty] impl VarianceInferable for KnownInstanceType#20924
sharkdp merged 2 commits intoastral-sh:mainfrom
mtshiba:type-alias-variance

Conversation

@mtshiba
Copy link
Collaborator

@mtshiba mtshiba commented Oct 16, 2025

Summary

Derived from #20900

Implement VarianceInferable for KnownInstanceType (especially for KnownInstanceType::TypeAliasType).

The variance of a type alias matches its value type. In normal usage, type aliases are expanded to value types, so the variance of a type alias can be obtained without implementing this. However, for example, if we want to display the variance when hovering over a type alias, we need to be able to obtain the variance of the type alias itself (cf. #20900).

Test Plan

I couldn't come up with a way to test this in mdtest, so I'm testing it in a test submodule at the end of types.rs.
I also added a test to mdtest/generics/pep695/variance.md, but it passes without the changes in this PR.

@github-actions
Copy link
Contributor

github-actions bot commented Oct 16, 2025

Diagnostic diff on typing conformance tests

No changes detected when running ty on typing conformance tests ✅

@github-actions
Copy link
Contributor

github-actions bot commented Oct 16, 2025

mypy_primer results

No ecosystem changes detected ✅
No memory usage changes detected ✅

@mtshiba mtshiba marked this pull request as ready for review October 16, 2025 17:14
@AlexWaygood AlexWaygood added the ty Multi-file analysis & type inference label Oct 16, 2025
}
}

pub(crate) fn raw_value_type(self, db: &'db dyn Db) -> Type<'db> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we please document what raw_value_type does compared to value_type?

More importantly, value_type has fixpoint handling, but raw_value_type does not. Does this lead to problems with recursive type aliases?

Copy link
Collaborator Author

@mtshiba mtshiba Oct 17, 2025

Choose a reason for hiding this comment

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

raw_value_type is called by KnownInstanceType::variance_of except within the queried value_type.

As already explained, KnownInstanceType::variance_of is not normally called. In current usage, the only time it may be called is during a hover request.
Clearly, the type inference functions that raw_value_type depends on never call a hover request. Therefore, in current usage, there is no possibility of infinite recursion.

For safety, I added a note that raw_value_type is not tracked, so that anyone using it in the future knows to be careful.

Copy link
Contributor

@sharkdp sharkdp left a comment

Choose a reason for hiding this comment

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

Thank you for the update and the explanations!

@sharkdp sharkdp merged commit e4384fc into astral-sh:main Oct 17, 2025
41 checks passed
@mtshiba mtshiba deleted the type-alias-variance branch October 19, 2025 04:07
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