Skip to content

Commit 11d9ed2

Browse files
authored
Merge pull request #30459 from charris/backport-30456
TYP: restore ``generic.__hash__`` (#30456)
2 parents d04a429 + 2a17ddb commit 11d9ed2

3 files changed

Lines changed: 21 additions & 0 deletions

File tree

numpy/__init__.pyi

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3670,6 +3670,12 @@ class generic(_ArrayOrScalarCommon, Generic[_ItemT_co]):
36703670
@abstractmethod
36713671
def __new__(cls, /, *args: Any, **kwargs: Any) -> Self: ...
36723672

3673+
# NOTE: Technically this doesn't exist at runtime, but it is unlikely to lead to
3674+
# type-unsafe situations (the abstract scalar types cannot be instantiated
3675+
# themselves) and is convenient to have, so we include it regardless. See
3676+
# https://github.com/numpy/numpy/issues/30445 for use-cases and discussion.
3677+
def __hash__(self, /) -> int: ...
3678+
36733679
if sys.version_info >= (3, 12):
36743680
def __buffer__(self, flags: int, /) -> memoryview: ...
36753681

@@ -5728,6 +5734,10 @@ class bytes_(character[bytes], bytes): # type: ignore[misc]
57285734
@overload
57295735
def __new__(cls, value: str, /, encoding: str, errors: str = "strict") -> Self: ...
57305736

5737+
#
5738+
@override
5739+
def __hash__(self, /) -> int: ...
5740+
57315741
#
57325742
def __bytes__(self, /) -> bytes: ...
57335743

@@ -5737,6 +5747,10 @@ class str_(character[str], str): # type: ignore[misc]
57375747
@overload
57385748
def __new__(cls, value: bytes, /, encoding: str, errors: str = "strict") -> Self: ...
57395749

5750+
#
5751+
@override
5752+
def __hash__(self, /) -> int: ...
5753+
57405754
# See `numpy._typing._ufunc` for more concrete nin-/nout-specific stubs
57415755
@final
57425756
class ufunc:

numpy/typing/tests/data/pass/ndarray_misc.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from __future__ import annotations
1010

1111
import operator
12+
from collections.abc import Hashable
1213
from typing import Any, cast
1314

1415
import numpy as np
@@ -192,3 +193,7 @@ class IntSubClass(npt.NDArray[np.intp]): ...
192193
A_void: npt.NDArray[np.void] = np.empty(3, [("yop", float), ("yap", float)])
193194
A_void["yop"] = A_float[:, 0]
194195
A_void["yap"] = A_float[:, 1]
196+
197+
# regression test for https://github.com/numpy/numpy/issues/30445
198+
def f(x: np.generic) -> Hashable:
199+
return x

tools/stubtest/allowlist.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ numpy\.(\w+\.)*integer\.bit_count
3939
numpy\.(\w+\.)*floating\.as_integer_ratio
4040
numpy\.(\w+\.)*floating\.is_integer
4141
numpy\.(\w+\.)*complexfloating\.__complex__
42+
# https://github.com/numpy/numpy/issues/30445#issuecomment-3665484402
43+
numpy\.(\w+\.)*generic\.__hash__
4244

4345
# intentionally missing deprecated module stubs
4446
numpy\.core\._dtype

0 commit comments

Comments
 (0)