mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-29 05:14:52 +00:00
[ty] Fix CallableTypeOf[…]
for bound methods (#20338)
Some checks are pending
CI / Determine changes (push) Waiting to run
CI / cargo fmt (push) Waiting to run
CI / cargo clippy (push) Blocked by required conditions
CI / cargo test (linux) (push) Blocked by required conditions
CI / cargo test (linux, release) (push) Blocked by required conditions
CI / cargo test (windows) (push) Blocked by required conditions
CI / cargo test (wasm) (push) Blocked by required conditions
CI / cargo build (release) (push) Waiting to run
CI / mkdocs (push) Waiting to run
CI / cargo build (msrv) (push) Blocked by required conditions
CI / cargo fuzz build (push) Blocked by required conditions
CI / fuzz parser (push) Blocked by required conditions
CI / test scripts (push) Blocked by required conditions
CI / ecosystem (push) Blocked by required conditions
CI / Fuzz for new ty panics (push) Blocked by required conditions
CI / cargo shear (push) Blocked by required conditions
CI / python package (push) Waiting to run
CI / pre-commit (push) Waiting to run
CI / formatter instabilities and black similarity (push) Blocked by required conditions
CI / test ruff-lsp (push) Blocked by required conditions
CI / check playground (push) Blocked by required conditions
CI / benchmarks-instrumented (push) Blocked by required conditions
CI / benchmarks-walltime (push) Blocked by required conditions
[ty Playground] Release / publish (push) Waiting to run
Some checks are pending
CI / Determine changes (push) Waiting to run
CI / cargo fmt (push) Waiting to run
CI / cargo clippy (push) Blocked by required conditions
CI / cargo test (linux) (push) Blocked by required conditions
CI / cargo test (linux, release) (push) Blocked by required conditions
CI / cargo test (windows) (push) Blocked by required conditions
CI / cargo test (wasm) (push) Blocked by required conditions
CI / cargo build (release) (push) Waiting to run
CI / mkdocs (push) Waiting to run
CI / cargo build (msrv) (push) Blocked by required conditions
CI / cargo fuzz build (push) Blocked by required conditions
CI / fuzz parser (push) Blocked by required conditions
CI / test scripts (push) Blocked by required conditions
CI / ecosystem (push) Blocked by required conditions
CI / Fuzz for new ty panics (push) Blocked by required conditions
CI / cargo shear (push) Blocked by required conditions
CI / python package (push) Waiting to run
CI / pre-commit (push) Waiting to run
CI / formatter instabilities and black similarity (push) Blocked by required conditions
CI / test ruff-lsp (push) Blocked by required conditions
CI / check playground (push) Blocked by required conditions
CI / benchmarks-instrumented (push) Blocked by required conditions
CI / benchmarks-walltime (push) Blocked by required conditions
[ty Playground] Release / publish (push) Waiting to run
## Summary `CallableTypeOf[bound_method]` would previously bind `self` to the bound method type itself, instead of binding it to the instance type stored inside the bound method type. ## Test Plan Added regression test
This commit is contained in:
parent
8a0edf0da8
commit
cde5e4e343
3 changed files with 17 additions and 4 deletions
|
@ -469,6 +469,8 @@ c4: CallableTypeOf[()]
|
|||
Using it in annotation to reveal the signature of the callable object:
|
||||
|
||||
```py
|
||||
from typing_extensions import Self
|
||||
|
||||
class Foo:
|
||||
def __init__(self, x: int) -> None:
|
||||
pass
|
||||
|
@ -476,12 +478,16 @@ class Foo:
|
|||
def __call__(self, x: int) -> str:
|
||||
return "foo"
|
||||
|
||||
def returns_self(self, x: int) -> Self:
|
||||
return self
|
||||
|
||||
def _(
|
||||
c1: CallableTypeOf[f1],
|
||||
c2: CallableTypeOf[f2],
|
||||
c3: CallableTypeOf[f3],
|
||||
c4: CallableTypeOf[Foo],
|
||||
c5: CallableTypeOf[Foo(42).__call__],
|
||||
c6: CallableTypeOf[Foo(42).returns_self],
|
||||
) -> None:
|
||||
reveal_type(c1) # revealed: () -> Unknown
|
||||
reveal_type(c2) # revealed: () -> int
|
||||
|
@ -491,4 +497,6 @@ def _(
|
|||
reveal_type(c4) # revealed: (...) -> Foo
|
||||
|
||||
reveal_type(c5) # revealed: (x: int) -> str
|
||||
|
||||
reveal_type(c6) # revealed: (x: int) -> Foo
|
||||
```
|
||||
|
|
|
@ -1046,8 +1046,11 @@ impl<'db> Type<'db> {
|
|||
matches!(self, Type::FunctionLiteral(..))
|
||||
}
|
||||
|
||||
pub(crate) const fn is_bound_method(&self) -> bool {
|
||||
matches!(self, Type::BoundMethod(..))
|
||||
pub(crate) const fn into_bound_method(self) -> Option<BoundMethodType<'db>> {
|
||||
match self {
|
||||
Type::BoundMethod(bound_method) => Some(bound_method),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn is_union_of_single_valued(&self, db: &'db dyn Db) -> bool {
|
||||
|
|
|
@ -11040,8 +11040,10 @@ impl<'db> TypeInferenceBuilder<'db, '_> {
|
|||
.expect("`Bindings` should have at least one `CallableBinding`");
|
||||
|
||||
let mut signature_iter = callable_binding.into_iter().map(|binding| {
|
||||
if argument_type.is_bound_method() {
|
||||
binding.signature.bind_self(self.db(), Some(argument_type))
|
||||
if let Some(bound_method) = argument_type.into_bound_method() {
|
||||
binding
|
||||
.signature
|
||||
.bind_self(self.db(), Some(bound_method.self_instance(db)))
|
||||
} else {
|
||||
binding.signature.clone()
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue