[New-bugs-announce] [issue25412] __floordiv__ in module fraction fails with TypeError instead of returning NotImplemented

Sergey Shashkov report at bugs.python.org
Thu Oct 15 16:09:54 CEST 2015


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 <report at bugs.python.org>
<http://bugs.python.org/issue25412>
_______________________________________


More information about the New-bugs-announce mailing list