Skip to content

Commit e6f7fec

Browse files
committed
Allow comparisons to refine Optional types without strict-optional
Fixes #4520.
1 parent d2c0419 commit e6f7fec

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

mypy/checker.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2862,7 +2862,7 @@ def find_isinstance_check(self, node: Expression
28622862
if literal(expr) == LITERAL_TYPE:
28632863
vartype = type_map[expr]
28642864
return self.conditional_callable_type_map(expr, vartype)
2865-
elif isinstance(node, ComparisonExpr) and experiments.STRICT_OPTIONAL:
2865+
elif isinstance(node, ComparisonExpr):
28662866
# Check for `x is None` and `x is not None`.
28672867
is_not = node.operators == ['is not']
28682868
if any(is_literal_none(n) for n in node.operands) and (

test-data/unit/check-unions.test

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -947,3 +947,21 @@ MYTYPE = List[Union[str, "MYTYPE"]]
947947
[builtins fixtures/list.pyi]
948948
[out]
949949
main:2: error: Recursive types not fully supported yet, nested types replaced with "Any"
950+
951+
[case testNonStrictOptional]
952+
from typing import Optional, List
953+
954+
def union_test1(x):
955+
# type: (Optional[List[int]]) -> Optional[int]
956+
if x is None:
957+
return x
958+
else:
959+
return x[0]
960+
961+
def union_test2(x):
962+
# type: (Optional[List[int]]) -> Optional[int]
963+
if isinstance(x, type(None)):
964+
return x
965+
else:
966+
return x[0]
967+
[builtins fixtures/isinstancelist.pyi]

0 commit comments

Comments
 (0)