Skip to content

Commit 5bd7788

Browse files
committed
nits
1 parent fe942dc commit 5bd7788

File tree

2 files changed

+22
-17
lines changed

2 files changed

+22
-17
lines changed

crates/ty_python_semantic/src/types.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1202,16 +1202,24 @@ impl<'db> Type<'db> {
12021202
if yes { self.negate(db) } else { *self }
12031203
}
12041204

1205-
/// Filters union elements based on the provided predicate.
1206-
pub(crate) fn filter(self, db: &'db dyn Db, f: impl FnMut(&Type<'db>) -> bool) -> Type<'db> {
1205+
/// If the type is a union, filters union elements based on the provided predicate.
1206+
///
1207+
/// Otherwise, returns the type unchanged.
1208+
pub(crate) fn filter_union(
1209+
self,
1210+
db: &'db dyn Db,
1211+
f: impl FnMut(&Type<'db>) -> bool,
1212+
) -> Type<'db> {
12071213
if let Type::Union(union) = self {
12081214
union.filter(db, f)
12091215
} else {
12101216
self
12111217
}
12121218
}
12131219

1214-
/// Remove the union elements that are not related to `target`.
1220+
/// If the type is a union, removes union elements that are disjoint from `target`.
1221+
///
1222+
/// Otherwise, returns the type unchanged.
12151223
pub(crate) fn filter_disjoint_elements(
12161224
self,
12171225
db: &'db dyn Db,

crates/ty_python_semantic/src/types/call/bind.rs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2582,28 +2582,25 @@ impl<'a, 'db> ArgumentTypeChecker<'a, 'db> {
25822582
}
25832583
}
25842584

2585-
// Build the specialization once without type context.
2585+
// Build the specialization once without inferring the type context.
25862586
let isolated_specialization = builder.build(generic_context, *self.call_expression_tcx);
25872587
let isolated_return_ty = self
25882588
.return_ty
25892589
.apply_specialization(self.db, isolated_specialization);
25902590

25912591
// TODO: Ideally we would infer the annotated type _before_ the arguments if this call is part of an
25922592
// annotated assignment, to closer match the order of any unions written in the type annotation.
2593-
if let Some(call_expression_tcx) = self.call_expression_tcx.annotation {
2594-
match call_expression_tcx {
2595-
// A type variable is not a useful type-context for expression inference, and applying it
2596-
// to the return type can lead to confusing unions in nested generic calls.
2597-
Type::TypeVar(_) => {}
2598-
2599-
_ => {
2600-
// Ignore any specialization errors here, because the type context is only used as a hint
2601-
// to infer a more assignable return type.
2602-
let _ = builder.infer(self.return_ty, call_expression_tcx);
2603-
}
2604-
}
2593+
//
2594+
// A type variable is not a useful type-context for expression inference, and applying it
2595+
// to the return type can lead to confusing unions in nested generic calls.
2596+
if let Some(call_expression_tcx) = self.call_expression_tcx.annotation
2597+
&& !call_expression_tcx.is_type_var()
2598+
{
2599+
// Ignore any specialization errors here, because the type context is only used as a hint
2600+
// to infer a more assignable return type.
2601+
let _ = builder.infer(self.return_ty, call_expression_tcx);
26052602

2606-
// Build the specialization a second time with the type context.
2603+
// Build the specialization a second time after inferring the type context.
26072604
let specialization = builder.build(generic_context, *self.call_expression_tcx);
26082605
let return_ty = self.return_ty.apply_specialization(self.db, specialization);
26092606

0 commit comments

Comments
 (0)