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

Get rid of Optional when assigning Any in an `is None` branch #5629

Merged
merged 2 commits into from Sep 18, 2018

Conversation

Projects
None yet
4 participants
@msullivan
Collaborator

msullivan commented Sep 17, 2018

As a special case, when assigning Any to a variable with a
declared Optional type that has been narrowed to None,
replace all the Nones in the declared Union type with Any.

The result of this is:

def f():...
def g(x: Optional[int]) -> int:
    if x is None:
        x = f()
        reveal_type(x)   # Union[int, Any]  # <---- this is new (was Optional[int])
    reveal_type(x)  # Union[int, Any]  # was also Optional[int]
    return x  # Ok  # was an error

Fixes #3526.

Get rid of Optional when assigning Any in an `is None` branch
As a special case, when assigning Any to a variable with a
declared Optional type that has been narrowed to None,
replace all the Nones in the declared Union type with Any.

The result of this is:
```
def f():...
def g(x: Optional[int]) -> int:
    if x is None:
        x = f()
        reveal_type(x)   # Union[int, Any]  # <---- this is new (was Optional[int])
    reveal_type(x)  # Union[int, Any]  # was also Optional[int]
    return x  # Ok  # was an error
```

Fixes #3526.

@msullivan msullivan requested review from JukkaL, gvanrossum and ilevkivskyi Sep 17, 2018

@ilevkivskyi

Finally this old issue will be fixed! :-)

@ilevkivskyi ilevkivskyi merged commit 3f2f102 into master Sep 18, 2018

2 checks passed

continuous-integration/appveyor/pr AppVeyor build succeeded
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details

@ilevkivskyi ilevkivskyi deleted the optional-any branch Sep 18, 2018

@JukkaL

This comment has been minimized.

Collaborator

JukkaL commented Sep 18, 2018

Great to have this fixed! This has been a pain point for a long time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment