From 06ca86c69d4e021a6c553a411992126b1717035a Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Fri, 9 Jun 2023 20:40:57 +0200 Subject: [PATCH 1/3] BUG: Allow np.info on non-hashable objects with a dtype --- numpy/core/getlimits.py | 9 ++++++--- numpy/core/tests/test_getlimits.py | 9 +++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/numpy/core/getlimits.py b/numpy/core/getlimits.py index da9e1d7f311c..aaeb860a5dd9 100644 --- a/numpy/core/getlimits.py +++ b/numpy/core/getlimits.py @@ -482,9 +482,12 @@ class finfo: _finfo_cache = {} def __new__(cls, dtype): - obj = cls._finfo_cache.get(dtype) # most common path - if obj is not None: - return obj + try: + obj = cls._finfo_cache.get(dtype) # most common path + if obj is not None: + return obj + except TypeError: + obj = None if dtype is None: # Deprecated in NumPy 1.25, 2023-01-16 diff --git a/numpy/core/tests/test_getlimits.py b/numpy/core/tests/test_getlimits.py index 63217c38c098..277b2762d7e7 100644 --- a/numpy/core/tests/test_getlimits.py +++ b/numpy/core/tests/test_getlimits.py @@ -73,6 +73,15 @@ def test_regression_gh23108(self): f2 = np.finfo(np.float64(1.0)) assert f1 != f2 + def test_regression_gh23867(self): + class NonHashableWithDtype: + __hash__ = None + dtype = np.dtype('float32') + + x = NonHashableWithDtype() + assert np.finfo(x) == np.finfo(x.dtype) + + class TestIinfo: def test_basic(self): dts = list(zip(['i1', 'i2', 'i4', 'i8', From f80cba56f0e0120bb14d18e16651552a2d4cedcd Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Fri, 9 Jun 2023 20:49:35 +0200 Subject: [PATCH 2/3] lint --- numpy/core/tests/test_getlimits.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/numpy/core/tests/test_getlimits.py b/numpy/core/tests/test_getlimits.py index 277b2762d7e7..f646e2bd7980 100644 --- a/numpy/core/tests/test_getlimits.py +++ b/numpy/core/tests/test_getlimits.py @@ -75,8 +75,8 @@ def test_regression_gh23108(self): def test_regression_gh23867(self): class NonHashableWithDtype: - __hash__ = None - dtype = np.dtype('float32') + __hash__ = None + dtype = np.dtype('float32') x = NonHashableWithDtype() assert np.finfo(x) == np.finfo(x.dtype) From 4bb75f6b69ab687941e269c8f0c67c2872151971 Mon Sep 17 00:00:00 2001 From: Sebastian Berg Date: Fri, 9 Jun 2023 21:12:20 +0200 Subject: [PATCH 3/3] Update numpy/core/getlimits.py --- numpy/core/getlimits.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/numpy/core/getlimits.py b/numpy/core/getlimits.py index aaeb860a5dd9..13414c2a64d6 100644 --- a/numpy/core/getlimits.py +++ b/numpy/core/getlimits.py @@ -487,7 +487,7 @@ def __new__(cls, dtype): if obj is not None: return obj except TypeError: - obj = None + pass if dtype is None: # Deprecated in NumPy 1.25, 2023-01-16