Skip to content

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

Merged
ilevkivskyi merged 2 commits into
masterfrom
optional-any
Sep 18, 2018
Merged

ilevkivskyi merged 2 commits into
masterfrom
optional-any

Conversation

@msullivan

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

@ilevkivskyi ilevkivskyi left a comment

Copy link
Copy Markdown
Member

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

JukkaL commented Sep 18, 2018

Copy link
Copy Markdown
Collaborator

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