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)
- Are you reporting a bug, or opening a feature request?
Bug
- Please insert below the code you are checking with mypy,
or a mock-up repro if the source is private. We would appreciate
if you try to simplify your case to a minimal repro.
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
- What is the actual behavior/output?
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.
- What is the behavior/output you expect?
Z[F]
satisfies Union[Z[F], F]
- What are the versions of mypy and Python you are using?
Do you see the same issue after installing mypy from Git master?
Python 3.6.6, using mypy 0.660; same issue happens with master.
- What are the mypy flags you are using? (For example --strict-optional)
No flags