Repository navigation
wrongly inferred type builtins.object #15295
Copy link
Copy link
Open
Labels
bugmypy got something wrongmypy got something wrongtopic-join-v-unionUsing join vs. using unionsUsing join vs. using unionstopic-paramspecPEP 612, ParamSpec, ConcatenatePEP 612, ParamSpec, Concatenate
Description
Bug Report
In a rather convoluted nesting, I found that a Union was turned into builtins.object.
To Reproduce
import typing as t
_T = t.TypeVar("_T")
_P = t.ParamSpec("_P")
async def works() -> None:
async def callback(
cb: t.Callable[_P, t.Union[t.Awaitable[_T], _T]],
*args: _P.args, **kwargs: _P.kwargs
) -> _T:
raise NotImplementedError()
def make_t() -> t.Union[
t.Callable[[], t.Coroutine[t.Any, t.Any, int]],
t.Callable[[], int]
]:
raise NotImplementedError()
t = make_t()
t_cb = await callback(t)
reveal_type(t_cb) # note: Revealed type is "builtins.int"
async def fails() -> None:
async def callback(
cb: t.Callable[_P, t.Union[t.Awaitable[_T], _T]],
*args: _P.args, **kwargs: _P.kwargs
) -> _T:
raise NotImplementedError()
def make_t() -> t.Union[
t.Callable[[], t.Coroutine[t.Any, t.Any, t.Union[int, str]]],
t.Callable[[], t.Union[int, str]]
]:
raise NotImplementedError()
t = make_t()
t_cb = await callback(t)
reveal_type(t_cb) # Revealed type is "builtins.object"Gist URL: https://gist.github.com/mypy-play/f932b06f9d8638c2b0c711f8e4048348
Playground URL: https://mypy-play.net/?mypy=latest&python=3.11&gist=f932b06f9d8638c2b0c711f8e4048348
In the example you can see two example functions works and fails which are the exact same except for int in works was replaced with Union[int, str] in fails. Both end with a reveal_type.
Expected Behavior
worksshould revealbuiltins.intfailsshould revealUnion[builtins.int, builtins.str]
Actual Behavior
worksrevealsbuiltins.int(as expected)failsrevealsbuiltins.object(unexpected)
(here's the mypy output of the gist)
main.py:23: note: Revealed type is "builtins.int"
main.py:41: note: Revealed type is "builtins.object"
Success: no issues found in 1 source file
Your Environment
$ python -VV
Python 3.11.0 (main, Nov 2 2022, 13:45:57) [GCC 11.3.0]
$ pip freeze | grep 'mypy\|typing'
mypy==1.3.0
mypy-extensions==1.0.0
typing_extensions==4.5.0Related
I think there's a whole bunch of related issues already, but I wanted to share this example to potentially validate a future fix.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
bugmypy got something wrongmypy got something wrongtopic-join-v-unionUsing join vs. using unionsUsing join vs. using unionstopic-paramspecPEP 612, ParamSpec, ConcatenatePEP 612, ParamSpec, Concatenate
Activity
erictraut commentedon Aug 14, 2023
This appears to be another case of "union vs join". May want to add that tag for completeness.