Replies: 1 comment 1 reply
-
Overload evaluation is underspecified in the typing spec currently. I've been spearheading an effort to specify and clarify the standard behavior. In fact, I gave a talk about it earlier today in an online typing meetup. Both pyright and mypy current exhibit edge-case behaviors that are arguably incorrect — and clearly inconsistent with each other. The behavior you're highlighting here is a good example. I don't plan to make any changes to pyright's current overload behavior until we've reached a point of resolution on the official spec. Making changes prior to that would be unnecessarily disruptive to pyright users. Once we have agreement on the spec, I'll update pyright to conform to it, and I'm hopeful that mypy's maintainers will do the same. I realize this puts you in a tough position in the meantime. Overload behavior has been a long-time pain point for authors of libraries and stubs. |
Beta Was this translation helpful? Give feedback.
-
I'm working on a potential change to typeshed's definition of
dict.get()
, and I noticed that pyright produces what seems like an incorrect result. I reviewed pyright's overload resolution documentation but I can't tell at exactly which step it's going off based on that.For reference, this is the current typeshed definition of
dict.get()
:In this scenario, mypy and pyright disagree: mypy says
Any
and pyright saysstr
. That seems fine:str
is a good result here.I'm looking at what happens if the first branch of the overload changes to
def get(self, key: _KT, default: None = None, /) -> _VT | None: ...
, with a goal of:Here's code to reproduce that behavior without a custom typeshed:
In this case, mypy still says
Any
, but pyright now saysstr | None
, which doesn't seem like an acceptable result.str | Any
would be much better. You need to get to the third branch for that to appear, but I'd think that all three are ambiguous in the face ofdefault=Any
, and the third branch'sstr | Any
is a superset of bothstr | None
andstr
from the first and second branches. This seems like a failure of ambiguous overload coalescing?Beta Was this translation helpful? Give feedback.
All reactions