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

Not evaluating Union[X, Y] from Type[Union[X, Y]] over (Type[T]) -> T function #13600

Open
ericbn opened this issue Sep 3, 2022 · 1 comment
Labels
bug mypy got something wrong

Comments

@ericbn
Copy link

ericbn commented Sep 3, 2022

Bug Report

Mypy is evaluating builtins.object instead of Union[X, Y] as the returned type when passing an argument of type Type[Union[X, Y]] to a def [T] (cl: Type[T']) -> T' function.

To Reproduce

This works as expected:

from typing import TypeVar

T = TypeVar("T")


def _structure(cl: type[T]) -> T:
    return cl()


def structure(cl: type[int]) -> int:
    reveal_type(cl)
    reveal_type(_structure)
    data = _structure(cl)
    reveal_type(data)
    return data

Output:

scratch.py:11: note: Revealed type is "Type[builtins.int]"
scratch.py:12: note: Revealed type is "def [T] (cl: Type[T`-1]) -> T`-1"
scratch.py:14: note: Revealed type is "builtins.int"
Success: no issues found in 1 source file

Using str instead of int above also works as expected the same way.

But this does not:

from typing import TypeVar

T = TypeVar("T")


def _structure(cl: type[T]) -> T:
    return cl()


def structure(cl: type[int | str]) -> int | str:
    reveal_type(cl)
    reveal_type(_structure)
    data = _structure(cl)
    reveal_type(data)
    return data

Expected Behavior

Output:

scratch.py:11: note: Revealed type is "Union[Type[builtins.int], Type[builtins.str]]"
scratch.py:12: note: Revealed type is "def [T] (cl: Type[T`-1]) -> T`-1"
scratch.py:14: note: Revealed type is "Union[builtins.int, builtins.str]"
Success: no issues found in 1 source file

or

scratch.py:11: note: Revealed type is "Type[Union[builtins.int, builtins.str]]"
scratch.py:12: note: Revealed type is "def [T] (cl: Type[T`-1]) -> T`-1"
scratch.py:14: note: Revealed type is "Union[builtins.int, builtins.str]"
Success: no issues found in 1 source file

(Type[Union[X, Y]] instead of Union[Type[X], Type[Y]] in line 11)

Actual Behavior

Output:

scratch.py:11: note: Revealed type is "Union[Type[builtins.int], Type[builtins.str]]"
scratch.py:12: note: Revealed type is "def [T] (cl: Type[T`-1]) -> T`-1"
scratch.py:14: note: Revealed type is "builtins.object"
scratch.py:15: error: Incompatible return value type (got "object", expected "int | str")  [return-value]
Found 1 error in 1 file (checked 1 source file)

(revealed type in line 14 is "builtins.object" instead of "Union[builtins.int, builtins.str]")

The error looks like might be because of the covariance of Union.

Your Environment

  • Mypy version used: 1.4.1
  • Mypy command-line flags: just the file name, no extra flags.
  • Mypy configuration options from mypy.ini (and other config files):
    [tool.mypy]
    platform = "linux"
    python_version = "3.10"
    
  • Python version used: 3.10.12
  • Operating system and version: macOS 12.6.7
@ericbn ericbn added the bug mypy got something wrong label Sep 3, 2022
@ericbn
Copy link
Author

ericbn commented Jul 22, 2023

Updated the issue description. It was initially reported using mypy version 0.971 and it's still present in mypy 1.4.1.

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

1 participant