Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# This is a regression test for `store_expression_type`.
# ref: https://github.com/astral-sh/ty/issues/1688

x: int

type x[T] = x[T, U]
5 changes: 4 additions & 1 deletion crates/ty_python_semantic/src/types/infer/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7093,10 +7093,13 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {

#[track_caller]
fn store_expression_type(&mut self, expression: &ast::Expr, ty: Type<'db>) {
if self.deferred_state.in_string_annotation() {
if self.deferred_state.in_string_annotation()
|| self.inner_expression_inference_state.is_get()
Copy link
Member

Choose a reason for hiding this comment

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

Could we use multi_inference_state::Ignore instead of having two modes that represent "read-only" type inference?

Copy link
Contributor

Choose a reason for hiding this comment

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

drive-by context: I am likely to modify this check for in_string_annotation to do the "store (Expr, Expr) for string annotation types" thing we've been discussing.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Could we use multi_inference_state::Ignore instead of having two modes that represent "read-only" type inference?

I understand that MultiInferenceState::Ignore is an option to perform the same calculation twice and discard the second result.
Since all type inference for expressions is skipped while in InnerExpressionInferenceState::Get, I think it should be used whenever possible (for example, when only diagnostics are needed, such as the handling in infer_subscript_type_expression for union types).
But I'm not sure whether this can be assumed in all cases where MultiInferenceState::Ignore is used.

{
// Avoid storing the type of expressions that are part of a string annotation because
// the expression ids don't exists in the semantic index. Instead, we'll store the type
// on the string expression itself that represents the annotation.
// Also, if `inner_expression_inference_state` is `Get`, the expression type has already been stored.
return;
}

Expand Down
Loading