diff --git a/crates/ty_python_semantic/resources/corpus/inner_expression_inference_state.py b/crates/ty_python_semantic/resources/corpus/inner_expression_inference_state.py new file mode 100644 index 0000000000000..dcf4bd462b604 --- /dev/null +++ b/crates/ty_python_semantic/resources/corpus/inner_expression_inference_state.py @@ -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] diff --git a/crates/ty_python_semantic/src/types/infer/builder.rs b/crates/ty_python_semantic/src/types/infer/builder.rs index 4c97ad8c60628..3079245c3ed6c 100644 --- a/crates/ty_python_semantic/src/types/infer/builder.rs +++ b/crates/ty_python_semantic/src/types/infer/builder.rs @@ -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() + { // 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; }