From 040aa7463bcdcd147b733e2cc537c8a16d78c8e6 Mon Sep 17 00:00:00 2001 From: Ibraheem Ahmed Date: Fri, 21 Nov 2025 17:05:32 -0500 Subject: [PATCH] [ty] Narrow type context during literal promotion in generic class constructors (#21574) ## Summary Resolves https://github.com/astral-sh/ty/issues/1603. --- .../resources/mdtest/literal_promotion.md | 5 ++++- crates/ty_python_semantic/src/types.rs | 7 ++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/crates/ty_python_semantic/resources/mdtest/literal_promotion.md b/crates/ty_python_semantic/resources/mdtest/literal_promotion.md index 03eb45efbe..eb79c44b6c 100644 --- a/crates/ty_python_semantic/resources/mdtest/literal_promotion.md +++ b/crates/ty_python_semantic/resources/mdtest/literal_promotion.md @@ -335,6 +335,9 @@ reveal_type(x19) # revealed: list[Literal[1]] x20: list[Literal[1]] | None = [1] reveal_type(x20) # revealed: list[Literal[1]] -x21: X[Literal[1]] | None = x(1) +x21: X[Literal[1]] | None = X(1) reveal_type(x21) # revealed: X[Literal[1]] + +x22: X[Literal[1]] | None = x(1) +reveal_type(x22) # revealed: X[Literal[1]] ``` diff --git a/crates/ty_python_semantic/src/types.rs b/crates/ty_python_semantic/src/types.rs index 5447ec84a6..faf1a1f491 100644 --- a/crates/ty_python_semantic/src/types.rs +++ b/crates/ty_python_semantic/src/types.rs @@ -3504,9 +3504,10 @@ impl<'db> Type<'db> { return; }; - let tcx_specialization = tcx - .annotation - .and_then(|tcx| tcx.specialization_of(db, class_literal)); + let tcx_specialization = tcx.annotation.and_then(|tcx| { + tcx.filter_union(db, |ty| ty.specialization_of(db, class_literal).is_some()) + .specialization_of(db, class_literal) + }); for (typevar, ty) in specialization .generic_context(db)