Skip to content

Specialised-self overloads select the first match for a gradual receiver #5205

Open

Description

@cmp0xff

Describe the Bug

Pyrefly 1.3.2 and 1.4.0.dev3 select the first specialised-self overload for Container[Any], but infer Unknown for the equivalent ordinary function. This reproduces with and without --spec-compliant-overloads=true.

from typing import Any, Generic, TypeVar, overload, reveal_type

T = TypeVar("T")


class Container(Generic[T]):
    @overload
    def scalar(self: "Container[int]") -> int: ...
    @overload
    def scalar(self: "Container[str]") -> str: ...
    def scalar(self) -> int | str:
        raise NotImplementedError

@overload
def scalar(value: Container[int]) -> int: ...
@overload
def scalar(value: Container[str]) -> str: ...
def scalar(value: Container[Any]) -> int | str:
    raise NotImplementedError


def probe(gradual: Container[Any]) -> None:
    reveal_type(gradual.scalar())  # int
    reveal_type(scalar(gradual))   # Unknown

Python: 3.12

Expected: handle receiver ambiguity consistently with explicit arguments,
per overload Step 5: https://typing.python.org/en/latest/spec/overload.html#step-5

Reversing the method overloads changes its result to str; concrete receivers remain precise.

Sandbox Link

https://pyrefly.org/sandbox/?project=v2.pZUxD4IwEIX_SiMDuNCdycTJxTjgpA5VGzRqawD17_sO2lqqEokbIXevd-1974ZapvNF511fjShHwSYqWY_y9ah5PEvhVKsaDcgyMZKrfGOnb2I1X1BVO3GGDkGV4Rpc9gpPvoE0DRo-M5am6XCRxiBaEXx6IkGWPQYwUlyIwlzXM7KWi1R4XweFV4mnZ-judhL08WMmlR8U3xeOh3QHdTrp7aLVvJZ6K5OiFPubOH8U9XD3BsKmpKao8ZixiAp4jzQRJoECEblUJ6UfNPYBdBFbtD-YmV-sG9Ba3LDUQCGuIsI-k-xQ19cq49ym67LgUvG93lW8k8EZ-Yu4i2OzKrtyTDcmUUH2H9rjFva4j_WYUEdEL75P

(Only applicable for extension issues) IDE Information

No response

Activity

  1. cmp0xff commented on Oct 14, 2026

    @cmp0xff
    Author

    It seems that ty and pyright are both moving in this direction astral-sh/ruff#29342 microsoft/pyright#11864

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions