Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,8 @@ class C(Generic[T]):
@overload
def __init__(self: "C[bytes]", x: bytes) -> None: ...
@overload
def __init__(self: "C[int]", x: bytes) -> None: ...
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe not worth a separate follow-up PR, but wouldn't we ideally have a case where the second argument is optional and so we can have multiple matching overloads, and show that we pick the first?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This gives us two matching overloads if you provide a bytes parameter, and we show that we pick the first (giving C[bytes] instead of C[int] or C[bytes | int]). Maybe deserves a comment, since the argument / specialization pattern across the overloads is subtle?

@overload
def __init__(self, x: int) -> None: ...
def __init__(self, x: str | bytes | int) -> None: ...

Expand All @@ -369,6 +371,10 @@ C[bytes]("string") # error: [no-matching-overload]
C[bytes](b"bytes")
C[bytes](12)

C[int]("string") # error: [no-matching-overload]
C[int](b"bytes")
C[int](12)

C[None]("string") # error: [no-matching-overload]
C[None](b"bytes") # error: [no-matching-overload]
C[None](12)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,8 @@ class C[T]:
@overload
def __init__(self: C[bytes], x: bytes) -> None: ...
@overload
def __init__(self: C[int], x: bytes) -> None: ...
@overload
def __init__(self, x: int) -> None: ...
def __init__(self, x: str | bytes | int) -> None: ...

Expand All @@ -305,6 +307,10 @@ C[bytes]("string") # error: [no-matching-overload]
C[bytes](b"bytes")
C[bytes](12)

C[int]("string") # error: [no-matching-overload]
C[int](b"bytes")
C[int](12)

C[None]("string") # error: [no-matching-overload]
C[None](b"bytes") # error: [no-matching-overload]
C[None](12)
Expand Down
23 changes: 8 additions & 15 deletions crates/ty_python_semantic/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4649,32 +4649,25 @@ impl<'db> Type<'db> {
}
}

fn combine_binding_specialization<'db>(
db: &'db dyn Db,
binding: &CallableBinding<'db>,
) -> Option<Specialization<'db>> {
binding
.matching_overloads()
.map(|(_, binding)| binding.inherited_specialization())
.reduce(|acc, specialization| {
combine_specializations(db, acc, specialization)
})
.flatten()
}

let new_specialization = new_call_outcome
.and_then(Result::ok)
.as_ref()
.and_then(Bindings::single_element)
.and_then(|binding| combine_binding_specialization(db, binding))
.into_iter()
.flat_map(CallableBinding::matching_overloads)
.next()
.and_then(|(_, binding)| binding.inherited_specialization())
Comment on lines -4669 to +4659
Copy link
Member

@dhruvmanila dhruvmanila May 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is my mistake as I didn't revert this part from #17618 and only reverted the intersection of return type, sorry! 🙈

.filter(|specialization| {
Some(specialization.generic_context(db)) == generic_context
});
let init_specialization = init_call_outcome
.and_then(Result::ok)
.as_ref()
.and_then(Bindings::single_element)
.and_then(|binding| combine_binding_specialization(db, binding))
.into_iter()
.flat_map(CallableBinding::matching_overloads)
.next()
.and_then(|(_, binding)| binding.inherited_specialization())
.filter(|specialization| {
Some(specialization.generic_context(db)) == generic_context
});
Expand Down
Loading