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

Nested functions don't take type restrictions into account #2145

Closed
gvanrossum opened this issue Sep 15, 2016 · 2 comments
Closed

Nested functions don't take type restrictions into account #2145

gvanrossum opened this issue Sep 15, 2016 · 2 comments

Comments

@gvanrossum
Copy link
Member

Example:

from typing import *

def f(a: Union[int, str]) -> int:
    if isinstance(a, str):
        return -1
    def inner() -> int:
        return a+1  # E: Unsupported operand types for + ("Union[int, str]" and "int"
    return inner()

Note that when inner() is defined, and also when it's called, a can only be an int. But we still get an error.

I ran into this when checking some real-world code against strict optional -- there the union was Optional[int] and the type restriction was if a is not None, but it's the same problem.

@ddfisher
Copy link
Collaborator

I think this is quite difficult to do correctly. This is correct because f exits before inner is defined, but wouldn't be correct for other local information about types. For example:

from typing import *

def f(a: Union[int, str]) -> int:
    if isinstance(a, str):
        return -1
    else:
        def inner() -> int:
            return a+1  # E: Unsupported operand types for + ("Union[int, str]" and "int"
    a = "foo"
    return inner()

The binder doesn't keep track of this information at the moment -- I'm not sure how hard it would be to add, but probably not super easy.

@gvanrossum
Copy link
Member Author

I see. I'm fine with closing this then.

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

2 participants