[ty] Use UnionType helper methods more consistently (#22357)

This commit is contained in:
Alex Waygood
2026-01-03 14:19:06 +00:00
committed by GitHub
parent fd86e699b5
commit e1439beab2
3 changed files with 6 additions and 20 deletions

View File

@@ -7267,10 +7267,7 @@ impl<'db> Type<'db> {
}
(Some(Place::Defined(new_method, ..)), Place::Defined(init_method, ..)) => {
let callable = UnionBuilder::new(db)
.add(*new_method)
.add(*init_method)
.build();
let callable = UnionType::from_elements(db, [new_method, init_method]);
let new_method_bindings = new_method
.bindings(db)
@@ -10758,11 +10755,7 @@ fn walk_type_var_constraints<'db, V: visitor::TypeVisitor<'db> + ?Sized>(
impl<'db> TypeVarConstraints<'db> {
fn as_type(self, db: &'db dyn Db) -> Type<'db> {
let mut builder = UnionBuilder::new(db);
for ty in self.elements(db) {
builder = builder.add(*ty);
}
builder.build()
UnionType::from_elements(db, self.elements(db))
}
fn to_instance(self, db: &'db dyn Db) -> Option<TypeVarConstraints<'db>> {

View File

@@ -1083,13 +1083,9 @@ impl<'db> TypeInferenceBuilder<'db, '_> {
&mut self.inner_expression_inference_state,
InnerExpressionInferenceState::Get,
);
let union = union
.elements(self.db())
.iter()
.fold(UnionBuilder::new(self.db()), |builder, elem| {
builder.add(self.infer_subscript_type_expression(subscript, *elem))
})
.build();
let union = union.map(self.db(), |element| {
self.infer_subscript_type_expression(subscript, *element)
});
self.inner_expression_inference_state = previous_slice_inference_state;
union
}

View File

@@ -926,10 +926,7 @@ impl<'db, 'ast> NarrowingConstraintsBuilder<'db, 'ast> {
.build();
// Keep order: first literal complement, then broader arms.
let result = UnionBuilder::new(self.db)
.add(narrowed_single)
.add(rest_union)
.build();
let result = UnionType::from_elements(self.db, [narrowed_single, rest_union]);
Some(result)
} else {
None