Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -1166,6 +1166,47 @@ static_assert(is_subtype_of(TypeOf[C], Callable[[int], int]))
static_assert(is_subtype_of(TypeOf[C], Callable[[], str]))
```

#### Classes with `__new__`

```py
from typing import Callable
from knot_extensions import TypeOf, static_assert, is_subtype_of

class A:
def __new__(cls, a: int) -> int:
return a

static_assert(is_subtype_of(TypeOf[A], Callable[[int], int]))
static_assert(not is_subtype_of(TypeOf[A], Callable[[], int]))

class B: ...
class C(B): ...

class D:
def __new__(cls) -> B:
return B()

class E(D):
def __new__(cls) -> C:
return C()

static_assert(is_subtype_of(TypeOf[E], Callable[[], C]))
static_assert(is_subtype_of(TypeOf[E], Callable[[], B]))
static_assert(not is_subtype_of(TypeOf[D], Callable[[], C]))
static_assert(is_subtype_of(TypeOf[D], Callable[[], B]))

Comment thread
MatthewMckee4 marked this conversation as resolved.
class MetaWithIntReturn(type):
def __call__(cls) -> int:
return super().__call__()

class F(metaclass=MetaWithIntReturn):
def __new__(cls) -> str:
return super().__new__(cls)

static_assert(is_subtype_of(TypeOf[F], Callable[[], int]))
static_assert(not is_subtype_of(TypeOf[F], Callable[[], str]))
```

### Bound methods

```py
Expand Down
24 changes: 24 additions & 0 deletions crates/red_knot_python_semantic/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1152,6 +1152,21 @@ impl<'db> Type<'db> {
let new_function = new_function.into_callable_type(db);
return new_function.is_subtype_of(db, target);
}

let new_function_symbol = self
.member_lookup_with_policy(
db,
"__new__".into(),
MemberLookupPolicy::MRO_NO_OBJECT_FALLBACK
| MemberLookupPolicy::META_CLASS_NO_TYPE_FALLBACK,
)
.symbol;

if let Symbol::Type(Type::FunctionLiteral(new_function), _) = new_function_symbol {
let new_function = new_function.into_bound_method_type(db, self);
return new_function.is_subtype_of(db, target);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nit:

Suggested change
let new_function = new_function.into_bound_method_type(db, self);
return new_function.is_subtype_of(db, target);
return new_function.into_bound_method_type(db, self).is_subtype_of(db, target);

}

Comment thread
MatthewMckee4 marked this conversation as resolved.
Outdated
false
}

Expand Down Expand Up @@ -5904,6 +5919,15 @@ impl<'db> FunctionType<'db> {
))
}

/// Convert the `FunctionType` into a [`Type::BoundMethod`].
pub(crate) fn into_bound_method_type(
Comment thread
MatthewMckee4 marked this conversation as resolved.
self,
db: &'db dyn Db,
self_instance: Type<'db>,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The parameter name is a bit misleading because in this PR above where this is being called it's not an instance of the class but the class itself. This is also the case for the field name on BoundMethodType which is also called self_instance. I'm not sure what to name it, maybe self_or_cls (and remove the _instance suffix).

Regardless, this shouldn't block this PR and something that could be done in a follow-up instead.

) -> Type<'db> {
Type::BoundMethod(BoundMethodType::new(db, self, self_instance))
}

/// Returns the [`FileRange`] of the function's name.
pub fn focus_range(self, db: &dyn Db) -> FileRange {
FileRange::new(
Expand Down