Skip to content
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

Support narrowing of walrus in most cases #8458

Merged
merged 2 commits into from Feb 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 15 additions & 4 deletions mypy/checker.py
Expand Up @@ -3924,10 +3924,12 @@ def find_isinstance_check_helper(self, node: Expression) -> Tuple[TypeMap, TypeM
elif is_false_literal(node):
return None, {}
elif isinstance(node, CallExpr):
if len(node.args) == 0:
return {}, {}
expr = collapse_walrus(node.args[0])
if refers_to_fullname(node.callee, 'builtins.isinstance'):
if len(node.args) != 2: # the error will be reported elsewhere
return {}, {}
expr = node.args[0]
if literal(expr) == LITERAL_TYPE:
return self.conditional_type_map_with_intersection(
expr,
Expand All @@ -3937,13 +3939,11 @@ def find_isinstance_check_helper(self, node: Expression) -> Tuple[TypeMap, TypeM
elif refers_to_fullname(node.callee, 'builtins.issubclass'):
if len(node.args) != 2: # the error will be reported elsewhere
return {}, {}
expr = node.args[0]
if literal(expr) == LITERAL_TYPE:
return self.infer_issubclass_maps(node, expr, type_map)
elif refers_to_fullname(node.callee, 'builtins.callable'):
if len(node.args) != 1: # the error will be reported elsewhere
return {}, {}
expr = node.args[0]
if literal(expr) == LITERAL_TYPE:
vartype = type_map[expr]
return self.conditional_callable_type_map(expr, vartype)
Expand All @@ -3952,7 +3952,7 @@ def find_isinstance_check_helper(self, node: Expression) -> Tuple[TypeMap, TypeM
# narrow their types. (For example, we shouldn't try narrowing the
# types of literal string or enum expressions).

operands = node.operands
operands = [collapse_walrus(x) for x in node.operands]
operand_types = []
narrowable_operand_index_to_hash = {}
for i, expr in enumerate(operands):
Expand Down Expand Up @@ -5742,3 +5742,14 @@ def has_bool_item(typ: ProperType) -> bool:
return any(is_named_instance(item, 'builtins.bool')
for item in typ.items)
return False


def collapse_walrus(e: Expression) -> Expression:
"""If an expression is an AssignmentExpr, pull out the assignment target.

We don't make any attempt to pull out all the targets in code like `x := (y := z)`.
We could support narrowing those if that sort of code turns out to be common.
"""
if isinstance(e, AssignmentExpr):
return e.target
return e
19 changes: 16 additions & 3 deletions test-data/unit/check-python38.test
Expand Up @@ -189,7 +189,7 @@ def f(p1: bytes, p2: float, /) -> None:

[case testWalrus]
# flags: --strict-optional
from typing import NamedTuple, Optional
from typing import NamedTuple, Optional, List
from typing_extensions import Final

if a := 2:
Expand Down Expand Up @@ -288,10 +288,23 @@ def check_partial() -> None:

reveal_type(x) # N: Revealed type is 'Union[builtins.int, None]'

def check_narrow(x: Optional[int]) -> None:
def check_narrow(x: Optional[int], s: List[int]) -> None:
if (y := x):
reveal_type(y) # N: Revealed type is 'builtins.int'
[builtins fixtures/f_string.pyi]

if (y := x) is not None:
reveal_type(y) # N: Revealed type is 'builtins.int'
Copy link
Collaborator

Choose a reason for hiding this comment

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

Will 'x' also end up being narrowed here?

If it doesn't, I think it's probably fine for now, but we should probably file a follow-up task.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

No, but I think that's probably fine? I don't think it's actually idiomatic or makes sense to be using a walrus operator when the right hand side is a value.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Hmm, that's true. I guess we can just not worry about it unless somebody brings it up.


if (y := x) == 10:
reveal_type(y) # N: Revealed type is 'builtins.int'

if (y := x) in s:
reveal_type(y) # N: Revealed type is 'builtins.int'

if isinstance((y := x), int):
reveal_type(y) # N: Revealed type is 'builtins.int'

[builtins fixtures/isinstancelist.pyi]

[case testWalrusPartialTypes]
from typing import List
Expand Down