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

Narrow types after 'in' operator #4072

Merged
merged 6 commits into from Oct 13, 2017

Conversation

Projects
None yet
2 participants
@ilevkivskyi
Collaborator

ilevkivskyi commented Oct 8, 2017

Fixes #4071

This is separated from #4070 plus added various tests.

ilevkivskyi added a commit to ilevkivskyi/mypy that referenced this pull request Oct 8, 2017

ilevkivskyi added a commit to ilevkivskyi/mypy that referenced this pull request Oct 8, 2017

@JukkaL

This comment has been minimized.

Show comment
Hide comment
@JukkaL

JukkaL Oct 12, 2017

Collaborator

As discussed in #4099 (comment), let's only do this for optional types, since other cases can be unsafe because of operator overloading, and these other cases seem pretty rare anyway.

Collaborator

JukkaL commented Oct 12, 2017

As discussed in #4099 (comment), let's only do this for optional types, since other cases can be unsafe because of operator overloading, and these other cases seem pretty rare anyway.

@ilevkivskyi

This comment has been minimized.

Show comment
Hide comment
@ilevkivskyi

ilevkivskyi Oct 12, 2017

Collaborator

OK, I will do this later this evening or tomorrow morning.

Collaborator

ilevkivskyi commented Oct 12, 2017

OK, I will do this later this evening or tomorrow morning.

ilevkivskyi added a commit to ilevkivskyi/mypy that referenced this pull request Oct 13, 2017

@ilevkivskyi

This comment has been minimized.

Show comment
Hide comment
@ilevkivskyi

ilevkivskyi Oct 13, 2017

Collaborator

@JukkaL

let's only do this for optional types

Done!

Collaborator

ilevkivskyi commented Oct 13, 2017

@JukkaL

let's only do this for optional types

Done!

@JukkaL

Thanks, looks good! Just a few minor comments.

Show outdated Hide outdated mypy/checker.py
Show outdated Hide outdated test-data/unit/check-isinstance.test
# flags: --strict-optional
from typing import List, Optional
x: List[int]

This comment has been minimized.

@JukkaL

JukkaL Oct 13, 2017

Collaborator

Add test for optional item type (e.g. List[Optional[int]]).

@JukkaL

JukkaL Oct 13, 2017

Collaborator

Add test for optional item type (e.g. List[Optional[int]]).

@ilevkivskyi

This comment has been minimized.

Show comment
Hide comment
@ilevkivskyi

ilevkivskyi Oct 13, 2017

Collaborator

@JukkaL Thanks for review! I implemented the changes in the new commit.

Collaborator

ilevkivskyi commented Oct 13, 2017

@JukkaL Thanks for review! I implemented the changes in the new commit.

# flags: --strict-optional
from typing import List, Optional
x: List[Optional[int]]

This comment has been minimized.

@JukkaL

JukkaL Oct 13, 2017

Collaborator

Here's another special case: What if the container is a nested one, say List[List[x]], where x might be Any? How do we deal with various item types, such as List[int] and List[Any]?

@JukkaL

JukkaL Oct 13, 2017

Collaborator

Here's another special case: What if the container is a nested one, say List[List[x]], where x might be Any? How do we deal with various item types, such as List[int] and List[Any]?

This comment has been minimized.

@ilevkivskyi

ilevkivskyi Oct 13, 2017

Collaborator

a) If the container is List[Any], then we do nothing (there is a test for this).
b) If the container is List[List[Any]] we narrow the type (provided there is an overlap, this is consistent with how == currently treated), for example:

x: Optional[int]
lst: Optional[List[int]]
nested: List[List[Any]]
if lst in nested:
    reveal_type(lst) # List[int]
if x in nested:
    reveal_type(x) # Optional[int]

There is already a test for non-overlapping items int vs str. I will add one more test for other (nested) types?

@ilevkivskyi

ilevkivskyi Oct 13, 2017

Collaborator

a) If the container is List[Any], then we do nothing (there is a test for this).
b) If the container is List[List[Any]] we narrow the type (provided there is an overlap, this is consistent with how == currently treated), for example:

x: Optional[int]
lst: Optional[List[int]]
nested: List[List[Any]]
if lst in nested:
    reveal_type(lst) # List[int]
if x in nested:
    reveal_type(x) # Optional[int]

There is already a test for non-overlapping items int vs str. I will add one more test for other (nested) types?

This comment has been minimized.

@ilevkivskyi

ilevkivskyi Oct 13, 2017

Collaborator

OK, added one more test.

@ilevkivskyi

ilevkivskyi Oct 13, 2017

Collaborator

OK, added one more test.

@JukkaL JukkaL merged commit b875205 into python:master Oct 13, 2017

1 of 2 checks passed

continuous-integration/travis-ci/pr The Travis CI build is in progress
Details
continuous-integration/appveyor/pr AppVeyor build succeeded
Details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment