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

Allow comparisons to refine Optional types without strict-optional #4523

Merged
merged 7 commits into from Sep 21, 2018
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
12 changes: 5 additions & 7 deletions mypy/binder.py
Expand Up @@ -267,15 +267,13 @@ def assign_type(self, expr: Expression,
return

enclosing_type = self.most_recent_enclosing_type(expr, type)
if (isinstance(enclosing_type, AnyType)
and not restrict_any):
if isinstance(enclosing_type, AnyType) and not restrict_any:
# If x is Any and y is int, after x = y we do not infer that x is int.
# This could be changed.
if not isinstance(type, AnyType):
# We narrowed type from Any in a recent frame (probably an
# isinstance check), but now it is reassigned, so broaden back
# to Any (which is the most recent enclosing type)
self.put(expr, enclosing_type)
# Instead, since we narrowed type from Any in a recent frame (probably an
# isinstance check), but now it is reassigned, we broaden back
# to Any (which is the most recent enclosing type)
self.put(expr, enclosing_type)
# 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.
Expand Down
2 changes: 1 addition & 1 deletion mypy/checker.py
Expand Up @@ -3330,7 +3330,7 @@ def find_isinstance_check(self, node: Expression
if literal(expr) == LITERAL_TYPE:
vartype = type_map[expr]
return self.conditional_callable_type_map(expr, vartype)
elif isinstance(node, ComparisonExpr) and experiments.STRICT_OPTIONAL:
elif isinstance(node, ComparisonExpr):
# Check for `x is None` and `x is not None`.
is_not = node.operators == ['is not']
if any(is_literal_none(n) for n in node.operands) and (
Expand Down
11 changes: 4 additions & 7 deletions test-data/unit/check-isinstance.test
Expand Up @@ -2077,9 +2077,7 @@ class A: pass

def foo1(x: Union[A, str, None]) -> None:
if x is None:
# Since None is a subtype of all types in no-strict-optional,
# we can't really narrow the type here
reveal_type(x) # E: Revealed type is 'Union[__main__.A, builtins.str, None]'
reveal_type(x) # E: Revealed type is 'None'
elif isinstance(x, A):
# Note that Union[None, A] == A in no-strict-optional
reveal_type(x) # E: Revealed type is '__main__.A'
Expand All @@ -2088,12 +2086,12 @@ def foo1(x: Union[A, str, None]) -> None:

def foo2(x: Optional[str]) -> None:
if x is None:
reveal_type(x) # E: Revealed type is 'Union[builtins.str, None]'
reveal_type(x) # E: Revealed type is 'None'
elif isinstance(x, A):
# Mypy should, however, be able to skip impossible cases
reveal_type(x)
else:
reveal_type(x) # E: Revealed type is 'Union[builtins.str, None]'
reveal_type(x) # E: Revealed type is 'builtins.str'
[builtins fixtures/isinstance.pyi]

[case testNoneCheckDoesNotNarrowWhenUsingTypeVars]
Expand Down Expand Up @@ -2153,7 +2151,7 @@ def bar(x: Union[List[str], List[int], None]) -> None:
from typing import Union, Optional, List

# This test is the same as the one above, except for strict-optional.
# It isn't testing anything explicitly and mostly exists for the sake
# It isn't testing anything explicitly and mostly exists for the sake
# of completeness.

def foo(x: Optional[List[str]]) -> None:
Expand All @@ -2166,4 +2164,3 @@ def bar(x: Union[List[str], List[int], None]) -> None:
assert isinstance(x, list)
reveal_type(x) # E: Revealed type is 'Union[builtins.list[builtins.str], builtins.list[builtins.int]]'
[builtins fixtures/isinstancelist.pyi]

2 changes: 1 addition & 1 deletion test-data/unit/check-tuples.test
Expand Up @@ -1099,7 +1099,7 @@ possibles: Tuple[int, Tuple[A]]
x: Optional[Tuple[B]]

if x in possibles:
reveal_type(x) # E: Revealed type is 'Union[Tuple[__main__.B], None]'
reveal_type(x) # E: Revealed type is 'Tuple[__main__.B]'
else:
reveal_type(x) # E: Revealed type is 'Union[Tuple[__main__.B], None]'

Expand Down
28 changes: 28 additions & 0 deletions test-data/unit/check-unions.test
Expand Up @@ -948,3 +948,31 @@ MYTYPE = List[Union[str, "MYTYPE"]]
[builtins fixtures/list.pyi]
[out]
main:2: error: Recursive types not fully supported yet, nested types replaced with "Any"

[case testNonStrictOptional]
from typing import Optional, List

def union_test1(x):
# type: (Optional[List[int]]) -> Optional[int]
if x is None:
return x
else:
return x[0]

def union_test2(x):
# type: (Optional[List[int]]) -> Optional[int]
if isinstance(x, type(None)):
return x
else:
return x[0]

def f(): return 0

def union_test3():
# type: () -> int
x = f()
assert x is None
x = f()
return x + 1

[builtins fixtures/isinstancelist.pyi]
2 changes: 1 addition & 1 deletion test-data/unit/check-unreachable-code.test
Expand Up @@ -618,7 +618,7 @@ class Child(Parent):
def foo(self) -> int:
reveal_type(self) # E: Revealed type is '__main__.Child'
if self is None:
reveal_type(self) # E: Revealed type is '__main__.Child'
reveal_type(self) # E: Revealed type is 'None'
return None
reveal_type(self) # E: Revealed type is '__main__.Child'
return 3
Expand Down