Skip to content

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

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

Merged
merged 2 commits into from
Sep 18, 2018

Conversation

msullivan
Copy link
Collaborator

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.

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.
Copy link
Member

@ilevkivskyi ilevkivskyi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Finally this old issue will be fixed! :-)

@ilevkivskyi ilevkivskyi merged commit 3f2f102 into master Sep 18, 2018
@ilevkivskyi ilevkivskyi deleted the optional-any branch September 18, 2018 00:38
@JukkaL
Copy link
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
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants