Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dict type incorrectly converted (widened?) to object via TypeVar and overload #16282

Open
pgjones opened this issue Oct 17, 2023 · 2 comments
Open
Labels
bug mypy got something wrong

Comments

@pgjones
Copy link

pgjones commented Oct 17, 2023

Bug Report

In the snippet below I'd expect the revealed type to be def () -> typing.Awaitable[builtins.dict], not def () -> typing.Awaitable[builtins.object].

To Reproduce

from typing import *


P = ParamSpec("P")
T = TypeVar("T")

@overload
def ensure_async(f: Callable[P, Awaitable[T]])-> Callable[P, Awaitable[T]]: ...

@overload
def ensure_async(f: Callable[P, T])-> Callable[P, Awaitable[T]]: ...

def ensure_async(f):
   ...
        
async def a() -> None:
    g: Callable[[], dict] | Callable[[], Awaitable[dict]]
    reveal_type(ensure_async(g))

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 version used: 1.6 or master
  • Mypy command-line flags:
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used: 3.12

This may be related to #12385 - closest issue I can find.

@pgjones pgjones added the bug mypy got something wrong label Oct 17, 2023
@pgjones 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
@erictraut
Copy link

erictraut commented Oct 17, 2023

Mypy's behavior here is correct.

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.

The mypy documentation for overloaded call evaluation is a bit vague about how this works. The pyright documentation for overloads is a bit more detailed, so it may help illuminate what's happening here.

@pgjones
Copy link
Author

pgjones commented Oct 17, 2023

Thanks, I'll read the documents and try to understand.

Would you know how to fix the typing of the ensure_async function to preserve the dict type?

pgjones added a commit to pallets/quart that referenced this issue Nov 11, 2023
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

2 participants