You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
pgjones
changed the title
dict type incorrect converted (widened?) to object via TypeVar and overload
dict type incorrectly converted (widened?) to object via TypeVar and overload
Oct 17, 2023
The value g has type Callable[[], dict] | Callable[[], Awaitable[dict]]. A value of this type is not compatible with the first overload signature, but it is compatible with the second. The constraints of P and T are met with the following types:
P = []
T = int | Awaitable[int]
Mypy's constraint solver uses joins rather than unions, so it widens the type of T to object in this case. This is a legal solution for T even though it's not as precise (narrow) as it could be.
When applying the solutions for P and T to the return type of the second overload, the result is def () -> Awaitable[object]. That explains the behavior you're observing.
You likely thought that mypy would use "union expansion" for the type of argument g and apply different overloads for each of the subtypes of the union, but union expansion is used only if the unexpanded argument types match zero overload signatures. In your example, the unexpanded union type works fine with the second overload signature, so union expansion isn't applied.
This seems to hit a mypy error,
python/mypy#16282, whereby a union input is
widened rather than narrowed by the override options - hence the type
ignores.
Bug Report
In the snippet below I'd expect the revealed type to be
def () -> typing.Awaitable[builtins.dict]
, notdef () -> typing.Awaitable[builtins.object]
.To Reproduce
https://mypy-play.net/?mypy=master&python=3.12&gist=f596993883d9235e669e38dbe212909e
Actual Behavior
main.py:18: note: Revealed type is "def () -> typing.Awaitable[builtins.object]"
Your Environment
mypy.ini
(and other config files):This may be related to #12385 - closest issue I can find.
The text was updated successfully, but these errors were encountered: