8000 BUG,TYP: Allow subscripting ``iinfo`` and ``finfo`` generic types at runtime, or omit the type params by jorenham · Pull Request #27346 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG,TYP: Allow subscripting iinfo and finfo generic types at runtime, or omit the type params #27346

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions numpy/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,6 @@ from typing import (
SupportsComplex,
SupportsFloat,
SupportsInt,
TypeVar,
Protocol,
SupportsIndex,
Final,
Expand All @@ -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,
Expand Down Expand Up @@ -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]]
Expand Down Expand Up @@ -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]]
Expand Down
5 changes: 5 additions & 0 deletions numpy/_core/getlimits.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""
__all__ = ['finfo', 'iinfo']

import types
import warnings

from .._utils import set_module
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions numpy/_core/tests/test_getlimits.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
""" Test functions for limits module.

"""
import types
import warnings
import numpy as np
import pytest
Expand Down Expand Up @@ -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)
Loading
0