Skip to content

Commit cbc1d01

Browse files
committed
DEP: Deprecate finfo.machar
1 parent 123da22 commit cbc1d01

File tree

8 files changed

+30
-95
lines changed

8 files changed

+30
-95
lines changed
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
The ``np.MachAr`` class has been deprecated
22
-------------------------------------------
3-
The `~numpy.MachAr` class has been deprecated. Users are encouraged to
4-
use either `np.finfo(...).machar <numpy.finfo>`, or, if applicable, directly
5-
access the attribute of interest from the finfo instance.
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__.pyi

Lines changed: 0 additions & 9 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
@@ -3384,14 +3383,6 @@ class finfo(Generic[_FloatType]):
33843383
def smallest_normal(self) -> _FloatType: ...
33853384
@property
33863385
def tiny(self) -> _FloatType: ...
3387-
3388-
# NOTE: Not technically a property, but this is the only way we can
3389-
# access the precision of the underlying float
3390-
@property
3391-
def machar(self: finfo[floating[_NBit1]]) -> MachArLike[_NBit1]: ...
3392-
@machar.setter
3393-
def machar(self: finfo[floating[_NBit1]], value: MachArLike[_NBit1]) -> None: ...
3394-
33953386
@overload
33963387
def __new__(
33973388
cls, dtype: inexact[_NBit1] | _DTypeLike[inexact[_NBit1]]

numpy/core/getlimits.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1226,3 +1226,7 @@ def test_deprecated(self):
12261226

12271227
def test_deprecated_module(self):
12281228
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_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
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)