8000 Merge pull request #27272 from ev-br/strict_check_docs · numpy/numpy@6ffe3da · GitHub
[go: up one dir, main page]

Skip to content

Commit 6ffe3da

Browse files
authored
Merge pull request #27272 from ev-br/strict_check_docs
ENH: make check-{docs,tutorials} fail on dtype mismatch
2 parents ae926d7 + ccfc91c commit 6ffe3da

File tree

12 files changed

+35
-31
lines changed

12 files changed

+35
-31
lines changed

doc/source/user/basics.rec.rst

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,7 @@ Similarly to tuples, structured scalars can also be indexed with an integer::
535535

536536
>>> scalar = np.array([(1, 2., 3.)], dtype='i, f, f')[0]
537537
>>> scalar[0]
538-
1
538+
np.int32(1)
539539
>>> scalar[1] = 4
540540

541541
Thus, tuples might be thought of as the native Python equivalent to numpy's
@@ -595,7 +595,7 @@ removed::
595595

596596
>>> dt = np.dtype("i1,V3,i4,V1")[["f0", "f2"]]
597597
>>> dt
598-
dtype({'names':['f0','f2'], 'formats':['i1','<i4'], 'offsets':[0,4], 'itemsize':9})
598+
dtype({'names': ['f0', 'f2'], 'formats': ['i1', '<i4'], 'offsets': [0, 4], 'itemsize': 9})
599599
>>> np.result_type(dt)
600600
dtype([('f0', 'i1'), ('f2', '<i4')])
601601

@@ -606,7 +606,8 @@ If a structured dtype is created with ``align=True`` ensuring that
606606

607607
>>> dt = np.dtype("i1,V3,i4,V1", align=True)[["f0", "f2"]]
608608
>>> dt
609-
dtype({'names':['f0','f2'], 'formats':['i1','<i4'], 'offsets':[0,4], 'itemsize':12}, align=True)
609+
dtype({'names': ['f0', 'f2'], 'formats': ['i1', '<i4'], 'offsets': [0, 4], 'itemsize': 12}, align=True)
610+
610611
>>> np.result_type(dt)
611612
dtype([('f0', 'i1'), ('f2', '<i4')], align=True)
612613
>>> np.result_type(dt).isalignedstruct

doc/source/user/basics.types.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ but gives -1486618624 (incorrect) for a 32-bit integer.
314314
>>> np.power(100, 9, dtype=np.int64)
315315
1000000000000000000
316316
>>> np.power(100, 9, dtype=np.int32)
317-
-1486618624
317+
np.int32(-1486618624)
318318

319319
The behaviour of NumPy and Python integer types differs significantly for
320320
integer overflows and may confuse users expecting NumPy integers to behave

doc/source/user/byteswapping.rst

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ there are two integers, and that they are 16 bit and big-endian:
4040
>>> import numpy as np
4141
>>> big_end_arr = np.ndarray(shape=(2,),dtype='>i2', buffer=big_end_buffer)
4242
>>> big_end_arr[0]
43-
1
43+
np.int16(1)
4444
>>> big_end_arr[1]
45-
770
45+
np.int16(770)
4646

4747
Note the array ``dtype`` above of ``>i2``. The ``>`` means 'big-endian'
4848
(``<`` is little-endian) and ``i2`` means 'signed 2-byte integer'. For
@@ -99,14 +99,14 @@ We make something where they don't match:
9999

100100
>>> wrong_end_dtype_arr = np.ndarray(shape=(2,),dtype='<i2', buffer=big_end_buffer)
101101
>>> wrong_end_dtype_arr[0]
102-
256
102+
np.int16(256)
103103

104104
The obvious fix for this situation is to change the dtype so it gives
105105
the correct endianness:
106106

107107
>>> fixed_end_dtype_arr = wrong_end_dtype_arr.view(np.dtype('<i2').newbyteorder())
108108
>>> fixed_end_dtype_arr[0]
109-
1
109+
np.int16(1)
110110

111111
Note the array has not changed in memory:
112112

@@ -122,7 +122,7 @@ that needs a certain byte ordering.
122122

123123
>>> fixed_end_mem_arr = wrong_end_dtype_arr.byteswap()
124124
>>> fixed_end_mem_arr[0]
125-
1
125+
np.int16(1)
126126

127127
Now the array *has* changed in memory:
128128

@@ -140,7 +140,7 @@ the previous operations:
140140
>>> swapped_end_arr = big_end_arr.byteswap()
141141
>>> swapped_end_arr = swapped_end_arr.view(swapped_end_arr.dtype.newbyteorder())
142142
>>> swapped_end_arr[0]
143-
1
143+
np.int16(1)
144144
>>> swapped_end_arr.tobytes() == big_end_buffer
145145
False
146146

@@ -149,7 +149,7 @@ can be achieved with the ndarray astype method:
149149

