Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Wrong type inferred for union containing restricted type variable #2534

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

Closed
jboning opened this issue Dec 7, 2016 · 1 comment
Closed

Wrong type inferred for union containing restricted type variable #2534

jboning opened this issue Dec 7, 2016 · 1 comment
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal topic-union-types

Comments

@jboning
Copy link
Contributor

jboning commented Dec 7, 2016

from typing import Generic, TypeVar, Union

T = TypeVar('T')
class G(Generic[T]): pass
class A(object): pass
class B(object): pass
g_a = None  # type: G[A]
g_b = None  # type: G[B]

AB = TypeVar('AB', A, B)
def f(x):
    # type: (Union[G[AB],AB]) -> G[AB]
    pass

f(A())
f(B())
f(g_a)
f(g_b)  # E: Argument 1 to "f" has incompatible type G[B]; expected "Union[G[A], A]"

The error is incorrect--f(g_b) should be valid.

Swapping A and B in the definition of AB results in an error on the f(g_a) call instead.

I'm not sure this title clearly describes the issue; improvements welcome.

@JukkaL JukkaL added the bug mypy got something wrong label Dec 7, 2016
@JukkaL JukkaL changed the title With unions containing restricted type variables, mypy fixates on first possible type Wrong type inferred for union containing restricted type variable Dec 7, 2016
@JukkaL JukkaL added the false-positive mypy gave an error on correct code label May 19, 2018
@ilevkivskyi
Copy link
Member

This is a duplicate of #3644 (or rather the other way around, but the other issue has more discussion).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal topic-union-types
Projects
None yet
Development

No branches or pull requests

3 participants