Skip to content

Commit 504a19d

Browse files
committed
Change the return type for ambiguous matching
1 parent 1f11123 commit 504a19d

File tree

1 file changed

+27
-22
lines changed
  • crates/ty_python_semantic/src/types/call

1 file changed

+27
-22
lines changed

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

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ use crate::types::function::{DataclassTransformerParams, FunctionDecorators, Kno
2323
use crate::types::generics::{Specialization, SpecializationBuilder, SpecializationError};
2424
use crate::types::signatures::{Parameter, ParameterForm};
2525
use crate::types::{
26-
BoundMethodType, ClassLiteral, DataclassParams, KnownClass, KnownInstanceType,
27-
MethodWrapperKind, PropertyInstanceType, SpecialFormType, TupleType, TypeMapping, UnionType,
28-
WrapperDescriptorKind, ide_support, todo_type,
26+
BoundMethodType, ClassLiteral, DataclassParams, IntersectionBuilder, KnownClass,
27+
KnownInstanceType, MethodWrapperKind, PropertyInstanceType, SpecialFormType, TupleType,
28+
TypeMapping, UnionType, WrapperDescriptorKind, ide_support, todo_type,
2929
};
3030
use ruff_db::diagnostic::{Annotation, Diagnostic, Severity, SubDiagnostic};
3131
use ruff_python_ast as ast;
@@ -1328,9 +1328,9 @@ impl<'db> CallableBinding<'db> {
13281328
) {
13291329
let top_materialized_argument_type = TupleType::from_elements(
13301330
db,
1331-
argument_types.iter().map(|argument_type| {
1332-
argument_type.top_materialization(db, TypeVarVariance::Covariant)
1333-
}),
1331+
argument_types
1332+
.iter()
1333+
.map(|argument_type| argument_type.top_materialization(db)),
13341334
);
13351335

13361336
// A flag to indicate whether we've found the overload that makes the remaining overloads
@@ -1351,11 +1351,12 @@ impl<'db> CallableBinding<'db> {
13511351
// There is no parameter for this argument in this overload.
13521352
continue;
13531353
};
1354-
union.push(
1355-
overload.signature.parameters()[parameter_index]
1356-
.annotated_type()
1357-
.unwrap_or(Type::unknown()),
1358-
);
1354+
// TODO: For an unannotated `self` parameter, the type should be `typing.Self`
1355+
// while for other unannotated parameters, the type should be `Unknown`
1356+
let ty = overload.signature.parameters()[parameter_index]
1357+
.annotated_type()
1358+
.unwrap_or(Type::unknown());
1359+
union.push(ty);
13591360
}
13601361
if union.is_empty() {
13611362
continue;
@@ -1394,10 +1395,18 @@ impl<'db> CallableBinding<'db> {
13941395

13951396
if !are_return_types_equivalent_for_all_matching_overloads {
13961397
// Overload matching is ambiguous.
1397-
for (_, overload) in self.matching_overloads_mut() {
1398-
overload.mark_as_unmatched_overload();
1399-
}
1400-
self.overload_call_return_type = Some(OverloadCallReturnType::Ambiguous);
1398+
self.overload_call_return_type = Some(OverloadCallReturnType::Ambiguous(
1399+
IntersectionBuilder::new(db)
1400+
.positive_elements([
1401+
Type::any(),
1402+
UnionType::from_elements(
1403+
db,
1404+
self.matching_overloads()
1405+
.map(|(_, overload)| overload.return_type()),
1406+
),
1407+
])
1408+
.build(),
1409+
));
14011410
}
14021411
}
14031412

@@ -1477,8 +1486,8 @@ impl<'db> CallableBinding<'db> {
14771486
pub(crate) fn return_type(&self) -> Type<'db> {
14781487
if let Some(overload_call_return_type) = self.overload_call_return_type {
14791488
return match overload_call_return_type {
1480-
OverloadCallReturnType::ArgumentTypeExpansion(return_type) => return_type,
1481-
OverloadCallReturnType::Ambiguous => Type::any(),
1489+
OverloadCallReturnType::ArgumentTypeExpansion(return_type)
1490+
| OverloadCallReturnType::Ambiguous(return_type) => return_type,
14821491
};
14831492
}
14841493
if let Some((_, first_overload)) = self.matching_overloads().next() {
@@ -1522,10 +1531,6 @@ impl<'db> CallableBinding<'db> {
15221531
return;
15231532
}
15241533

1525-
if self.overload_call_return_type.is_some() {
1526-
return;
1527-
}
1528-
15291534
match self.overloads.as_slice() {
15301535
[] => {}
15311536
[overload] => {
@@ -1636,7 +1641,7 @@ impl<'a, 'db> IntoIterator for &'a CallableBinding<'db> {
16361641
#[derive(Debug, Copy, Clone)]
16371642
enum OverloadCallReturnType<'db> {
16381643
ArgumentTypeExpansion(Type<'db>),
1639-
Ambiguous,
1644+
Ambiguous(Type<'db>),
16401645
}
16411646

16421647
#[derive(Debug)]

0 commit comments

Comments
 (0)