8000 Merge pull request #27346 from jorenham/bug/fix-27341 · numpy/numpy@e88e37f · GitHub
[go: up one dir, main page]

Skip to content

Commit e88e37f

Browse files
authored
Merge pull request #27346 from jorenham/bug/fix-27341
BUG,TYP: Allow subscripting ``iinfo`` and ``finfo`` generic types at runtime, or omit the type params
2 parents 32abfe2 + be61071 commit e88e37f

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

numpy/__init__.pyi

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,6 @@ from typing import (
205205
SupportsComplex,
206206
SupportsFloat,
207207
SupportsInt,
208-
TypeVar,
209208
Protocol,
210209
SupportsIndex,
211210
Final,
@@ -219,7 +218,7 @@ from typing import (
219218
# This is because the `typeshed` stubs for the standard library include
220219
# `typing_extensions` stubs:
221220
# https://github.com/python/typeshed/blob/main/stdlib/typing_extensions.pyi
222-
from typing_extensions import LiteralString, Self
221+
from typing_extensions import LiteralString, Self, TypeVar
223222

224223
from numpy import (
225224
core,
@@ -4131,7 +4130,7 @@ class busdaycalendar:
41314130
def holidays(self) -> NDArray[datetime64]: ...
41324131

41334132

4134-
_FloatType_co = TypeVar('_FloatType_co', bound=floating[Any], covariant=True)
4133+
_FloatType_co = TypeVar('_FloatType_co', bound=floating[Any], covariant=True, default=floating[NBitBase])
41354134

41364135
class finfo(Generic[_FloatType_co]):
41374136
dtype: Final[dtype[_FloatType_co]]
@@ -4167,7 +4166,7 @@ class finfo(Generic[_FloatType_co]):
41674166
cls, dtype: str
41684167
) -> finfo[floating[Any]]: ...
41694168

4170-
_IntType_co = TypeVar("_IntType_co", bound=integer[Any], covariant=True)
4169+
_IntType_co = TypeVar("_IntType_co", bound=integer[Any], covariant=True, default=integer[NBitBase])
41714170

41724171
class iinfo(Generic[_IntType_co]):
41734172
dtype: Final[dtype[_IntType_co]]

numpy/_core/getlimits.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"""
44
__all__ = ['finfo', 'iinfo']
55

6+
import types
67
import warnings
78

89
from .._utils import set_module
@@ -487,6 +488,8 @@ class finfo:
487488

488489
_finfo_cache = {}
489490

491+
__class_getitem__ = classmethod(types.GenericAlias)
492+
490493
def __new__(cls, dtype):
491494
try:
492495
obj = cls._finfo_cache.get(dtype) # most common path
@@ -689,6 +692,8 @@ class iinfo:
689692
_min_vals = {}
690693
_max_vals = {}
691694

695+
__class_getitem__ = classmethod(types.GenericAlias)
696+
692697
def __init__(self, int_type):
693698
try:
694699
self.dtype = numeric.dtype(int_type)

numpy/_core/tests/test_getlimits.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
""" Test functions for limits module.
22
33
"""
4+
import types
45
import warnings
56
import numpy as np
67
import pytest
@@ -192,3 +193,11 @@ def test_plausible_finfo():
192193
assert_(info.nmant > 1)
193194
assert_(info.minexp < -1)
194195
assert_(info.maxexp > 1)
196+
197+
198+
class TestRuntimeSubscriptable:
199+
def test_finfo_generic(self):
200+
assert isinstance(np.finfo[np.float64], types.GenericAlias)
201+
202+
def test_iinfo_generic(self):
203+
assert isinstance(np.iinfo[np.int_], types.GenericAlias)

0 commit comments

Comments
 (0)
0