Skip to content

Commit 2d231f5

Browse files
Copilotm-aciek
andcommitted
Remove redundant comparison check code in visit_comparison_expr
Co-authored-by: m-aciek <9288014+m-aciek@users.noreply.github.com>
1 parent 1b55f52 commit 2d231f5

3 files changed

Lines changed: 2 additions & 46 deletions

File tree

mypy/checkexpr.py

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3697,12 +3697,6 @@ def visit_comparison_expr(self, e: ComparisonExpr) -> Type:
36973697
)
36983698
e.method_types.append(method_type)
36993699

3700-
# Check for unsafe subtype relationships if enabled
3701-
if not w.has_new_errors() and codes.UNSAFE_SUBTYPE in self.chk.options.enabled_error_codes:
3702-
right_type = self.accept(right)
3703-
if self.has_unsafe_subtype_relationship(left_type, right_type):
3704-
self.msg.unsafe_subtype_comparison(left_type, right_type, operator, e)
3705-
37063700
# Only show dangerous overlap if there are no other errors. See
37073701
# testCustomEqCheckStrictEquality for an example.
37083702
if not w.has_new_errors() and operator in ("==", "!="):
@@ -3881,32 +3875,6 @@ def dangerous_comparison(
38813875
return False
38823876
return not is_overlapping_types(left, right, ignore_promotions=False)
38833877

3884-
def has_unsafe_subtype_relationship(self, left: Type, right: Type) -> bool:
3885-
"""Check if left and right have an unsafe subtyping relationship.
3886-
3887-
Returns True if they are instances with a nominal subclass relationship
3888-
that is known to be unsafe (e.g., datetime and date).
3889-
"""
3890-
from mypy.subtypes import UNSAFE_SUBTYPING_PAIRS
3891-
3892-
left = get_proper_type(left)
3893-
right = get_proper_type(right)
3894-
3895-
if not isinstance(left, Instance) or not isinstance(right, Instance):
3896-
return False
3897-
3898-
left_name = left.type.fullname
3899-
right_name = right.type.fullname
3900-
3901-
# Check if this pair is in our list of known unsafe subtyping relationships
3902-
# Check both directions since we want to catch comparisons either way
3903-
for subclass, superclass in UNSAFE_SUBTYPING_PAIRS:
3904-
if ((left_name == subclass and right_name == superclass)
3905-
or (left_name == superclass and right_name == subclass)):
3906-
return True
3907-
3908-
return False
3909-
39103878
def check_method_call_by_name(
39113879
self,
39123880
method: str,

mypy/messages.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1655,18 +1655,6 @@ def dangerous_comparison(self, left: Type, right: Type, kind: str, ctx: Context)
16551655
code=codes.COMPARISON_OVERLAP,
16561656
)
16571657

1658-
def unsafe_subtype_comparison(
1659-
self, left: Type, right: Type, operator: str, ctx: Context
1660-
) -> None:
1661-
"""Report a comparison between types with an unsafe subtyping relationship.
1662-
1663-
This warns about comparisons where the types have a nominal subclass relationship
1664-
but comparing them can cause runtime errors (e.g., datetime vs date).
1665-
"""
1666-
left_typ, right_typ = format_type_distinctly(left, right, options=self.options)
1667-
message = f"Unsafe comparison between {left_typ} and {right_typ}; runtime comparison may raise TypeError"
1668-
self.fail(message, ctx, code=codes.UNSAFE_SUBTYPE)
1669-
16701658
def overload_inconsistently_applies_decorator(self, decorator: str, context: Context) -> None:
16711659
self.fail(
16721660
f'Overload does not consistently use the "@{decorator}" '

test-data/unit/check-unsafe-subtype.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ if dt < d: # E: Unsupported operand types for < ("datetime" and "date")
1111
if d > dt: # E: Unsupported operand types for < ("datetime" and "date")
1212
pass
1313

14-
if dt == d: # E: Unsafe comparison between "datetime" and "date"; runtime comparison may raise TypeError
14+
if dt == d:
1515
pass
1616

17-
if dt != d: # E: Unsafe comparison between "datetime" and "date"; runtime comparison may raise TypeError
17+
if dt != d:
1818
pass
1919

2020
if dt <= d: # E: Unsupported operand types for <= ("datetime" and "date")

0 commit comments

Comments
 (0)