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

Union of different Tuples as a return value and conditionals after that #6641

Closed
yurkobb opened this issue Apr 6, 2019 · 1 comment
Closed

Comments

@yurkobb
Copy link

yurkobb commented Apr 6, 2019

  • Are you reporting a bug, or opening a feature request?

Not exactly sure if this is a bug or a feature request, but I think this might be a valid use case.

  • Please insert below the code you are checking with mypy,

A simplified example:

from typing import Tuple, Union

def something_or_nothing() -> Union[Tuple[str, int],
                                    Tuple[None, None]]:
    # In alternative scenario, based on a network request,
    # could return (None, None)
    return ("hi", 18)

def process(string: str, integer: int):
    pass

something, number = something_or_nothing()
if something:
    # number must be int
    process(something, number)
  • What is the actual behavior/output?

test.py:16: error: Argument 2 to "process" has incompatible type "Optional[int]"; expected "int"

  • What is the behavior/output you expect?

Ideally, mypy would recognize that it should be either a tuple of (str, int) or (None, None), but it can't be (str, None).

Currently the only way to represent this code that I know of is to use cast(int, integer), however that doesn't improve the code readability and makes it more confusing.

  • What are the versions of mypy and Python you are using?

mypy 0.700

I haven't tried with git master yet.

  • What are the mypy flags you are using? (For example --strict-optional)

none

@ilevkivskyi
Copy link
Member

This is essentially a duplicate of #5579 (see also #6478 which is related).

This is a valid feature request, but still relatively low priority IMO.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants