@@ -1697,7 +1697,7 @@ def visit_op_expr(self, e: OpExpr) -> Type:
16971697
16981698 if e .op in nodes .op_methods :
16991699 method = self .get_operator_method (e .op )
1700- result , method_type = self .check_op (method , left_type , e .right , e ,
1700+ result , method_type = self .check_op (method , e . left , left_type , e .right , e ,
17011701 allow_reverse = True )
17021702 e .method_type = method_type
17031703 return result
@@ -1749,7 +1749,7 @@ def visit_comparison_expr(self, e: ComparisonExpr) -> Type:
17491749 sub_result = self .bool_type ()
17501750 elif operator in nodes .op_methods :
17511751 method = self .get_operator_method (operator )
1752- sub_result , method_type = self .check_op (method , left_type , right , e ,
1752+ sub_result , method_type = self .check_op (method , left , left_type , right , e ,
17531753 allow_reverse = True )
17541754
17551755 elif operator == 'is' or operator == 'is not' :
@@ -1820,19 +1820,12 @@ def check_op_reversible(self,
18201820 left_expr : Expression ,
18211821 right_type : Type ,
18221822 right_expr : Expression ,
1823- context : Context ) -> Tuple [Type , Type ]:
1824- # Note: this kludge exists mostly to maintain compatibility with
1825- # existing error messages. Apparently, if the left-hand-side is a
1826- # union and we have a type mismatch, we print out a special,
1827- # abbreviated error message. (See messages.unsupported_operand_types).
1828- unions_present = isinstance (left_type , UnionType )
1829-
1823+ context : Context ,
1824+ msg : MessageBuilder ) -> Tuple [Type , Type ]:
18301825 def make_local_errors () -> MessageBuilder :
18311826 """Creates a new MessageBuilder object."""
1832- local_errors = self . msg .clean_copy ()
1827+ local_errors = msg .clean_copy ()
18331828 local_errors .disable_count = 0
1834- if unions_present :
1835- local_errors .disable_type_names += 1
18361829 return local_errors
18371830
18381831 def lookup_operator (op_name : str , base_type : Type ) -> Optional [Type ]:
@@ -2009,9 +2002,9 @@ def lookup_definer(typ: Instance, attr_name: str) -> Optional[str]:
20092002 # TODO: Remove this extra case
20102003 return result
20112004
2012- self . msg .add_errors (errors [0 ])
2005+ msg .add_errors (errors [0 ])
20132006 if warn_about_uncalled_reverse_operator :
2014- self . msg .reverse_operator_method_never_called (
2007+ msg .reverse_operator_method_never_called (
20152008 nodes .op_methods_to_symbols [op_name ],
20162009 op_name ,
20172010 right_type ,
@@ -2025,22 +2018,57 @@ def lookup_definer(typ: Instance, attr_name: str) -> Optional[str]:
20252018 result = error_any , error_any
20262019 return result
20272020
2028- def check_op (self , method : str , base_type : Type , arg : Expression ,
2029- context : Context ,
2021+ def check_op (self , method : str , base_expr : Expression , base_type : Type ,
2022+ arg : Expression , context : Context ,
20302023 allow_reverse : bool = False ) -> Tuple [Type , Type ]:
20312024 """Type check a binary operation which maps to a method call.
20322025
20332026 Return tuple (result type, inferred operator method type).
20342027 """
20352028
20362029 if allow_reverse :
2037- return self .check_op_reversible (
2038- op_name = method ,
2039- left_type = base_type ,
2040- left_expr = TempNode (base_type ),
2041- right_type = self .accept (arg ),
2042- right_expr = arg ,
2043- context = context )
2030+ # Note: We want to pass in the original 'base_expr' and 'arg' for
2031+ # 'left_expr' and 'right_expr' whenever possible so that plugins
2032+ # and similar things can introspect on the original node if possible.
2033+ left_variants = [(base_type , base_expr )]
2034+ if isinstance (base_type , UnionType ):
2035+ left_variants = [(item , TempNode (item )) for item in base_type .relevant_items ()]
2036+
2037+ right_type = self .accept (arg )
2038+ right_variants = [(right_type , arg )]
2039+ if isinstance (right_type , UnionType ):
2040+ right_variants = [(item , TempNode (item )) for item in right_type .relevant_items ()]
2041+
2042+ msg = self .msg .clean_copy ()
2043+ msg .disable_count = 0
2044+ all_results = []
2045+ all_inferred = []
2046+
2047+ for left_possible_type , left_expr in left_variants :
2048+ for right_possible_type , right_expr in right_variants :
2049+ result , inferred = self .check_op_reversible (
2050+ op_name = method ,
2051+ left_type = left_possible_type ,
2052+ left_expr = left_expr ,
2053+ right_type = right_possible_type ,
2054+ right_expr = right_expr ,
2055+ context = context ,
2056+ msg = msg )
2057+ all_results .append (result )
2058+ all_inferred .append (inferred )
2059+
2060+ if msg .is_errors ():
2061+ self .msg .add_errors (msg )
2062+ if len (left_variants ) >= 2 and len (right_variants ) >= 2 :
2063+ self .msg .warn_both_operands_are_from_unions (context )
2064+ elif len (left_variants ) >= 2 :
2065+ self .msg .warn_operand_was_from_union ("Left" , base_type , context )
2066+ elif len (right_variants ) >= 2 :
2067+ self .msg .warn_operand_was_from_union ("Right" , right_type , context )
2068+
2069+ results_final = UnionType .make_simplified_union (all_results )
2070+ inferred_final = UnionType .make_simplified_union (all_inferred )
2071+ return results_final , inferred_final
20442072 else :
20452073 return self .check_op_local_by_name (
20462074 method = method ,
@@ -2125,7 +2153,7 @@ def check_list_multiply(self, e: OpExpr) -> Type:
21252153 left_type = self .accept (e .left , type_context = self .type_context [- 1 ])
21262154 else :
21272155 left_type = self .accept (e .left )
2128- result , method_type = self .check_op ('__mul__' , left_type , e .right , e )
2156+ result , method_type = self .check_op ('__mul__' , e . left , left_type , e .right , e )
21292157 e .method_type = method_type
21302158 return result
21312159
@@ -2179,7 +2207,7 @@ def visit_index_expr_helper(self, e: IndexExpr) -> Type:
21792207 and left_type .is_type_obj () and left_type .type_object ().is_enum ):
21802208 return self .visit_enum_index_expr (left_type .type_object (), e .index , e )
21812209 else :
2182- result , method_type = self .check_op ('__getitem__' , left_type , e .index , e )
2210+ result , method_type = self .check_op ('__getitem__' , e . base , left_type , e .index , e )
21832211 e .method_type = method_type
21842212 return result
21852213
0 commit comments