8000 Merge pull request #20201 from BvB93/machar2 · numpy/numpy@d5f6618 · GitHub
[go: up one dir, main page]

Skip to content

Commit d5f6618

Browse files
authored
Merge pull request #20201 from BvB93/machar2
DEP: Deprecate `np.MachAr`
2 parents 9da64d8 + cbc1d01 commit d5f6618

14 files changed

+71
-109
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,3 +220,4 @@ numpy/core/src/umath/loops_arithm_fp.dispatch.c
220220
numpy/core/src/umath/loops_arithmetic.dispatch.c
221221
numpy/core/src/umath/loops_trigonometric.dispatch.c
222222
numpy/core/src/umath/loops_exponent_log.dispatch.c
223+
numpy/core/src/umath/loops_umath_fp.dispatch.c
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
The ``np.MachAr`` class has been deprecated
2+
-------------------------------------------
3+
The `~numpy.MachAr` class and `finfo.machar <numpy.finfo>` attribute have
4+
been deprecated. Users are encouraged to access the property if interest
5+
directly from the corresponding `~numpy.finfo` attribute.

numpy/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,12 +188,19 @@
188188
n: (getattr(_builtins, n), _msg.format(n=n, extended_msg=extended_msg))
189189
for n, extended_msg in _type_info
190190
})
191+
191192
# Numpy 1.20.0, 2020-10-19
192193
__deprecated_attrs__["typeDict"] = (
193194
core.numerictypes.typeDict,
194195
"`np.typeDict` is a deprecated alias for `np.sctypeDict`."
195196
)
196197

