Skip to content

Commit

Permalink
Fix for bug with in operation on optionals in no-strict-optional
Browse files Browse the repository at this point in the history
…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]`)
  • Loading branch information
koogoro authored Feb 18, 2023
1 parent ef3187a commit c99133f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -2950,7 +2950,7 @@ def visit_comparison_expr(self, e: ComparisonExpr) -> Type:
right_type = get_proper_type(right_type)
item_types: Sequence[Type] = [right_type]
if isinstance(right_type, UnionType):
item_types = list(right_type.items)
item_types = list(right_type.relevant_items())

sub_result = self.bool_type()

Expand Down
9 changes: 9 additions & 0 deletions test-data/unit/check-optional.test
Original file line number Diff line number Diff line change
Expand Up @@ -1031,3 +1031,12 @@ def f1(b: bool) -> Optional[int]:
class Defer:
def __init__(self) -> None:
self.defer = 10

[case testOptionalIterator]
# mypy: no-strict-optional
from typing import Optional, List

x: Optional[List[int]]
if 3 in x:
pass

0 comments on commit c99133f

Please sign in to comment.