[issue25412] __floordiv__ in module fraction fails with TypeError instead of returning NotImplemented
Sergey Shashkov added the comment: This patch actually fixes the problem: https://bugs.python.org/issue35588 https://github.com/python/cpython/commit/3a374e0c5abe805667b71ffaaa7614781101ff4c from fractions import Fraction import operator class Goo: __radd__, __rdivmod__, __rfloordiv__, __rmod__, __rmul__, __rpow__, __rsub__, __rtruediv__ = [lambda a, b: 'ok'] * 8 for func in operator.add, operator.sub, operator.mul, operator.truediv, operator.pow, operator.mod, operator.floordiv, divmod: print(func.__name__, func(Fraction(1), Goo())) -- nosy: -mark.dickinson, oscarbenjamin, vstinner pull_requests: +11085 resolution: -> fixed stage: -> patch review status: open -> closed ___ Python tracker <https://bugs.python.org/issue25412> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue25412] __floordiv__ in module fraction fails with TypeError instead of returning NotImplemented
New submission from Sergey Shashkov: __floordiv__ in module fraction fails with TypeError instead of returning NotImplemented when modulo is a class without __rtruediv__ or __mul__. Code sample: class Foo(object): def __rdivmod__(self, other): return 'rdivmod works' from fractions import Fraction a = Fraction(1,1) b = Foo() print(divmod(1, b)) print(divmod(a, b)) __divmod__ in Fraction is inherited from class Real (numbers.py): def __divmod__(self, other): return (self // other, self % other) So __floordiv__ and __mod__ are called. def __floordiv__(a, b): """a // b""" return math.floor(a / b) def __mod__(a, b): """a % b""" div = a // b return a - b * div __floordiv__ if fractions.py makes a true division, and __mod__ makes multiplication. The following code will fix the problem: def __divmod__(self, other): if isinstance(a, numbers.Complex): return (self // other, self % other) else: return NotImplemented -- components: Library (Lib) messages: 253046 nosy: ShashkovS priority: normal severity: normal status: open title: __floordiv__ in module fraction fails with TypeError instead of returning NotImplemented type: behavior versions: Python 2.7, Python 3.2, Python 3.3, Python 3.4, Python 3.5, Python 3.6 ___ Python tracker <http://bugs.python.org/issue25412> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue25412] __floordiv__ in module fraction fails with TypeError instead of returning NotImplemented
Sergey Shashkov added the comment: def __floordiv__(a, b): """a // b""" if isinstance(b, numbers.Complex): return math.floor(a / b) else: return NotImplemented And the same for __mod__. -- ___ Python tracker <http://bugs.python.org/issue25412> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue25412] __floordiv__ in module fraction fails with TypeError instead of returning NotImplemented
Sergey Shashkov added the comment: OK, then we should not change numbers.py. And in fractions.py: def __floordiv__(a, b): """a // b""" if isinstance(b, numbers.Complex) or hasattr(b, '__rtruediv__'): fr = a / b if fr != NotImplemented: return math.floor(a / b) else: return NotImplemented else: return NotImplemented -- ___ Python tracker <http://bugs.python.org/issue25412> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue25412] __floordiv__ in module fraction fails with TypeError instead of returning NotImplemented
Sergey Shashkov added the comment: Bad idea, just def __floordiv__(a, b): """a // b""" if isinstance(b, numbers.Complex): return math.floor(a / b) else: return NotImplemented If b is inherited from number, real, complex, Fraction and etc, then a of type Fraction knows, how do make a division. Otherwise may be b has __rfloordiv__, that khows how to be divided by Fraction. -- ___ Python tracker <http://bugs.python.org/issue25412> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue25412] __floordiv__ in module fraction fails with TypeError instead of returning NotImplemented
Sergey Shashkov added the comment: ... def forward(a, b): if isinstance(b, (int, Fraction)): return monomorphic_operator(a, b) elif isinstance(b, float): return fallback_operator(float(a), b) elif isinstance(b, complex): return fallback_operator(complex(a), b) else: return NotImplemented forward.__name__ = '__' + fallback_operator.__name__ + '__' forward.__doc__ = monomorphic_operator.__doc__ def reverse(b, a): if isinstance(a, numbers.Rational): # Includes ints. return monomorphic_operator(a, b) elif isinstance(a, numbers.Real): return fallback_operator(float(a), float(b)) elif isinstance(a, numbers.Complex): return fallback_operator(complex(a), complex(b)) else: return NotImplemented ... so division is possible only with int, Fraction, float, complex, numbers.Rational, numbers.Real, numbers.Complex. For all of them "isinstance(b, numbers.Complex)" is true -- ___ Python tracker <http://bugs.python.org/issue25412> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue25412] __floordiv__ in module fraction fails with TypeError instead of returning NotImplemented
Changes by Sergey Shashkov : -- hgrepos: +320 ___ Python tracker <http://bugs.python.org/issue25412> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue25412] __floordiv__ in module fraction fails with TypeError instead of returning NotImplemented
Changes by Sergey Shashkov : -- hgrepos: -320 ___ Python tracker <http://bugs.python.org/issue25412> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue25412] __floordiv__ in module fraction fails with TypeError instead of returning NotImplemented
Changes by Sergey Shashkov : -- hgrepos: +321 keywords: +patch Added file: http://bugs.python.org/file40797/fractions_truediv_fix.patch ___ Python tracker <http://bugs.python.org/issue25412> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com