Skip to content

wrongly inferred type builtins.object #15295

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

Open
robsdedude opened this issue May 24, 2023 · 1 comment
Open

wrongly inferred type builtins.object #15295

robsdedude opened this issue May 24, 2023 · 1 comment
Labels
bug mypy got something wrong topic-join-v-union Using join vs. using unions topic-paramspec PEP 612, ParamSpec, Concatenate

Comments

@robsdedude
Copy link

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

  • works should reveal builtins.int
  • fails should reveal Union[builtins.int, builtins.str]

Actual Behavior

  • works reveals builtins.int (as expected)
  • fails reveals builtins.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.0

Related
I think there's a whole bunch of related issues already, but I wanted to share this example to potentially validate a future fix.

#12009 (comment)

@robsdedude robsdedude added the bug mypy got something wrong label May 24, 2023
@JelleZijlstra JelleZijlstra added the topic-paramspec PEP 612, ParamSpec, Concatenate label May 24, 2023
@erictraut
Copy link

This appears to be another case of "union vs join". May want to add that tag for completeness.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-join-v-union Using join vs. using unions topic-paramspec PEP 612, ParamSpec, Concatenate
Projects
None yet
Development

No branches or pull requests

3 participants