diff --git a/numpy/__init__.pyi b/numpy/__init__.pyi index e1092e39757f..22d462d9ee5f 100644 --- a/numpy/__init__.pyi +++ b/numpy/__init__.pyi @@ -205,7 +205,6 @@ from typing import ( SupportsComplex, SupportsFloat, SupportsInt, - TypeVar, Protocol, SupportsIndex, Final, @@ -219,7 +218,7 @@ from typing import ( # This is because the `typeshed` stubs for the standard library include # `typing_extensions` stubs: # https://github.com/python/typeshed/blob/main/stdlib/typing_extensions.pyi -from typing_extensions import LiteralString, Self +from typing_extensions import LiteralString, Self, TypeVar from numpy import ( core, @@ -4131,7 +4130,7 @@ class busdaycalendar: def holidays(self) -> NDArray[datetime64]: ... -_FloatType_co = TypeVar('_FloatType_co', bound=floating[Any], covariant=True) +_FloatType_co = TypeVar('_FloatType_co', bound=floating[Any], covariant=True, default=floating[NBitBase]) class finfo(Generic[_FloatType_co]): dtype: Final[dtype[_FloatType_co]] @@ -4167,7 +4166,7 @@ class finfo(Generic[_FloatType_co]): cls, dtype: str ) -> finfo[floating[Any]]: ... -_IntType_co = TypeVar("_IntType_co", bound=integer[Any], covariant=True) +_IntType_co = TypeVar("_IntType_co", bound=integer[Any], covariant=True, default=integer[NBitBase]) class iinfo(Generic[_IntType_co]): dtype: Final[dtype[_IntType_co]] diff --git a/numpy/_core/getlimits.py b/numpy/_core/getlimits.py index 669dfc71e298..3ceb8139ee70 100644 --- a/numpy/_core/getlimits.py +++ b/numpy/_core/getlimits.py @@ -3,6 +3,7 @@ """ __all__ = ['finfo', 'iinfo'] +import types import warnings from .._utils import set_module @@ -487,6 +488,8 @@ class finfo: _finfo_cache = {} + __class_getitem__ = classmethod(types.GenericAlias) + def __new__(cls, dtype): try: obj = cls._finfo_cache.get(dtype) # most common path @@ -689,6 +692,8 @@ class iinfo: _min_vals = {} _max_vals = {} + __class_getitem__ = classmethod(types.GenericAlias) + def __init__(self, int_type): try: self.dtype = numeric.dtype(int_type) diff --git a/numpy/_core/tests/test_getlimits.py b/numpy/_core/tests/test_getlimits.py index 930c0145c71c..3fe67a1f4037 100644 --- a/numpy/_core/tests/test_getlimits.py +++ b/numpy/_core/tests/test_getlimits.py @@ -1,6 +1,7 @@ """ Test functions for limits module. """ +import types import warnings import numpy as np import pytest @@ -192,3 +193,11 @@ def test_plausible_finfo(): assert_(info.nmant > 1) assert_(info.minexp < -1) assert_(info.maxexp > 1) + + +class TestRuntimeSubscriptable: + def test_finfo_generic(self): + assert isinstance(np.finfo[np.float64], types.GenericAlias) + + def test_iinfo_generic(self): + assert isinstance(np.iinfo[np.int_], types.GenericAlias)