Skip to content

Commit 4a552d5

Browse files
committed
avoid narrowing builtins
1 parent 4dbd107 commit 4a552d5

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

mypy/checker.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6545,6 +6545,9 @@ def comparison_type_narrowing_helper(self, node: ComparisonExpr) -> tuple[TypeMa
65456545
if (
65466546
literal(expr) == LITERAL_TYPE
65476547
and not is_literal_none(expr)
6548+
and not is_literal_not_implemented(expr)
6549+
and not is_false_literal(expr)
6550+
and not is_true_literal(expr)
65486551
and not self.is_literal_enum(expr)
65496552
):
65506553
h = literal_hash(expr)
@@ -6584,7 +6587,7 @@ def comparison_type_narrowing_helper(self, node: ComparisonExpr) -> tuple[TypeMa
65846587
operands,
65856588
operand_types,
65866589
expr_indices,
6587-
narrowable_operand_index_to_hash,
6590+
narrowable_indices=narrowable_operand_index_to_hash.keys(),
65886591
)
65896592
elif operator in {"in", "not in"}:
65906593
assert len(expr_indices) == 2
@@ -6649,7 +6652,7 @@ def equality_type_narrowing_helper(
66496652
operands: list[Expression],
66506653
operand_types: list[Type],
66516654
expr_indices: list[int],
6652-
narrowable_operand_index_to_hash: dict[int, tuple[Key, ...]],
6655+
narrowable_indices: AbstractSet[int],
66536656
) -> tuple[TypeMap, TypeMap]:
66546657
"""Calculate type maps for '==', '!=', 'is' or 'is not' expression."""
66556658
# If we haven't been able to narrow types yet, we might be dealing with a
@@ -6659,7 +6662,7 @@ def equality_type_narrowing_helper(
66596662
operands,
66606663
operand_types,
66616664
expr_indices,
6662-
narrowable_operand_index_to_hash.keys(),
6665+
narrowable_indices=narrowable_indices,
66636666
)
66646667
if node is not None:
66656668
type_if_map, type_else_map = self.find_type_equals_check(node, expr_indices)

test-data/unit/check-narrowing.test

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2712,3 +2712,30 @@ reveal_type(t.foo) # N: Revealed type is "__main__.D"
27122712
t.foo = C1()
27132713
reveal_type(t.foo) # N: Revealed type is "__main__.C"
27142714
[builtins fixtures/property.pyi]
2715+
2716+
[case testNarrowingNotImplemented]
2717+
from __future__ import annotations
2718+
from typing_extensions import Self
2719+
2720+
class X:
2721+
def __divmod__(self, other: Self | int) -> tuple[Self, Self]: ...
2722+
2723+
def __floordiv__(self, other: Self | int) -> Self:
2724+
qr = self.__divmod__(other)
2725+
if qr is NotImplemented:
2726+
return NotImplemented
2727+
return qr[0]
2728+
[builtins fixtures/notimplemented.pyi]
2729+
2730+
2731+
[case testNarrowingBooleans]
2732+
# flags: --warn-return-any
2733+
from typing import Any
2734+
2735+
def foo(x: dict[str, Any]) -> bool:
2736+
if x.get("event") is False:
2737+
return False
2738+
if x.get("event") is True:
2739+
return True
2740+
raise
2741+
[builtins fixtures/dict.pyi]

test-data/unit/fixtures/notimplemented.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class str: pass
1212
class dict: pass
1313
class tuple: pass
1414
class ellipsis: pass
15+
class list: pass
1516

1617
import sys
1718

0 commit comments

Comments
 (0)