SIGN IN SIGN UP
astral-sh / ruff UNCLAIMED

An extremely fast Python linter and code formatter, written in Rust.

0 0 35 Rust

[ty] Narrow bound method overloads by receiver (#24707)

## Summary

Given:

```python
from typing import overload

class Base:
    @overload
    def convert(self: "Base", x: int) -> int: ...

    @overload
    def convert(self: "Child", x: str) -> str: ...

    def convert(self, x: int | str) -> int | str:
        return x

class Child(Base): ...
```

On main, we treat `Base().convert` as if both overloads are still
possible, including the overload that requires `self: "Child"`.

After this PR:

```python
reveal_type(Base().convert)
# bound method Base.convert(x: int) -> int

reveal_type(Child().convert)
# Overload[(x: int) -> int, (x: str) -> str]
```

So `Base().convert("x")` is no longer considered valid via narrowing on
the actual receiver type.

Closes https://github.com/astral-sh/ty/issues/2693.

Closes https://github.com/astral-sh/ty/issues/2612.

Closes https://github.com/astral-sh/ty/issues/3380.

Closes https://github.com/astral-sh/ty/issues/1169.
C
Charlie Marsh committed
a5cc4e79e33ecba98a1d4c837c1fd78e04efd87b
Parent: 5a6c811
Committed by GitHub <noreply@github.com> on 5/30/2026, 7:37:34 AM