Skip to content

DOC: doctest ReST sources in doc/source/reference #29547

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .spin/cmds.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ def check_tutorials(*, parent_callback, pytest_args, **kwargs):
# - `spin check-tutorials path/to/rst`, and
# - `spin check-tutorials path/to/rst -- --durations=3`
if (not pytest_args) or all(arg.startswith('-') for arg in pytest_args):
pytest_args = ('doc/source/user',) + pytest_args
pytest_args = ('doc/source/user', 'doc/source/reference/') + pytest_args

# make all paths relative to the numpy source folder
pytest_args = tuple(
Expand Down
2 changes: 1 addition & 1 deletion doc/source/reference/arrays.classes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,7 @@ iterating. The ndenumerate iterator can achieve this.
>>> import numpy as np
>>> for i, val in np.ndenumerate(a):
... if sum(i)%5 == 0:
print(i, val)
... print(i, val)
(0, 0, 0) 10
(1, 1, 3) 25
(2, 0, 3) 29
Expand Down
10 changes: 5 additions & 5 deletions doc/source/reference/arrays.datetime.rst
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,12 @@ returns the current UTC date with day precision.
NAT (not a time):

>>> np.datetime64('nat')
np.datetime64('NaT')
np.datetime64('NaT','generic')

The current date:

>>> np.datetime64('today')
np.datetime64('2025-08-05') # result will depend on the current date
np.datetime64('2025-08-05') # may vary: result will depend on the current date

When creating an array of datetimes from a string, it is still possible
to automatically select the unit from the inputs, by using the
Expand Down Expand Up @@ -232,7 +232,7 @@ simple datetime calculations.
np.timedelta64('NaT','D')

>>> np.datetime64('2009-01-01') + np.timedelta64('nat')
np.datetime64('NaT')
np.datetime64('NaT','D')

There are two Timedelta units ('Y', years and 'M', months) which are treated
specially, because how much time they represent changes depending
Expand All @@ -253,7 +253,7 @@ calculating the averaged values from the 400 year leap-year cycle.
>>> a = np.timedelta64(1, 'Y')

>>> np.timedelta64(a, 'M')
numpy.timedelta64(12,'M')
np.timedelta64(12,'M')

>>> np.timedelta64(a, 'D')
Traceback (most recent call last):
Expand Down Expand Up @@ -548,7 +548,7 @@ given below.
>>> a = np.datetime64("0000-01-01", "us")
>>> b = np.datetime64("1600-01-01", "us")
>>> b - a
numpy.timedelta64(50491123200000000,'us')
np.timedelta64(50491123200000000,'us')

The computed results, `50491123200` seconds, are obtained as the elapsed
number of days (`584388`) times `86400` seconds; this is the number of
Expand Down
2 changes: 1 addition & 1 deletion doc/source/reference/arrays.ndarray.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ objects implementing the :class:`memoryview` or :ref:`array

>>> # The element of x in the *second* row, *third* column, namely, 6.
>>> x[1, 2]
6
np.int32(6)

For example :ref:`slicing <arrays.indexing>` can produce views of
the array:
Expand Down
28 changes: 23 additions & 5 deletions doc/source/reference/arrays.promotion.rst
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,20 @@ cannot be coerced to the NumPy dtype, an error is raised:
Traceback (most recent call last):
...
OverflowError: Python int too large to convert to C long

For floating-point values, however, NumPy returns an infinity and emits a warning:

>>> np.float32(1) + 1e300
Traceback (most recent call last):
...
RuntimeWarning: overflow encountered in cast
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm, this seems a tad annoying? Can we change warning settings for doctests to not raise? I think just printing the warning as it was is nicer, because that is what users see!


>>> import warnings
>>> with warnings.catch_warnings():
... warnings.simplefilter('ignore')
... x = np.float32(1) + 1e300
>>> x
np.float32(inf)
... RuntimeWarning: overflow encountered in cast

Second, since the Python float or integer precision is always ignored, a low
precision NumPy scalar will keep using its lower precision unless explicitly
Expand All @@ -125,12 +136,19 @@ converted to a higher precision NumPy dtype or Python scalar (e.g. via ``int()``
some calculations or lead to incorrect results, especially in the case of integer
overflows:

>>> np.int8(100) + 100 # the result exceeds the capacity of int8
>>> import warnings
>>> with warnings.catch_warnings():
... warnings.simplefilter('ignore')
... x = np.int8(100) + 100 # the result exceeds the capacity of int8
>>> x
np.int8(-56)
... RuntimeWarning: overflow encountered in scalar add

Note that NumPy warns when overflows occur for scalars, but not for arrays;
e.g., ``np.array(100, dtype="uint8") + 100`` will *not* warn.

Note that NumPy warns when overflows occur for scalars, but not for arrays:

>>> np.array(100, dtype="int8") + 100
np.int8(-56) # overflows, does not warn


Numerical promotion
-------------------
Expand Down
6 changes: 5 additions & 1 deletion doc/source/reference/constants.rst
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,11 @@ NumPy includes several constants:
>>> import numpy as np
>>> np.inf
inf
>>> np.array([1]) / 0.
>>> import warnings
>>> with warnings.catch_warnings():
... warnings.simplefilter('ignore')
... x = np.array([1]) / 0. # emits a 'Division by zero' warning
>>> x
array([inf])


Expand Down
3 changes: 3 additions & 0 deletions numpy/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,9 @@ def warnings_errors_and_rng(test=None):
'basics.dispatch.rst': 'errors out in /testing/overrides.py',
'basics.subclassing.rst': '.. testcode:: admonitions not understood',
'misc.rst': 'manipulates warnings',
'arrays.nditer.cython.rst': 'uses non-python syntax',
'distutils.rst': '',
'coremath.rst': 'numpy.distutils does not exis'
}

# ignores are for things fail doctest collection (optionals etc)
Expand Down