GeezerVison is showing github.com — `overload`s resolved as a conflict with `Any` · Issue #4055 · astral-sh/ty
overloads resolved as a conflict with Any #4055
DescriptionSummary
In the following example, ty gives Unknown, whereas other type checkers give Series[Timestamp].
I think the first overload should take over.
from typing import Any, Generic, TypeVar, overload, reveal_type
T = TypeVar("T")
class Timestamp: ...
class Timedelta: ...
class Series(Generic[T]):
@overload
def __sub__(self: Series[Timestamp], other: Timedelta) -> Series[Timestamp]: ...
@overload
def __sub__(self: Series[Timedelta], other: Timedelta) -> Series[Timedelta]: ...
reveal_type(Series[Any]() - Timedelta())
Version
5e698d5e4 Activitycarljm commented on Jul 21, 2026 This is a case where ty is doing what the typing spec actually says to do (and the strictly correct thing to do), whereas all other type checkers have decided to implement some heuristic to favor the first overload and depart from what the spec actually says.
Specifically, mypy and pyrefly both exclude self arguments (bound method receiver) from step 5 of the overload algorithm. (I don't see any principled reason why these arguments should be excluded). Pyright goes further than this and often prefers the first overload rather than applying step 5 of the specified algorithm at all.
It seems to me that ty's inference here is more correct than any other type checker. If you have a Series[Any] and call __sub__ on it, there's no reason to assume it must be a Series[Timestamp] just because that overload happens to come first. The ideal return type here would be AnyOf[Timestamp, Timedelta] -- Any or Unknown is the best available option short of having an AnyOf type.
That said, we could easily implement the same approach as mypy/pyrefly for compatibility's sake. added this to the This milestone has been deleted milestone on Jul 21, 2026carljm commented on Jul 21, 2026 added a commit that references this issue on Jul 31, 2026TST: #1799 enable `ty` in `tests/frame/*/**` except for `tests/frame/… cmp0xff commented on Oct 12, 2026 Following python/mypy#22011, nightly mypy now also recognises the gradual receiver ambiguity. Rather than restoring first-match selection, could ty preserve the shared outer container where possible? This would give pandas-stubs a migration path. carljm commented on Oct 13, 2026 astral-sh/ruff#29342 should fix this and preserve Series[Unknown] rather than Unknown as the result of an ambiguous call. added a commit that references this issue on Oct 14, 2026This issue will close once commit ef02d60 is merged into the 'main' branch.ef02d60
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|