Closed
Description
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.
Activity
ddfisher commentedon Oct 22, 2016
I think this is quite difficult to do correctly. This is correct because
f
exits beforeinner
is defined, but wouldn't be correct for other local information about types. For example: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 commentedon Oct 22, 2016
I see. I'm fine with closing this then.