@@ -793,14 +793,34 @@ def _numeric_binary_method( |
793 | 793 | Tries to find the minimum cost conversion of either the LHS or the RHS, by finding all methods with either |
794 | 794 | the LHS or the RHS as exactly the right type and then upcasting the other to that type. |
795 | 795 | """ |
| 796 | + from .conversion import ( # noqa: PLC0415 |
| 797 | + ConvertError, |
| 798 | + convert_to_same_type, |
| 799 | + min_binary_conversion, |
| 800 | + resolve_type, |
| 801 | + ) |
| 802 | + |
| 803 | + # 1. switch if reversed method |
| 804 | + if r_method: |
| 805 | + self, other = other, self |
796 | 806 | # First check if we have a preserved method for this: |
797 | 807 | if isinstance(self, RuntimeExpr) and ( |
798 | | - (preserved_method := self.__egg_class_decl__.preserved_methods.get(method_name)) is not None |
| 808 | + (preserved_method := self.__egg_class_decl__.preserved_methods.get(name)) is not None |
799 | 809 | ): |
800 | 810 | return preserved_method.__get__(self)(other) |
801 | | - # 1. switch if reversed method |
802 | | - if r_method: |
803 | | - self, other = other, self |
| 811 | + # Then check if the self is a Python type and the other side has a preserved method |
| 812 | + if ( |
| 813 | + not isinstance(self, RuntimeExpr) |
| 814 | + and isinstance(other, RuntimeExpr) |
| 815 | + and ((preserved_method := other.__egg_class_decl__.preserved_methods.get(name)) is not None) |
| 816 | + ): |
| 817 | + try: |
| 818 | + new_self = convert_to_same_type(self, other) |
| 819 | + except ConvertError: |
| 820 | + pass |
| 821 | + else: |
| 822 | + return preserved_method.__get__(new_self)(other) |
| 823 | + |
804 | 824 | # If the types don't exactly match to start, then we need to try converting one of them, by finding the cheapest conversion |
805 | 825 | if not ( |
806 | 826 | isinstance(self, RuntimeExpr) |
@@ -811,8 +831,6 @@ def _numeric_binary_method( |
811 | 831 | ) |
812 | 832 | ) |
813 | 833 | ): |
814 | | - from .conversion import min_binary_conversion, resolve_type # noqa: PLC0415 |
815 | | - |
816 | 834 | best_method = min_binary_conversion(name, resolve_type(self), resolve_type(other)) |
817 | 835 |
|
818 | 836 | if not best_method: |
|