diff --git a/crates/ty_python_semantic/resources/mdtest/assignment/annotations.md b/crates/ty_python_semantic/resources/mdtest/assignment/annotations.md index 3d5e75ab99..adf0de358d 100644 --- a/crates/ty_python_semantic/resources/mdtest/assignment/annotations.md +++ b/crates/ty_python_semantic/resources/mdtest/assignment/annotations.md @@ -114,7 +114,7 @@ h: list[list[int]] = [[], [42]] reveal_type(h) # revealed: list[list[int]] i: list[typing.Any] = [1, 2, "3", ([4],)] -reveal_type(i) # revealed: list[Any | int | str | tuple[list[Unknown | int]]] +reveal_type(i) # revealed: list[Any] j: list[tuple[str | int, ...]] = [(1, 2), ("foo", "bar"), ()] reveal_type(j) # revealed: list[tuple[str | int, ...]] @@ -123,7 +123,7 @@ k: list[tuple[list[int], ...]] = [([],), ([1, 2], [3, 4]), ([5], [6], [7])] reveal_type(k) # revealed: list[tuple[list[int], ...]] l: tuple[list[int], *tuple[list[typing.Any], ...], list[str]] = ([1, 2, 3], [4, 5, 6], [7, 8, 9], ["10", "11", "12"]) -reveal_type(l) # revealed: tuple[list[int], list[Any | int], list[Any | int], list[str]] +reveal_type(l) # revealed: tuple[list[int], list[Any], list[Any], list[str]] type IntList = list[int] @@ -187,7 +187,7 @@ h: list[list[int]] | None = [[], [42]] reveal_type(h) # revealed: list[list[int]] i: list[typing.Any] | None = [1, 2, "3", ([4],)] -reveal_type(i) # revealed: list[Any | int | str | tuple[list[Unknown | int]]] +reveal_type(i) # revealed: list[Any] j: list[tuple[str | int, ...]] | None = [(1, 2), ("foo", "bar"), ()] reveal_type(j) # revealed: list[tuple[str | int, ...]] @@ -196,7 +196,7 @@ k: list[tuple[list[int], ...]] | None = [([],), ([1, 2], [3, 4]), ([5], [6], [7] reveal_type(k) # revealed: list[tuple[list[int], ...]] l: tuple[list[int], *tuple[list[typing.Any], ...], list[str]] | None = ([1, 2, 3], [4, 5, 6], [7, 8, 9], ["10", "11", "12"]) -reveal_type(l) # revealed: tuple[list[int], list[Any | int], list[Any | int], list[str]] +reveal_type(l) # revealed: tuple[list[int], list[Any], list[Any], list[str]] type IntList = list[int] @@ -282,7 +282,7 @@ reveal_type(k) # revealed: list[Literal[1, 2, 3]] type Y[T] = list[T] l: Y[Y[Literal[1]]] = [[1]] -reveal_type(l) # revealed: list[list[Literal[1]]] +reveal_type(l) # revealed: list[Y[Literal[1]]] m: list[tuple[Literal[1], Literal[2], Literal[3]]] = [(1, 2, 3)] reveal_type(m) # revealed: list[tuple[Literal[1], Literal[2], Literal[3]]] diff --git a/crates/ty_python_semantic/src/types/display.rs b/crates/ty_python_semantic/src/types/display.rs index ed19a16358..73957453c9 100644 --- a/crates/ty_python_semantic/src/types/display.rs +++ b/crates/ty_python_semantic/src/types/display.rs @@ -591,7 +591,15 @@ impl Display for DisplayRepresentation<'_> { .0 .display_with(self.db, self.settings.clone()) .fmt(f), - Type::TypeAlias(alias) => f.write_str(alias.name(self.db)), + Type::TypeAlias(alias) => { + f.write_str(alias.name(self.db))?; + match alias.specialization(self.db) { + None => Ok(()), + Some(specialization) => specialization + .display_short(self.db, TupleSpecialization::No, self.settings.clone()) + .fmt(f), + } + } } } } diff --git a/crates/ty_python_semantic/src/types/infer/builder.rs b/crates/ty_python_semantic/src/types/infer/builder.rs index 34e2e89f71..5ebcfcda15 100644 --- a/crates/ty_python_semantic/src/types/infer/builder.rs +++ b/crates/ty_python_semantic/src/types/infer/builder.rs @@ -6179,6 +6179,13 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> { let inferred_elt_ty = self.get_or_infer_expression(elt, elt_tcx); + // Simplify the inference based on the declared type of the element. + if let Some(elt_tcx) = elt_tcx.annotation { + if inferred_elt_ty.is_assignable_to(self.db(), elt_tcx) { + continue; + } + } + // Convert any element literals to their promoted type form to avoid excessively large // unions for large nested list literals, which the constraint solver struggles with. let inferred_elt_ty = inferred_elt_ty.promote_literals(self.db(), elt_tcx);