@@ -3721,7 +3721,7 @@ def visit_comparison_expr(self, e: ComparisonExpr) -> Type:
37213721 elif operator == "is" or operator == "is not" :
37223722 right_type = self .accept (right ) # validate the right operand
37233723 sub_result = self .bool_type ()
3724- if self .dangerous_comparison (left_type , right_type ):
3724+ if self .dangerous_comparison (left_type , right_type , identity_check = True ):
37253725 # Show the most specific literal types possible
37263726 left_type = try_getting_literal (left_type )
37273727 right_type = try_getting_literal (right_type )
@@ -3763,6 +3763,7 @@ def dangerous_comparison(
37633763 original_container : Type | None = None ,
37643764 seen_types : set [tuple [Type , Type ]] | None = None ,
37653765 prefer_literal : bool = True ,
3766+ identity_check : bool = False ,
37663767 ) -> bool :
37673768 """Check for dangerous non-overlapping comparisons like 42 == 'no'.
37683769
@@ -3790,10 +3791,10 @@ def dangerous_comparison(
37903791
37913792 left , right = get_proper_types ((left , right ))
37923793
3793- # We suppress the error if there is a custom __eq__() method on either
3794- # side. User defined (or even standard library) classes can define this
3794+ # We suppress the error for equality and container checks if there is a custom __eq__()
3795+ # method on either side. User defined (or even standard library) classes can define this
37953796 # to return True for comparisons between non-overlapping types.
3796- if custom_special_method (left , "__eq__" ) or custom_special_method (right , "__eq__" ):
3797+ if ( custom_special_method (left , "__eq__" ) or custom_special_method (right , "__eq__" )) and not identity_check :
37973798 return False
37983799
37993800 if prefer_literal :
@@ -3817,7 +3818,10 @@ def dangerous_comparison(
38173818 #
38183819 # TODO: find a way of disabling the check only for types resulted from the expansion.
38193820 return False
3820- if isinstance (left , NoneType ) and isinstance (right , NoneType ):
3821+ if self .chk .options .strict_equality_for_none :
3822+ if isinstance (left , NoneType ) and isinstance (right , NoneType ):
3823+ return False
3824+ elif isinstance (left , NoneType ) or isinstance (right , NoneType ):
38213825 return False
38223826 if isinstance (left , UnionType ) and isinstance (right , UnionType ):
38233827 left = remove_optional (left )
0 commit comments