150150
>>> swapped_end_arr = big_end_arr.astype('<i2')
151151
>>> swapped_end_arr[0]
152-
1
152+
np.int16(1)
153153
>>> swapped_end_arr.tobytes() == big_end_buffer
154154
False
155155

numpy/_core/_add_newdocs.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -919,7 +919,7 @@
919919
920920
>>> x = np.array([(1,2),(3,4)],dtype=[('a','<i4'),('b','<i4')])
921921
>>> x['a']
922-
array([1, 3])
922+
array([1, 3], dtype=int32)
923923
924924
Creating an array from sub-classes:
925925
@@ -3350,7 +3350,7 @@
33503350
array([1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0,
33513351
0, 0], dtype=uint8)
33523352
>>> A.view(A.dtype.newbyteorder()).byteswap(inplace=True)
3353-
array([1, 2, 3])
3353+
array([1, 2, 3], dtype='>i8')
33543354
>>> A.view(np.uint8)
33553355
array([0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0,
33563356
0, 3], dtype=uint8)
@@ -4459,7 +4459,7 @@
44594459
>>> a = np.uint32([1, 2])
44604460
>>> a_list = list(a)
44614461
>>> a_list
4462-
[1, 2]
4462+
[np.uint32(1), np.uint32(2)]
44634463
>>> type(a_list[0])
44644464
<class 'numpy.uint32'>
44654465
>>> a_tolist = a.tolist()

numpy/_core/_ufunc_config.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def seterr(all=None, divide=None, over=None, under=None, invalid=None):
7777
>>> import numpy as np
7878
>>> orig_settings = np.seterr(all='ignore') # seterr to known value
7979
>>> np.int16(32000) * np.int16(3)
80-
30464
80+
np.int16(30464)
8181
>>> np.seterr(over='raise')
8282
{'divide': 'ignore', 'over': 'ignore', 'under': 'ignore', 'invalid': 'ignore'}
8383
>>> old_settings = np.seterr(all='warn', over='raise')
@@ -90,7 +90,7 @@ def seterr(all=None, divide=None, over=None, under=None, invalid=None):
9090
>>> np.geterr()
9191
{'divide': 'print', 'over': 'print', 'under': 'print', 'invalid': 'print'}
9292
>>> np.int16(32000) * np.int16(3)
93-
30464
93+
np.int16(30464)
9494
>>> np.seterr(**orig_settings) # restore original
9595
{'divide': 'print', 'over': 'print', 'under': 'print', 'invalid': 'print'}
9696

