From 3511b7a06bafffdf07180f07282e799d8e7e6c02 Mon Sep 17 00:00:00 2001 From: Shunsuke Shibayama <45118249+mtshiba@users.noreply.github.com> Date: Fri, 5 Dec 2025 11:05:41 +0900 Subject: [PATCH] [ty] do nothing with `store_expression_type` if `inner_expression_inference_state` is `Get` (#21718) ## Summary Fixes https://github.com/astral-sh/ty/issues/1688 ## Test Plan N/A --- .../resources/corpus/inner_expression_inference_state.py | 6 ++++++ crates/ty_python_semantic/src/types/infer/builder.rs | 5 ++++- 2 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 crates/ty_python_semantic/resources/corpus/inner_expression_inference_state.py 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 0000000000..dcf4bd462b --- /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 ccb25b66a2..d308553801 100644 --- a/crates/ty_python_semantic/src/types/infer/builder.rs +++ b/crates/ty_python_semantic/src/types/infer/builder.rs @@ -7108,10 +7108,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; }