Skip to content

Commit b763bb9

Browse files
committed
prefer declared type for collection literals
1 parent 2bffef5 commit b763bb9

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

crates/ty_python_semantic/resources/mdtest/assignment/annotations.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ h: list[list[int]] = [[], [42]]
114114
reveal_type(h) # revealed: list[list[int]]
115115

116116
i: list[typing.Any] = [1, 2, "3", ([4],)]
117-
reveal_type(i) # revealed: list[Any | int | str | tuple[list[Unknown | int]]]
117+
reveal_type(i) # revealed: list[Any]
118118

119119
j: list[tuple[str | int, ...]] = [(1, 2), ("foo", "bar"), ()]
120120
reveal_type(j) # revealed: list[tuple[str | int, ...]]
@@ -123,7 +123,7 @@ k: list[tuple[list[int], ...]] = [([],), ([1, 2], [3, 4]), ([5], [6], [7])]
123123
reveal_type(k) # revealed: list[tuple[list[int], ...]]
124124

125125
l: tuple[list[int], *tuple[list[typing.Any], ...], list[str]] = ([1, 2, 3], [4, 5, 6], [7, 8, 9], ["10", "11", "12"])
126-
reveal_type(l) # revealed: tuple[list[int], list[Any | int], list[Any | int], list[str]]
126+
reveal_type(l) # revealed: tuple[list[int], list[Any], list[Any], list[str]]
127127

128128
type IntList = list[int]
129129

@@ -181,7 +181,7 @@ h: list[list[int]] | None = [[], [42]]
181181
reveal_type(h) # revealed: list[list[int]]
182182

183183
i: list[typing.Any] | None = [1, 2, "3", ([4],)]
184-
reveal_type(i) # revealed: list[Any | int | str | tuple[list[Unknown | int]]]
184+
reveal_type(i) # revealed: list[Any]
185185

186186
j: list[tuple[str | int, ...]] | None = [(1, 2), ("foo", "bar"), ()]
187187
reveal_type(j) # revealed: list[tuple[str | int, ...]]
@@ -277,7 +277,7 @@ reveal_type(k) # revealed: list[Literal[1, 2, 3]]
277277
type Y[T] = list[T]
278278

279279
l: Y[Y[Literal[1]]] = [[1]]
280-
reveal_type(l) # revealed: list[list[Literal[1]]]
280+
reveal_type(l) # revealed: list[Y[Literal[1]]]
281281

282282
m: list[tuple[Literal[1], Literal[2], Literal[3]]] = [(1, 2, 3)]
283283
reveal_type(m) # revealed: list[tuple[Literal[1], Literal[2], Literal[3]]]

crates/ty_python_semantic/src/types/display.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,15 @@ impl Display for DisplayRepresentation<'_> {
593593
.0
594594
.display_with(self.db, self.settings.clone())
595595
.fmt(f),
596-
Type::TypeAlias(alias) => f.write_str(alias.name(self.db)),
596+
Type::TypeAlias(alias) => {
597+
f.write_str(alias.name(self.db))?;
598+
match alias.specialization(self.db) {
599+
None => Ok(()),
600+
Some(specialization) => specialization
601+
.display_short(self.db, TupleSpecialization::No, self.settings.clone())
602+
.fmt(f),
603+
}
604+
}
597605
}
598606
}
599607
}

crates/ty_python_semantic/src/types/infer/builder.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6061,6 +6061,13 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
60616061

60626062
let inferred_elt_ty = self.get_or_infer_expression(elt, elt_tcx);
60636063

6064+
// Simplify the inference based on the declared type of the element.
6065+
if let Some(elt_tcx) = elt_tcx.annotation {
6066+
if inferred_elt_ty.is_assignable_to(self.db(), elt_tcx) {
6067+
continue;
6068+
}
6069+
}
6070+
60646071
// Convert any element literals to their promoted type form to avoid excessively large
60656072
// unions for large nested list literals, which the constraint solver struggles with.
60666073
let inferred_elt_ty = inferred_elt_ty.promote_literals(self.db(), elt_tcx);

0 commit comments

Comments
 (0)