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

Incorrect type reification on optional union attribute in conditional #7884

Open
elprans opened this issue Nov 5, 2019 · 1 comment
Open
Labels
bug mypy got something wrong priority-1-normal topic-join-v-union Using join vs. using unions

Comments

@elprans
Copy link
Contributor

elprans commented Nov 5, 2019

Repro:

bug.py:

from __future__ import annotations

from typing import *


class B:
    pass


class A:
    name: Optional[Union[str, B]]


def foo(a: A):
    if isinstance(a.name, str):
        pass
    elif isinstance(a.name, B):
        pass
    else:
        raise AssertionError

    bar(a.name)


def bar(arg: Union[str, B]):
    pass

mypy incorrectly reports an error:

bug.py:22: error: Argument 1 to "bar" has incompatible type "object"; expected "Union[str, B]"

Interestingly, when a.name is aliased with another name, the issue disappers, i.e. the following is checked without an issue:

ok.py:

from __future__ import annotations

from typing import *


class B:
    pass


class A:
    name: Optional[Union[str, B]]


def foo(a: A):
    a_name = a.name

    if isinstance(a_name, str):
        pass
    elif isinstance(a_name, B):
        pass
    else:
        raise AssertionError

    bar(a_name)


def bar(arg: Union[str, B]):
    pass
@ilevkivskyi
Copy link
Member

I confirm this. This honestly looks like a mystery, it looks like mypy applies a join instead of a union for attribute expressions, but understanding why would require some investigation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong priority-1-normal topic-join-v-union Using join vs. using unions
Projects
None yet
Development

No branches or pull requests

3 participants