GeezerVison is showing github.com — Improve negative narrowing for membership checks on tuples (#21456) · python/mypy@a900831
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: mypy/checker.py +38-18Lines changed: 38 additions & 18 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -6771,25 +6771,45 @@ def comparison_type_narrowing_helper(self, node: ComparisonExpr) -> tuple[TypeMa | 6771 | 6771 | else_map = {} | 6772 | 6772 |
| 6773 | 6773 | if left_index in narrowable_operand_index_to_hash: | 6774 | | - collection_item_type = get_proper_type(builtin_item_type(iterable_type)) | 6775 | | - if collection_item_type is not None: | 6776 | | - if_map, else_map = self.narrow_type_by_identity_equality( | 6777 | | - "==", | 6778 | | - operands=[operands[left_index], operands[right_index]], | 6779 | | - operand_types=[item_type, collection_item_type], | 6780 | | - expr_indices=[0, 1], | 6781 | | - narrowable_indices={0}, | 6782 | | - ) | 6783 | | - if else_map and not ( | 6784 | | - isinstance(p_typ := get_proper_type(iterable_type), TupleType) | 6785 | | - and all( | 6786 | | - is_singleton_equality_type(get_proper_type(item)) | 6787 | | - for item in p_typ.items | | 6774 | + p_iterable_type = get_proper_type(iterable_type) | | 6775 | + if ( | | 6776 | + isinstance(p_iterable_type, TupleType) | | 6777 | + and find_unpack_in_list(p_iterable_type.items) is None | | 6778 | + ): | | 6779 | + # For some tuples, we can do negative narrowing, e.g. `x not in (None,)` | | 6780 | + all_if_maps = [] | | 6781 | + all_else_maps = [] | | 6782 | + for known_item in p_iterable_type.items: | | 6783 | + # Match the should_coerce_literals logic from narrow_type_by_identity_equality | | 6784 | + p_known_item = get_proper_type(known_item) | | 6785 | + if is_literal_type_like(p_known_item) or ( | | 6786 | + isinstance(p_known_item, Instance) and p_known_item.type.is_enum | | 6787 | + ): | | 6788 | + known_item = coerce_to_literal(known_item) | | 6789 | + if_map, else_map = self.narrow_type_by_identity_equality( | | 6790 | + "==", | | 6791 | + operands=[operands[left_index], operands[right_index]], | | 6792 | + operand_types=[item_type, known_item], | | 6793 | + expr_indices=[0, 1], | | 6794 | + narrowable_indices={0}, | 6788 | 6795 | ) | 6789 | | - ): | 6790 | | - # In general, we can't do negative narrowing, since e.g. the container | 6791 | | - # could just be empty. However, we can do negative narrowing for some | 6792 | | - # tuples e.g. `x not in (None,)` | | 6796 | + all_if_maps.append(if_map) | | 6797 | + if is_singleton_equality_type(get_proper_type(known_item)): | | 6798 | + all_else_maps.append(else_map) | | 6799 | + if_map = reduce_or_conditional_type_maps(all_if_maps) | | 6800 | + else_map = reduce_and_conditional_type_maps(all_else_maps, use_meet=True) | | 6801 | + else: | | 6802 | + collection_item_type = get_proper_type(builtin_item_type(iterable_type)) | | 6803 | + if collection_item_type is not None: | | 6804 | + if_map, else_map = self.narrow_type_by_identity_equality( | | 6805 | + "==", | | 6806 | + operands=[operands[left_index], operands[right_index]], | | 6807 | + operand_types=[item_type, collection_item_type], | | 6808 | + expr_indices=[0, 1], | | 6809 | + narrowable_indices={0}, | | 6810 | + ) | | 6811 | + # We can't do negative narrowing, since e.g. the container could | | 6812 | + # just be empty. | 6793 | 6813 | else_map = {} | 6794 | 6814 |
| 6795 | 6815 | if right_index in narrowable_operand_index_to_hash: | |
Collapse file Copy file name to clipboardExpand all lines: test-data/unit/check-isinstance.test +5-7Lines changed: 5 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -2294,18 +2294,16 @@ def f(x: Optional[int], lst: Optional[List[int]], nested_any: List[List[Any]]) - | 2294 | 2294 |
| 2295 | 2295 | [case testNarrowTypeAfterInTuple] | 2296 | 2296 | # flags: --warn-unreachable | 2297 | | -from typing import Optional | 2298 | 2297 | class A: pass | 2299 | 2298 | class B(A): pass | 2300 | 2299 | class C(A): pass | 2301 | 2300 |
| 2302 | | -y: Optional[B] | 2303 | | -if y in (B(), C()): | 2304 | | - reveal_type(y) # N: Revealed type is "__main__.B" | 2305 | | -else: | 2306 | | - reveal_type(y) # N: Revealed type is "__main__.B | None" | | 2301 | +def f(y: B | None): | | 2302 | + if y in (B(), C()): | | 2303 | + reveal_type(y) # N: Revealed type is "__main__.B" | | 2304 | + else: | | 2305 | + reveal_type(y) # N: Revealed type is "__main__.B | None" | 2307 | 2306 | [builtins fixtures/tuple.pyi] | 2308 | | -[out] | 2309 | 2307 |
| 2310 | 2308 | [case testNarrowTypeAfterInNamedTuple] | 2311 | 2309 | # flags: --warn-unreachable | |
Collapse file Copy file name to clipboardExpand all lines: test-data/unit/check-narrowing.test +64-1Lines changed: 64 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -3201,7 +3201,7 @@ class X: | 3201 | 3201 | [builtins fixtures/dict.pyi] | 3202 | 3202 |
| 3203 | 3203 |
| 3204 | | -[case testTypeNarrowingStringInLiteralContainer] | | 3204 | +[case testNarrowStringInLiteralContainer] | 3205 | 3205 | # flags: --strict-equality --warn-unreachable | 3206 | 3206 | from typing import Literal | 3207 | 3207 |
| @@ -3235,6 +3235,69 @@ def narrow_set(x: str, t: set[Literal['a', 'b']]): | 3235 | 3235 | reveal_type(x) # N: Revealed type is "builtins.str" | 3236 | 3236 | [builtins fixtures/primitives.pyi] | 3237 | 3237 |
| | 3238 | +[case testNarrowLiteralInLiteralContainer] | | 3239 | +# flags: --strict-equality --warn-unreachable | | 3240 | +from typing import Literal | | 3241 | + | | 3242 | +def narrow_tuple_exact(x: Literal['a', 'b', 'c'], t: tuple[Literal['a'], Literal['b']]): | | 3243 | + if x in t: | | 3244 | + reveal_type(x) # N: Revealed type is "Literal['a'] | Literal['b']" | | 3245 | + else: | | 3246 | + reveal_type(x) # N: Revealed type is "Literal['c']" | | 3247 | + | | 3248 | + if x not in t: | | 3249 | + reveal_type(x) # N: Revealed type is "Literal['c']" | | 3250 | + else: | | 3251 | + reveal_type(x) # N: Revealed type is "Literal['a'] | Literal['b']" | | 3252 | + | | 3253 | +def narrow_tuple_expression(x: Literal['a', 'b', 'c']): | | 3254 | + # TODO: this should match narrow_tuple_exact | | 3255 | + if x in ('a', 'b'): | | 3256 | + reveal_type(x) # N: Revealed type is "Literal['a'] | Literal['b']" | | 3257 | + else: | | 3258 | + reveal_type(x) # N: Revealed type is "Literal['a'] | Literal['b'] | Literal['c']" | | 3259 | + | | 3260 | + if x not in ('a', 'b'): | | 3261 | + reveal_type(x) # N: Revealed type is "Literal['a'] | Literal['b'] | Literal['c']" | | 3262 | + else: | | 3263 | + reveal_type(x) # N: Revealed type is "Literal['a'] | Literal['b']" | | 3264 | + | | 3265 | +def narrow_tuple_union(x: Literal['a', 'b', 'c'], t: tuple[Literal['a', 'b']]): | | 3266 | + if x in t: | | 3267 | + reveal_type(x) # N: Revealed type is "Literal['a'] | Literal['b']" | | 3268 | + else: | | 3269 | + reveal_type(x) # N: Revealed type is "Literal['a'] | Literal['b'] | Literal['c']" | | 3270 | + | | 3271 | + if x not in t: | | 3272 | + reveal_type(x) # N: Revealed type is "Literal['a'] | Literal['b'] | Literal['c']" | | 3273 | + else: | | 3274 | + reveal_type(x) # N: Revealed type is "Literal['a'] | Literal['b']" | | 3275 | + | | 3276 | +def narrow_tuple_with_other_type(x: Literal['a', 'b', 'c'], t: tuple[Literal['a'], int]): | | 3277 | + if x in t: | | 3278 | + reveal_type(x) # N: Revealed type is "Literal['a']" | | 3279 | + else: | | 3280 | + reveal_type(x) # N: Revealed type is "Literal['b'] | Literal['c']" | | 3281 | + | | 3282 | +def narrow_homo_tuple(x: Literal['a', 'b', 'c'], t: tuple[Literal['a', 'b'], ...]): | | 3283 | + if x in t: | | 3284 | + reveal_type(x) # N: Revealed type is "Literal['a'] | Literal['b']" | | 3285 | + else: | | 3286 | + reveal_type(x) # N: Revealed type is "Literal['a'] | Literal['b'] | Literal['c']" | | 3287 | + | | 3288 | +def narrow_list(x: Literal['a', 'b', 'c'], t: list[Literal['a', 'b']]): | | 3289 | + if x in t: | | 3290 | + reveal_type(x) # N: Revealed type is "Literal['a'] | Literal['b']" | | 3291 | + else: | | 3292 | + reveal_type(x) # N: Revealed type is "Literal['a'] | Literal['b'] | Literal['c']" | | 3293 | + | | 3294 | +def narrow_set(x: Literal['a', 'b', 'c'], t: set[Literal['a', 'b']]): | | 3295 | + if x in t: | | 3296 | + reveal_type(x) # N: Revealed type is "Literal['a'] | Literal['b']" | | 3297 | + else: | | 3298 | + reveal_type(x) # N: Revealed type is "Literal['a'] | Literal['b'] | Literal['c']" | | 3299 | +[builtins fixtures/primitives.pyi] | | 3300 | + | 3238 | 3301 |
| 3239 | 3302 | [case testNarrowingLiteralInLiteralContainer] | 3240 | 3303 | # flags: --strict-equality --warn-unreachable | |
Collapse file Copy file name to clipboardExpand all lines: test-data/unit/check-typevar-tuple.test +4-5Lines changed: 4 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -2145,14 +2145,13 @@ match(b) # E: Argument 1 to "match" has incompatible type "Bad"; expected "PC[U | 2145 | 2145 | [builtins fixtures/tuple.pyi] | 2146 | 2146 |
| 2147 | 2147 | [case testVariadicTupleCollectionCheck] | 2148 | | -from typing import Tuple, Optional | 2149 | 2148 | from typing_extensions import Unpack | 2150 | 2149 |
| 2151 | | -allowed: Tuple[int, Unpack[Tuple[int, ...]]] | | 2150 | +allowed: tuple[int, Unpack[tuple[int, ...]]] | 2152 | 2151 |
| 2153 | | -x: Optional[int] | 2154 | | -if x in allowed: | 2155 | | - reveal_type(x) # N: Revealed type is "builtins.int" | | 2152 | +def f(x: int | None): | | 2153 | + if x in allowed: | | 2154 | + reveal_type(x) # N: Revealed type is "builtins.int" | 2156 | 2155 | [builtins fixtures/tuple.pyi] | 2157 | 2156 |
| 2158 | 2157 | [case testJoinOfVariadicTupleCallablesNoCrash] | |
|