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

Weird [mis]interaction between generics and union #17756

Open
ANogin opened this issue Sep 10, 2024 · 0 comments
Open

Weird [mis]interaction between generics and union #17756

ANogin opened this issue Sep 10, 2024 · 0 comments
Labels
bug mypy got something wrong

Comments

@ANogin
Copy link

ANogin commented Sep 10, 2024

Bug Report

In the following file

from typing import Generic, Sequence, Tuple, TypeVar

def f(
    x: Sequence[str], y: Sequence[int] | Sequence[bool]
) -> zip[Tuple[str, int]] | zip[Tuple[str, bool]]:
    return zip(x, y) # OK

T = TypeVar("T")

class C(Generic[T]):
    def g(
        self, x: Sequence[str], y: Sequence[T] | Sequence[bool]
    ) -> zip[Tuple[str, T]] | zip[Tuple[str, bool]]:
        return zip(x, y) # ERROR

mypy accepts f, but complains

error: Incompatible return value type (got "zip[tuple[str, object]]", expected "zip[tuple[str, T]] | zip[tuple[str, bool]]")  [return-value]

on the return from C.g

Confirmed with mypy 1.11.2 in Python 3.11.9, but does not seem version-specific.

P.S. My specific use case where I found this issue was where I had for (a,b) in zip(x,y), then was using b where Union[T|bool] was expected, and getting an error message from mypy that object (b) is used where T|bool was expected; I tried to simplify that code to digest it to the minimal reproducible example. I do not fully understand all the constraints here, but the fact that f is accepted and C.g is not does suggest to me that something is clearly wrong.

@ANogin ANogin added the bug mypy got something wrong label Sep 10, 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