Skip to content

Commit c99133f

Browse files
authored
Fix for bug with in operation on optionals in no-strict-optional mode (#14727)
Fixes a bug introduced in #14384 wherein a union that includes `None` is no longer treated as a valid right-hand type for the `in` operator in `no-strict-optional` mode. (The reported error is `error: "None" has no attribute "__iter__" (not iterable) [attr-defined]`)
1 parent ef3187a commit c99133f

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

Diff for: mypy/checkexpr.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2950,7 +2950,7 @@ def visit_comparison_expr(self, e: ComparisonExpr) -> Type:
29502950
right_type = get_proper_type(right_type)
29512951
item_types: Sequence[Type] = [right_type]
29522952
if isinstance(right_type, UnionType):
2953-
item_types = list(right_type.items)
2953+
item_types = list(right_type.relevant_items())
29542954

29552955
sub_result = self.bool_type()
29562956

Diff for: test-data/unit/check-optional.test

+9
Original file line numberDiff line numberDiff line change
@@ -1031,3 +1031,12 @@ def f1(b: bool) -> Optional[int]:
10311031
class Defer:
10321032
def __init__(self) -> None:
10331033
self.defer = 10
1034+
1035+
[case testOptionalIterator]
1036+
# mypy: no-strict-optional
1037+
from typing import Optional, List
1038+
1039+
x: Optional[List[int]]
1040+
if 3 in x:
1041+
pass
1042+

0 commit comments

Comments
 (0)