Skip to content
Open
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
6 changes: 6 additions & 0 deletions pyrefly/lib/solver/solver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2645,6 +2645,9 @@ pub enum SubsetError {
/// A function without **kwargs is not assignable to a function with Unpack-ed TypedDict **kwargs
/// unless the TypedDict is closed.
OpenTypedDictKwargs(Name),
/// A bound method cannot be converted to a callable because there is no
/// receiver parameter to bind.
BoundMethodMissingSelf(Name),
// TODO(rechen): replace this with specific reasons
Other,
}
Expand Down Expand Up @@ -2683,6 +2686,9 @@ impl SubsetError {
SubsetError::OpenTypedDictKwargs(td) => Some(format!(
"Callable without `**kwargs` cannot be assigned to callable with `**kwargs: Unpack[{td}]`, because `{td}` is not closed and may have additional unknown keys"
)),
SubsetError::BoundMethodMissingSelf(function) => Some(format!(
"Function `{function}` is treated as a method when accessed from an instance, but its signature does not accept a bound `self` argument"
)),
SubsetError::Other => None,
}
}
Expand Down
26 changes: 18 additions & 8 deletions pyrefly/lib/solver/subset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1843,21 +1843,31 @@ impl<'a, Ans: LookupAnswer> Subset<'a, Ans> {
Ok(())
}
(Type::Overload(overload), want) => self.is_subset_overload(overload, want),
(Type::BoundMethod(method), Type::Callable(_) | Type::Function(_))
(Type::BoundMethod(method), Type::Callable(_) | Type::Function(_)) => {
if let Some(l_no_self) =
self.type_order.bind_boundmethod(method, &mut |got, want| {
self.is_subset_eq(got, want).is_ok()
}) =>
{
self.is_subset_eq(&l_no_self, want)
})
{
self.is_subset_eq(&l_no_self, want)
} else {
Err(SubsetError::BoundMethodMissingSelf(
method.func.metadata().kind.function_name().into_owned(),
))
}
}
(Type::Callable(_) | Type::Function(_), Type::BoundMethod(method))
(Type::Callable(_) | Type::Function(_), Type::BoundMethod(method)) => {
if let Some(u_no_self) =
self.type_order.bind_boundmethod(method, &mut |got, want| {
self.is_subset_eq(got, want).is_ok()
}) =>
{
self.is_subset_eq(got, &u_no_self)
})
{
self.is_subset_eq(got, &u_no_self)
} else {
Err(SubsetError::BoundMethodMissingSelf(
method.func.metadata().kind.function_name().into_owned(),
))
}
}
(Type::BoundMethod(l), Type::BoundMethod(u))
if let Some(l_no_self) = self
Expand Down
17 changes: 17 additions & 0 deletions pyrefly/lib/test/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,23 @@ def foo(x: Callable[[int], str], c: C, c2: C2, c3: C3):
"#,
);

testcase!(
test_classvar_callable_return_bound_method_error,
r#"
from typing import Callable, ClassVar

def integer_factory() -> int:
return 1

class A:
_factory: ClassVar[Callable[[], int]] = integer_factory

@property
def factory(self) -> Callable[[], int]:
return self._factory # E: Function `_factory` is treated as a method when accessed from an instance, but its signature does not accept a bound `self` argument
"#,
);

testcase!(
test_bound_classmethod_explicit_targs,
r#"
Expand Down
Loading