numpy/_core/code_generators/ufunc_docstrings.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,7 @@ def add_newdoc(place, name, doc):
703703
array([ 6, 5, 255])
704704
>>> np.bitwise_or(np.array([2, 5, 255, 2147483647], dtype=np.int32),
705705
... np.array([4, 4, 4, 2147483647], dtype=np.int32))
706-
array([ 6, 5, 255, 2147483647])
706+
array([ 6, 5, 255, 2147483647], dtype=int32)
707707
>>> np.bitwise_or([True, True], [False, True])
708708
array([ True, True])
709709
@@ -1679,15 +1679,15 @@ def add_newdoc(place, name, doc):
16791679
16801680
>>> x = np.invert(np.array(13, dtype=np.uint8))
16811681
>>> x
1682-
242
1682+
np.uint8(242)
16831683
>>> np.binary_repr(x, width=8)
16841684
'11110010'
16851685
16861686
The result depends on the bit-width:
16871687
16881688
>>> x = np.invert(np.array(13, dtype=np.uint16))
16891689
>>> x
1690-
65522
1690+
np.uint16(65522)
16911691
>>> np.binary_repr(x, width=16)
16921692
'1111111111110010'
16931693
@@ -2683,7 +2683,7 @@ def add_newdoc(place, name, doc):
26832683
--------
26842684
>>> import numpy as np
26852685
>>> np.fmax([2, 3, 4], [1, 5, 2])
2686-
array([ 2., 5., 4.])
2686+
array([ 2, 5, 4])
26872687
26882688
>>> np.fmax(np.eye(2), [0.5, 2])
26892689
array([[ 1. , 2. ],
@@ -4263,7 +4263,7 @@ def add_newdoc(place, name, doc):
42634263
array([ 0. , 0.5 , 0.5 , 0.75 , 0.5 , 0.625, 0.75 , 0.875,
42644264
0.5 ])
42654265
>>> y2
4266-
array([0, 1, 2, 2, 3, 3, 3, 3, 4])
4266+
array([0, 1, 2, 2, 3, 3, 3, 3, 4], dtype=int32)
42674267
>>> y1 * 2**y2
42684268
array([ 0., 1., 2., 3., 4., 5., 6., 7., 8.])
42694269
@@ -4411,7 +4411,7 @@ def add_newdoc(place, name, doc):
44114411
--------
44124412
>>> import numpy as np
44134413
>>> np.bitwise_count(1023)
4414-
10
4414+
np.uint8(10)
44154415
>>> a = np.array([2**i - 1 for i in range(16)])
44164416
>>> np.bitwise_count(a)
44174417
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],

numpy/_core/fromnumeric.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2452,7 +2452,7 @@ def sum(a, axis=None, dtype=None, out=None, keepdims=np._NoValue,
24522452
>>> np.sum([0.5, 1.5])
24532453
2.0
24542454
>>> np.sum([0.5, 0.7, 0.2, 1.5], dtype=np.int32)
2455-
1
2455+
np.int32(1)
24562456
>>> np.sum([[0, 1], [0, 5]])
24572457
6
24582458
>>> np.sum([[0, 1], [0, 5]], axis=0)
@@ -2465,7 +2465,7 @@ def sum(a, axis=None, dtype=None, out=None, keepdims=np._NoValue,
24652465
If the accumulator is too small, overflow occurs:
24662466
24672467
>>> np.ones(128, dtype=np.int8).sum(dtype=np.int8)
2468-
-128
2468+
np.int8(-128)
24692469
24702470
You can also start the sum with a value other than zero:
24712471
@@ -3877,7 +3877,7 @@ def mean(a, axis=None, dtype=None, out=None, keepdims=np._NoValue, *,
38773877
>>> a[0, :] = 1.0
38783878
>>> a[1, :] = 0.1
38793879
>>> np.mean(a)
3880-
0.54999924
3880+
np.float32(0.54999924)
38813881
38823882
Computing the mean in float64 is more accurate:
38833883
@@ -4064,7 +4064,7 @@ def std(a, axis=None, dtype=None, out=None, ddof=0, keepdims=np._NoValue, *,
40644064
>>> a[0, :] = 1.0
40654065
>>> a[1, :] = 0.1
40664066
>>> np.std(a)
4067-
0.45000005
4067+
np.float32(0.45000005)
40684068
40694069
Computing the standard deviation in float64 is more accurate:
40704070
@@ -4267,7 +4267,7 @@ def var(a, axis=None, dtype=None, out=None, ddof=0, keepdims=np._NoValue, *,
42674267
>>> a[0, :] = 1.0
42684268
>>> a[1, :] = 0.1
42694269
>>> np.var(a)
4270-
0.20250003
4270+
np.float32(0.20250003)
42714271
42724272
Computing the variance in float64 is more accurate:
42734273

numpy/conftest.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,9 @@ def warnings_errors_and_rng(test=None):
211211
dt_config.rndm_markers.add('#uninitialized')
212212
dt_config.rndm_markers.add('# uninitialized')
213213

214+
# make the checker pick on mismatched dtypes
215+
dt_config.strict_check = True
216+
214217
import doctest
215218
dt_config.optionflags = doctest.NORMALIZE_WHITESPACE | doctest.ELLIPSIS
216219

numpy/lib/_function_base_impl.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1439,7 +1439,7 @@ def diff(a, n=1, axis=-1, prepend=np._NoValue, append=np._NoValue):
14391439
>>> np.diff(u8_arr)
14401440
array([255], dtype=uint8)
14411441
>>> u8_arr[1,...] - u8_arr[0,...]
1442-
255
1442+
np.uint8(255)
14431443
14441444
If this is not desirable, then the array should be cast to a larger
14451445
integer type first:

numpy/ma/core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7808,7 +7808,7 @@ def diff(a, /, n=1, axis=-1, prepend=np._NoValue, append=np._NoValue):
78087808
fill_value=np.int64(999999),
78097809
dtype=uint8)
78107810
>>> u8_arr[1,...] - u8_arr[0,...]
7811-
255
7811+
np.uint8(255)
78127812
78137813
If this is not desirable, then the array should be cast to a larger
78147814
integer type first:

numpy/polynomial/polynomial.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1468,7 +1468,7 @@ def polyfit(x, y, deg, rcond=None, full=False, w=None):
14681468
array([-6.73496154e-17, -1.00000000e+00, 0.00000000e+00, 1.00000000e+00])
14691469
>>> stats # note the minuscule SSR
14701470
[array([8.79579319e-31]),
1471-
4,
1471+
np.int32(4),
14721472
array([1.38446749, 1.32119158, 0.50443316, 0.28853036]),
14731473
1.1324274851176597e-14]
14741474

numpy/random/mtrand.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1138,7 +1138,7 @@ cdef class RandomState:
11381138
11391139
>>> x = np.float32(5*0.99999999)
11401140
>>> x
1141-
5.0
1141+
np.float32(5.0)
11421142
11431143
11441144
Examples

0 commit comments

Comments
 (0)
0