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

Return conditional expr incorrectly complains about union #1224

Closed
gvanrossum opened this issue Feb 20, 2016 · 4 comments
Closed

Return conditional expr incorrectly complains about union #1224

gvanrossum opened this issue Feb 20, 2016 · 4 comments
Assignees
Labels
bug mypy got something wrong

Comments

@gvanrossum
Copy link
Member

This gives an error:

def foo(a: bool) -> Dict[str, Union[str, int]]:
    return {'a': 'b'} if a else {}  # E: Incompatible return value type: expected builtins.dict[builtins.str, Union[builtins.str, builtins.int]], got builtins.dict[builtins.str, builtins.str]

However this rewrite is fine:

def foo(a: bool) -> Dict[str, Union[str, int]]:
    if a:
        return {'a': 'b'}
    else:
        return {}
@gvanrossum
Copy link
Member Author

It gets weirder. Try this:

def foo(a: bool) -> Dict[str, Union[str, int]]:
    return {'a': 'b'} if a else {'a': 0}

This gives the same error as above PLUS:

x.py:4: error: List item 0 has incompatible type "Tuple[str, int]"

That error also shows up for just this (at the top level):

{'a': 'b'} if True else {'a': 0}

@gvanrossum
Copy link
Member Author

The latter issue may be a red herring; it's because if-else expressions aren't balanced, and it looks weird because the dict literals are translated to dict() calls. A similar error appears for a simpler case:

[''] if True else [0]  # E: List item 0 has incompatible type "int"

@ddfisher ddfisher added this to the 0.3.2 milestone Mar 1, 2016
@ddfisher ddfisher added the bug mypy got something wrong label Mar 2, 2016
@ddfisher
Copy link
Collaborator

ddfisher commented Mar 2, 2016

Looks similar to #1094

@gvanrossum
Copy link
Member Author

Yeah, they're duplicates. We could probably fix a few common examples without doing full unification.

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

2 participants