198+
# NumPy 1.22, 2021-10-20
199+
__deprecated_attrs__["MachAr"] = (
200+
core._machar.MachAr,
201+
"`np.MachAr` is deprecated (NumPy 1.22)."
202+
)
203+
197204
_msg = (
198205
"`np.{n}` is a deprecated alias for `np.compat.{n}`. "
199206
"To silence this warning, use `np.compat.{n}` by itself. "

numpy/__init__.pyi

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ if sys.version_info >= (3, 9):
1414

1515
from numpy._pytesttester import PytestTester
1616
from numpy.core._internal import _ctypes
17-
from numpy.core.getlimits import MachArLike
1817

1918
from numpy.typing import (
2019
# Arrays
@@ -665,17 +664,6 @@ test: PytestTester
665664
# their annotations are properly implemented
666665
#
667666
# Placeholders for classes
668-
# TODO: Remove `__getattr__` once the classes are stubbed out
669-
class MachAr:
670-
def __init__(
671-
self,
672-
float_conv: Any = ...,
673-
int_conv: Any = ...,
674-
float_to_float: Any = ...,
675-
float_to_str: Any = ...,
676-
title: Any = ...,
677-
) -> None: ...
678-
def __getattr__(self, key: str) -> Any: ...
679667

680668
# Some of these are aliases; others are wrappers with an identical signature
681669
round = around
@@ -3395,14 +3383,6 @@ class finfo(Generic[_FloatType]):
33953383
def smallest_normal(self) -> _FloatType: ...
33963384
@property
33973385
def tiny(self) -> _FloatType: ...
3398-
3399-
# NOTE: Not technically a property, but this is the only way we can
3400-
# access the precision of the underlying float
3401-
@property
3402-
def machar(self: finfo[floating[_NBit1]]) -> MachArLike[_NBit1]: ...
3403-
@machar.setter
3404-
def machar(self: finfo[floating[_NBit1]], value: MachArLike[_NBit1]) -> None: ...
3405-
34063386
@overload
34073387
def __new__(
34083388
cls, dtype: inexact[_NBit1] | _DTypeLike[inexact[_NBit1]]
@@ -4335,3 +4315,6 @@ class chararray(ndarray[_ShapeType, _CharDType]):
43354315
def isupper(self) -> ndarray[_ShapeType, dtype[bool_]]: ...
43364316
def isnumeric(self) -> ndarray[_ShapeType, dtype[bool_]]: ...
43374317
def isdecimal(self) -> ndarray[_ShapeType, dtype[bool_]]: ...
4318+
4319+
# NOTE: Deprecated
4320+
# class MachAr: ...

numpy/core/__init__.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from numpy.version import version as __version__
1010

1111
import os
12+
import warnings
1213

1314
# disables OpenBLAS affinity setting of the main thread that limits
1415
# python threads or processes to one core
@@ -80,8 +81,8 @@
8081
from .defchararray import chararray
8182
from . import function_base
8283
from .function_base import *
83-
from . import machar
84-
from .machar import *
84+
from . import _machar
85+
from ._machar import *
8586
from . import getlimits
8687
from .getlimits import *
8788
from . import shape_base
@@ -109,7 +110,6 @@
109110
__all__ += ['record', 'recarray', 'format_parser']
110111
__all__ += ['chararray']
111112
__all__ += function_base.__all__
112-
__all__ += machar.__all__
113113
__all__ += getlimits.__all__
114114
__all__ += shape_base.__all__
115115
__all__ += einsumfunc.__all__
@@ -151,6 +151,17 @@ def _DType_reduce(DType):
151151
return _DType_reconstruct, (scalar_type,)
152152

153153

154+
def __getattr__(name):
155+
# Deprecated 2021-10-20, NumPy 1.22
156+
if name == "machar":
157+
warnings.warn(
158+
"The `np.core.machar` module is deprecated (NumPy 1.22)",
159+
DeprecationWarning, stacklevel=2,
160+
)
161+
return _machar
162+
raise AttributeError(f"Module {__name__!r} has no attribute {name!r}")
163+
164+
154165
import copyreg
155166

156167
copyreg.pickle(ufunc, _ufunc_reduce)

numpy/core/machar.py renamed to numpy/core/_machar.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
# Need to speed this up...especially for longfloat
1515

16+
# Deprecated 2021-10-20, NumPy 1.22
1617
@set_module('numpy')
1718
class MachAr:
1819
"""

numpy/core/getlimits.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import warnings
77

8-
from .machar import MachAr
8+
from ._machar import MachAr
99
from .overrides import set_module
1010
from . import numeric
1111
from . import numerictypes as ntypes
@@ -385,6 +385,8 @@ class finfo:
385385
machar : MachAr
386386
The object which calculated these parameters and holds more
387387
detailed information.
388+
389+
.. deprecated:: 1.22
388390
machep : int
389391
The exponent that yields `eps`.
390392
max : floating point number of the appropriate type
@@ -501,7 +503,7 @@ def _init(self, dtype):
501503
self.eps = machar.eps.flat[0]
502504
self.nexp = machar.iexp
503505
self.nmant = machar.it
504-
self.machar = machar
506+
self._machar = machar
505507
self._str_tiny = machar._str_xmin.strip()
506508
self._str_max = machar._str_xmax.strip()
507509
self._str_epsneg = machar._str_epsneg.strip()
@@ -551,11 +553,11 @@ def smallest_normal(self):
551553
"""
552554
# This check is necessary because the value for smallest_normal is
553555
# platform dependent for longdouble types.
554-
if isnan(self.machar.smallest_normal.flat[0]):
556+
if isnan(self._machar.smallest_normal.flat[0]):
555557
warnings.warn(
556558
'The value of smallest normal is undefined for double double',
557559
UserWarning, stacklevel=2)
558-
return self.machar.smallest_normal.flat[0]
560+
return self._machar.smallest_normal.flat[0]
559561

560562
@property
561563
def tiny(self):
@@ -574,6 +576,20 @@ def tiny(self):
574576
"""
575577
return self.smallest_normal
576578

579+
@property
580+
def machar(self):
581+
"""The object which calculated these parameters and holds more
582+
detailed information.
583+
584+
.. deprecated:: 1.22
585+
"""
586+
# Deprecated 2021-10-27, NumPy 1.22
587+
warnings.warn(
588+
"`finfo.machar` is deprecated (NumPy 1.22)",
589+
DeprecationWarning, stacklevel=2,
590+
)
591+
return self._machar
592+
577593

578594
@set_module('numpy')
579595
class iinfo:

numpy/core/getlimits.pyi

Lines changed: 1 addition & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,8 @@
1-
from typing import Any, Generic, List, Type, TypeVar
1+
from typing import List
22

33
from numpy import (
44
finfo as finfo,
55
iinfo as iinfo,
6-
floating,
7-
signedinteger,
86
)
97

10-
from numpy.typing import NBitBase, NDArray
11-
12-
_NBit = TypeVar("_NBit", bound=NBitBase)
13-
148
__all__: List[str]
15-
16-
class MachArLike(Generic[_NBit]):
17-
def __init__(
18-
self,
19-
ftype: Type[floating[_NBit]],
20-
*,
21-
eps: floating[Any],
22-
epsneg: floating[Any],
23-
huge: floating[Any],
24-
tiny: floating[Any],
25-
ibeta: int,
26-
smallest_subnormal: None | floating[Any] = ...,
27-
# Expand `**kwargs` into keyword-only arguments
28-
machep: int,
29-
negep: int,
30-
minexp: int,
31-
maxexp: int,
32-
it: int,
33-
iexp: int,
34-
irnd: int,
35-
ngrd: int,
36-
) -> None: ...
37-
@property
38-
def smallest_subnormal(self) -> NDArray[floating[_NBit]]: ...
39-
eps: NDArray[floating[_NBit]]
40-
epsilon: NDArray[floating[_NBit]]
41-
epsneg: NDArray[floating[_NBit]]
42-
huge: NDArray[floating[_NBit]]
43-
ibeta: signedinteger[_NBit]
44-
iexp: int
45-
irnd: int
46-
it: int
47-
machep: int
48-
maxexp: int
49-
minexp: int
50-
negep: int
51-
ngrd: int
52-
precision: int
53-
resolution: NDArray[floating[_NBit]]
54-
smallest_normal: NDArray[floating[_NBit]]
55-
tiny: NDArray[floating[_NBit]]
56-
title: str
57-
xmax: NDArray[floating[_NBit]]
58-
xmin: NDArray[floating[_NBit]]

numpy/core/tests/test_deprecations.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1215,3 +1215,18 @@ def test_deprecated(self, func):
12151215
def test_not_deprecated(self, func):
12161216
self.assert_not_deprecated(lambda: func(1))
12171217
self.assert_not_deprecated(lambda: func([0, 1]))
1218+
1219+
1220+
class TestMachAr(_DeprecationTestCase):
1221+
# Deprecated 2021-10-19, NumPy 1.22
1222+
warning_cls = DeprecationWarning
1223+
1224+
def test_deprecated(self):
1225+
self.assert_deprecated(lambda: np.MachAr)
1226+
1227+
def test_deprecated_module(self):
1228+
self.assert_deprecated(lambda: getattr(np.core, "machar"))
1229+
1230+
def test_deprecated_attr(self):
1231+
finfo = np.finfo(float)
1232+
self.assert_deprecated(lambda: getattr(finfo, "machar"))

numpy/core/tests/test_getlimits.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def test_basic(self):
4646
[np.float16, np.float32, np.float64, np.complex64,
4747
np.complex128]))
4848
for dt1, dt2 in dts:
49-
for attr in ('bits', 'eps', 'epsneg', 'iexp', 'machar', 'machep',
49+
for attr in ('bits', 'eps', 'epsneg', 'iexp', 'machep',
5050
'max', 'maxexp', 'min', 'minexp', 'negep', 'nexp',
5151
'nmant', 'precision', 'resolution', 'tiny',
5252
'smallest_normal', 'smallest_subnormal'):

numpy/core/tests/test_machar.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
rid of both MachAr and this test at some point.
44
55
"""
6-
from numpy.core.machar import MachAr
6+
from numpy.core._machar import MachAr
77
import numpy.core.numerictypes as ntypes< F438 /span>
88
from numpy import errstate, array
99

numpy/core/tests/test_numeric.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,7 @@ def test_floating_exceptions(self, typecode):
646646
if np.dtype(ftype).kind == 'f':
647647
# Get some extreme values for the type
648648
fi = np.finfo(ftype)
649-
ft_tiny = fi.machar.tiny
649+
ft_tiny = fi._machar.tiny
650650
ft_max = fi.max
651651
ft_eps = fi.eps
652652
underflow = 'underflow'
@@ -655,7 +655,7 @@ def test_floating_exceptions(self, typecode):
655655
# 'c', complex, corresponding real dtype
656656
rtype = type(ftype(0).real)
657657
fi = np.finfo(rtype)
658-
ft_tiny = ftype(fi.machar.tiny)
658+
ft_tiny = ftype(fi._machar.tiny)
659659
ft_max = ftype(fi.max)
660660
ft_eps = ftype(fi.eps)
661661
# The complex types raise different exceptions

numpy/tests/test_public_api.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,6 @@ def test_NPY_NO_EXPORT():
178178
"core.fromnumeric",
179179
"core.function_base",
180180
"core.getlimits",
181-
"core.machar",
182181
"core.memmap",
183182
"core.multiarray",
184183
"core.numeric",
Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
import numpy as np
2-
from numpy.typing import _32Bit
3-
42
f: float
53
f8: np.float64
64
c8: np.complex64
@@ -11,7 +9,6 @@ u4: np.uint32
119

1210
finfo_f8: np.finfo[np.float64]
1311
iinfo_i8: np.iinfo[np.int64]
14-
machar_f4: np.core.getlimits.MachArLike[_32Bit]
1512

1613
reveal_type(np.finfo(f)) # E: numpy.finfo[{double}]
1714
reveal_type(np.finfo(f8)) # E: numpy.finfo[{float64}]
@@ -36,7 +33,6 @@ reveal_type(finfo_f8.resolution) # E: {float64}
3633
reveal_type(finfo_f8.tiny) # E: {float64}
3734
reveal_type(finfo_f8.smallest_normal) # E: {float64}
3835
reveal_type(finfo_f8.smallest_subnormal) # E: {float64}
39-
reveal_type(finfo_f8.machar) # E: MachArLike[numpy.typing._64Bit]
4036

4137
reveal_type(np.iinfo(i)) # E: iinfo[{int_}]
4238
reveal_type(np.iinfo(i8)) # E: iinfo[{int64}]
@@ -49,25 +45,3 @@ reveal_type(iinfo_i8.bits) # E: int
4945
reveal_type(iinfo_i8.key) # E: str
5046
reveal_type(iinfo_i8.min) # E: int
5147
reveal_type(iinfo_i8.max) # E: int
52-
53-
reveal_type(machar_f4.eps) # E: numpy.ndarray[Any, numpy.dtype[{float32}]]
54-
reveal_type(machar_f4.epsilon) # E: numpy.ndarray[Any, numpy.dtype[{float32}]]
55-
reveal_type(machar_f4.epsneg) # E: numpy.ndarray[Any, numpy.dtype[{float32}]]
56-
reveal_type(machar_f4.huge) # E: numpy.ndarray[Any, numpy.dtype[{float32}]]
57-
reveal_type(machar_f4.resolution) # E: numpy.ndarray[Any, numpy.dtype[{float32}]]
58-
reveal_type(machar_f4.tiny) # E: numpy.ndarray[Any, numpy.dtype[{float32}]]
59-
reveal_type(machar_f4.xmax) # E: numpy.ndarray[Any, numpy.dtype[{float32}]]
60-
reveal_type(machar_f4.xmin) # E: numpy.ndarray[Any, numpy.dtype[{float32}]]
61-
reveal_type(machar_f4.smallest_subnormal) # E: numpy.ndarray[Any, numpy.dtype[{float32}]]
62-
reveal_type(machar_f4.smallest_normal) # E: numpy.ndarray[Any, numpy.dtype[{float32}]]
63-
reveal_type(machar_f4.iexp) # E: int
64-
reveal_type(machar_f4.irnd) # E: int
65-
reveal_type(machar_f4.it) # E: int
66-
reveal_type(machar_f4.machep) # E: int
67-
reveal_type(machar_f4.maxexp) # E: int
68-
reveal_type(machar_f4.minexp) # E: int
69-
reveal_type(machar_f4.negep) # E: int
70-
reveal_type(machar_f4.ngrd) # E: int
71-
reveal_type(machar_f4.precision) # E: int
72-
reveal_type(machar_f4.ibeta) # E: {int32}
73-
reveal_type(machar_f4.title) # E: str

0 commit comments

Comments
 (0)
0