Skip to content

np.around fails when using doctest #19759

@Illviljan

Description

@Illviljan

The docstring for np.around fails when using doctest.

Reproducing code example:

The docstring example is taken straight from:

Examples
--------
>>> np.around([0.37, 1.64])
array([0., 2.])
>>> np.around([0.37, 1.64], decimals=1)
array([0.4, 1.6])
>>> np.around([.5, 1.5, 2.5, 3.5, 4.5]) # rounds to nearest even value
array([0., 2., 2., 4., 4.])
>>> np.around([1,2,3,11], decimals=1) # ndarray of ints is returned
array([ 1, 2, 3, 11])
>>> np.around([1,2,3,11], decimals=-1)
array([ 0, 0, 0, 10])
"""

import numpy as np


def test():
    """
    Examples
    --------
    >>> np.around([0.37, 1.64])
    array([0.,  2.])
    >>> np.around([0.37, 1.64], decimals=1)
    array([0.4,  1.6])
    >>> np.around([.5, 1.5, 2.5, 3.5, 4.5]) # rounds to nearest even value
    array([0.,  2.,  2.,  4.,  4.])
    >>> np.around([1,2,3,11], decimals=1) # ndarray of ints is returned
    array([ 1,  2,  3, 11])
    >>> np.around([1,2,3,11], decimals=-1)
    array([ 0,  0,  0, 10])
    """
    return 0

if __name__ == "__main__":
    import doctest

    doctest.testmod()

Error message:

**********************************************************************
File "G:\Program\Dropbox\Python\untitled0.py", line 105, in __main__.test
Failed example:
    np.around([0.37, 1.64])
Expected:
    array([0.,  2.])
Got:
    array([0., 2.])
**********************************************************************
File "G:\Program\Dropbox\Python\untitled0.py", line 107, in __main__.test
Failed example:
    np.around([0.37, 1.64], decimals=1)
Expected:
    array([0.4,  1.6])
Got:
    array([0.4, 1.6])
**********************************************************************
File "G:\Program\Dropbox\Python\untitled0.py", line 109, in __main__.test
Failed example:
    np.around([.5, 1.5, 2.5, 3.5, 4.5]) # rounds to nearest even value
Expected:
    array([0.,  2.,  2.,  4.,  4.])
Got:
    array([0., 2., 2., 4., 4.])
**********************************************************************
1 items had failures:
   3 of   5 in __main__.test
***Test Failed*** 3 failures.

Possible solution:

For this particular case switch to this:

    """
    Examples
    --------
    >>> np.around([0.37, 1.64])
    array([0., 2.])
    >>> np.around([0.37, 1.64], decimals=1)
    array([0.4, 1.6])
    >>> np.around([.5, 1.5, 2.5, 3.5, 4.5]) # rounds to nearest even value
    array([0., 2., 2., 4., 4.])
    >>> np.around([1,2,3,11], decimals=1) # ndarray of ints is returned
    array([ 1,  2,  3, 11])
    >>> np.around([1,2,3,11], decimals=-1)
    array([ 0,  0,  0, 10])

    """

To avoid this happening again it would be nice if the CI ran doctest.

If the amount of failing docstrings seems insurmountable you can try out https://github.com/max-sixty/pytest-accept

NumPy/Python version information:

1.20.3 3.8.8 | packaged by conda-forge | (default, Feb 20 2021, 15:50:08) [MSC v.1916 64 bit (AMD64)]

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