python / python/mypy

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

Open
#16,282 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-join-v-union
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

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 https://github.com/python/mypy/issues/12385 - closest issue I can find.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the provided Python reproducer in mypy-play and confirm the revealed type on the current mypy version. Trace overload resolution and TypeVar inference for the union of callable signatures; done means the result is reported as Callable[[], Awaitable[dict]] rather than Awaitable[object], with a regression test covering the case.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.