Skip to content

Conversion to Boolean with fractions.Fraction and numpy integers #15277

@francois-durand

Description

@francois-durand

When defining a Fraction based on a numpy array of integers, the numerator is a numpy integer, which causes problems when converting to bool.

Reproducing code example:

import numpy as np
from fractions import Fraction
numerators = np.array([4, 3])
my_fraction = Fraction(numerators[0], 100)
print(repr(my_fraction))
print(bool(my_fraction))

In this example my_fraction is represented as Fraction(4, 100), and one would expect that it converts to True; but it raises an error instead.

Error message:

Traceback (most recent call last):
File "main.py", line 6, in
print(bool(my_fraction))
TypeError: bool should return bool, returned numpy.bool_

Numpy/Python version information:

Numpy 1.17.2
Python 3.7.4

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions