Union[Generic[T], T]] does not work as expected as a parameter in the signature of a generic function · Issue #6417 · python/mypy (original) (raw)

Bug

from typing import Generic, TypeVar, Union

F = TypeVar("F")

class Z(Generic[F]): y: F def init(self, y: F): self.y = y

def q2(x: Union[Z[F], F]) -> F: if isinstance(x, Z): return x.y else: return x

def q3(x: Z[F]) -> F: return x.y

p = Z('hi') reveal_type(p) # Z[builtins.str*] reveal_type(q2) # def [F] (x: Union[Z[F-1], F-1]) -> F`-1 q2(p) # error: Argument 1 to "q2" has incompatible type "Z[str]"; expected "Z[]" q3(p) # No error

When creating a generic function that has a signature where one parameter is a union on a generic of a typevar and that typevar, calling with the generic causes an error.

Z[F] satisfies Union[Z[F], F]

Python 3.6.6, using mypy 0.660; same issue happens with master.

No flags