From 5e2702fa753ed1625d5788dcb1b71ac4a50989fd Mon Sep 17 00:00:00 2001 From: Max Murin Date: Fri, 17 Feb 2023 13:20:23 -0800 Subject: [PATCH] Add fix and regression test for bug with `in` on optionals --- mypy/checkexpr.py | 2 +- test-data/unit/check-optional.test | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/mypy/checkexpr.py b/mypy/checkexpr.py index 754ba6f093f5..38b5c2419d95 100644 --- a/mypy/checkexpr.py +++ b/mypy/checkexpr.py @@ -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() diff --git a/test-data/unit/check-optional.test b/test-data/unit/check-optional.test index db07290f7b40..754c6b52ff19 100644 --- a/test-data/unit/check-optional.test +++ b/test-data/unit/check-optional.test @@ -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 +