8000 Allow comparisons to refine Optional types without strict-optional · python/mypy@e77113a · GitHub
[go: up one dir, main page]

Skip to content

Commit e77113a

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

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
@@ -3278,7 +3278,7 @@ def find_isinstance_check(self, node: Expression
32783278
if literal(expr) == LITERAL_TYPE:
32793279
vartype = type_map[expr]
32803280
return self.conditional_callable_type_map(expr, vartype)
3281-
elif isinstance(node, ComparisonExpr) and experiments.STRICT_OPTIONAL:
3281+
elif isinstance(node, ComparisonExpr):
32823282
# Check for `x is None` and `x is not None`.
32833283
is_not = node.operators == ['is not']
32843284
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
@@ -948,3 +948,21 @@ MYTYPE = List[Union[str, "MYTYPE"]]
948948
[builtins fixtures/list.pyi]
949949
[out]
950950
main:2: error: Recursive types not fully supported yet, nested types replaced with "Any"
951+
952+
[case testNonStrictOptional]
953+
from typing import Optional, List
954+
955+
def union_test1(x):
956+
# type: (Optional[List[int]]) -> Optional[int]
957+
if x is None:
958+
return x
959+
else:
960+
return x[0]
961+
962+
def union_test2(x):
963+
# type: (Optional[List[int]]) -> Optional[int]
964+
if isinstance(x, type(None)):
965+
return x
966+
else:
967+
return x[0]
968+
[builtins fixtures/isinstancelist.pyi]

0 commit comments

Comments
 (0)
0