mirror of
https://github.com/astral-sh/ruff.git
synced 2026-12-04 02:03:58 +08:00
Currently some synthesized methods handle `Self` differently than normal
methods: instead of treating `Self` as an inferrable typevar in the
generic context of the synthesized methods, they eagerly replace `Self`
with the concrete defining class.
The code implementing this eager substitution was a little too broad,
and could also catch method aliases, in cases like this (real-world
example):
```py
from collections.abc import Awaitable
from typing import Any, Literal, Protocol, overload, reveal_type
class AsyncClient(Protocol):
_is_async_client: Literal[True]
class SyncClient(Protocol):
_is_async_client: Literal[False]
class Commands:
@overload
def incrby(self: SyncClient, name: str) -> int: ...
@overload
def incrby(self: AsyncClient, name: str) -> Awaitable[int]: ...
def incrby(self: Any, name: str) -> int | Awaitable[int]:
raise NotImplementedError
incr = incrby
class Redis(Commands):
_is_async_client: Literal[True] = True
def f(r: Redis) -> None:
reveal_type(r.incrby("k")) # Awaitable[int]
reveal_type(r.incr("k")) # expected Awaitable[int]
```
On main, we incorrectly categorize the `incr` alias as a synthesized
method, so we prune overloads correctly for `r.incrby` calls but not for
`r.incr` calls.
This PR makes `Self` handling consistent across synthesized and normal
methods, which fixes these calls to method aliases, too.
Synthesized named-tuple and dataclass methods now register `Self` in
their generic contexts and connect field annotations to that inferable
type variable. This lets them use ordinary call inference. Anonymous
decorator results retain a temporary fallback for
[ty#3186](https://github.com/astral-sh/ty/issues/3186) when a receiver
type variable has no generic context. Specialized protocol receivers and
constrained receivers retain their existing handling.
Pydantic constructor receivers are positional-only, matching the runtime
signature and keeping fields named `self` separate from the receiver.
Fixes astral-sh/ty#4715.
## Test plan
Ran the classmethod, constrained-TypedDict, and union-receiver examples
from the review of #26861 against this implementation. Aliased
classmethods preserve their return types and subclass `Self`;
constrained TypedDict subscription is accepted; the invalid
union-receiver call retains `no-matching-overload` diagnostics instead
of becoming non-callable.
## Ecosystem report
Analyzed
[`6052e4fb1c`](https://github.com/astral-sh/ruff/commit/6052e4fb1c6f941ead46f4a103810802dbe14e38).
Most changes update existing receiver counts; alias return inference
improves, and borrowed methods and classmethod aliases enforce their
receiver constraints. Additional diagnostics expose an existing
native-function binding limitation. All public ecosystem changes are
accounted for and do not block this PR. [Full ecosystem
analysis](https://github.com/astral-sh/ruff/pull/29341#issuecomment-6157452697).
483 B
483 B
Strawberry GraphQL
[environment]
python-version = "3.13"
python-platform = "linux"
[project]
dependencies = ["strawberry-graphql==0.283.3"]
Basic model
import strawberry
@strawberry.type
class User:
id: int
role: str = strawberry.field(default="user")
reveal_type(User.__init__) # revealed: (self: User, /, *, id: int, role: str = ...) -> None
user = User(id=1)
reveal_type(user.id) # revealed: int
reveal_type(user.role) # revealed: str