Skip to content

Weird interaction between TypeVar and union #17193

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
tamird opened this issue Apr 29, 2024 · 0 comments
Open

Weird interaction between TypeVar and union #17193

tamird opened this issue Apr 29, 2024 · 0 comments
Labels
bug mypy got something wrong

Comments

@tamird
Copy link
Contributor

tamird commented Apr 29, 2024

See this code.

from typing import TypeVar

T = TypeVar("T", list, dict)

def ctor(cls: type[T]) -> T:
    return cls()
    
def ctor_union(cls: type[list] | type[dict]) -> list | dict:
    return cls()
    
def ctor_union_proxy(cls: type[list] | type[dict]) -> list | dict:
    return ctor(cls)

produces

main.py:12: error: Value of type variable "T" of "ctor" cannot be "Collection[Any]"  [type-var]
main.py:12: error: Incompatible return value type (got "Collection[Any]", expected "list[Any] | dict[Any, Any]")  [return-value]
Found 2 errors in 1 file (checked 1 source file)

possibly duplicate of #9424.

@tamird tamird added the bug mypy got something wrong label Apr 29, 2024
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