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

UnboundLocalError handling #4639

Closed
ambv opened this issue Feb 26, 2018 · 1 comment
Closed

UnboundLocalError handling #4639

ambv opened this issue Feb 26, 2018 · 1 comment

Comments

@ambv
Copy link
Contributor

ambv commented Feb 26, 2018

Mypy currently doesn't find any problem with the following code:

def f(x: int) -> str:
    if x > 0:
        result = "larger"
    elif x < 0:
        result = "smaller"
    return result

It will find a related but different type problem if you add an extra line:

def f(x: int) -> str:
    result = None
    if x > 0:
        result = "larger"
    elif x < 0:
        result = "smaller"
    return result  # Incompatible return value type (got "Optional[str]", expected "str")

AFAICT, a local defined in a branch could be viewed as a Union of its proper type and Unbound. In which case the first example could raise a Mypy error about result being potentially unbound (as it is for x=0).

Thoughts?

@JukkaL
Copy link
Collaborator

JukkaL commented Feb 27, 2018

Yes, this would be nice to have, but generally detecting these issues without false positives could be tricky.

Closing as a duplicate of #2400.

@JukkaL JukkaL closed this as completed Feb 27, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants