Skip to content
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
4 changes: 4 additions & 0 deletions doc/release/upcoming_changes/30774.deprecation.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
``typename`` is deprecated
--------------------------
``numpy.typename`` is deprecated because the names returned by it were outdated and inconsistent.
``numpy.dtype.name`` can be used as a replacement.
8 changes: 8 additions & 0 deletions numpy/_core/tests/test_deprecations.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,3 +347,11 @@ class TestTooManyArgsExtremum(_DeprecationTestCase):
@pytest.mark.parametrize("ufunc", [np.minimum, np.maximum])
def test_extremem_3_args(self, ufunc):
self.assert_deprecated(ufunc, args=(np.ones(1), np.zeros(1), np.empty(1)))


class TestTypenameDeprecation(_DeprecationTestCase):
# Deprecation in Numpy 2.5, 2026-02

def test_typename_emits_deprecation_warning(self):
self.assert_deprecated(lambda: np.typename("S1"))
self.assert_deprecated(lambda: np.typename("h"))
1 change: 1 addition & 0 deletions numpy/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ def warnings_errors_and_rng(test=None):
"NumPy warning suppression and assertion utilities are deprecated.",
"numpy.fix is deprecated", # fix -> trunc
"The chararray class is deprecated", # char.chararray
"numpy.typename is deprecated", # typename -> dtype.name
]
msg = "|".join(msgs)

Expand Down
10 changes: 10 additions & 0 deletions numpy/lib/_type_check_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

"""
import functools
import warnings

__all__ = ['iscomplexobj', 'isrealobj', 'imag', 'iscomplex',
'isreal', 'nan_to_num', 'real', 'real_if_close',
Expand Down Expand Up @@ -587,6 +588,9 @@ def typename(char):
"""
Return a description for the given data type code.

.. deprecated:: 2.5
`numpy.typename` is deprecated. Use `numpy.dtype.name` instead.

Parameters
----------
char : str
Expand Down Expand Up @@ -633,6 +637,12 @@ def typename(char):
q : long long integer

"""
# Deprecated in NumPy 2.5, 2026-02-03
warnings.warn(
"numpy.typename is deprecated. Use numpy.dtype.name instead.",
DeprecationWarning,
stacklevel=2
)
return _namefromtype[char]

#-----------------------------------------------------------------------------
Expand Down
23 changes: 23 additions & 0 deletions numpy/lib/_type_check_impl.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from _typeshed import Incomplete
from collections.abc import Container, Iterable
from typing import Any, Literal as L, Protocol, overload, type_check_only
from typing_extensions import deprecated

import numpy as np
from numpy._typing import (
Expand Down Expand Up @@ -148,48 +149,70 @@ def real_if_close(a: ArrayLike, tol: float = 100) -> NDArray[Any]: ...

#
@overload
@deprecated("numpy.typename is deprecated. Use numpy.dtype.name instead.")
def typename(char: L["S1"]) -> L["character"]: ...
@overload
@deprecated("numpy.typename is deprecated. Use numpy.dtype.name instead.")
def typename(char: L["?"]) -> L["bool"]: ...
@overload
@deprecated("numpy.typename is deprecated. Use numpy.dtype.name instead.")
def typename(char: L["b"]) -> L["signed char"]: ...
@overload
@deprecated("numpy.typename is deprecated. Use numpy.dtype.name instead.")
def typename(char: L["B"]) -> L["unsigned char"]: ...
@overload
@deprecated("numpy.typename is deprecated. Use numpy.dtype.name instead.")
def typename(char: L["h"]) -> L["short"]: ...
@overload
@deprecated("numpy.typename is deprecated. Use numpy.dtype.name instead.")
def typename(char: L["H"]) -> L["unsigned short"]: ...
@overload
@deprecated("numpy.typename is deprecated. Use numpy.dtype.name instead.")
def typename(char: L["i"]) -> L["integer"]: ...
@overload
@deprecated("numpy.typename is deprecated. Use numpy.dtype.name instead.")
def typename(char: L["I"]) -> L["unsigned integer"]: ...
@overload
@deprecated("numpy.typename is deprecated. Use numpy.dtype.name instead.")
def typename(char: L["l"]) -> L["long integer"]: ...
@overload
@deprecated("numpy.typename is deprecated. Use numpy.dtype.name instead.")
def typename(char: L["L"]) -> L["unsigned long integer"]: ...
@overload
@deprecated("numpy.typename is deprecated. Use numpy.dtype.name instead.")
def typename(char: L["q"]) -> L["long long integer"]: ...
@overload
@deprecated("numpy.typename is deprecated. Use numpy.dtype.name instead.")
def typename(char: L["Q"]) -> L["unsigned long long integer"]: ...
@overload
@deprecated("numpy.typename is deprecated. Use numpy.dtype.name instead.")
def typename(char: L["f"]) -> L["single precision"]: ...
@overload
@deprecated("numpy.typename is deprecated. Use numpy.dtype.name instead.")
def typename(char: L["d"]) -> L["double precision"]: ...
@overload
@deprecated("numpy.typename is deprecated. Use numpy.dtype.name instead.")
def typename(char: L["g"]) -> L["long precision"]: ...
@overload
@deprecated("numpy.typename is deprecated. Use numpy.dtype.name instead.")
def typename(char: L["F"]) -> L["complex single precision"]: ...
@overload
@deprecated("numpy.typename is deprecated. Use numpy.dtype.name instead.")
def typename(char: L["D"]) -> L["complex double precision"]: ...
@overload
@deprecated("numpy.typename is deprecated. Use numpy.dtype.name instead.")
def typename(char: L["G"]) -> L["complex long double precision"]: ...
@overload
@deprecated("numpy.typename is deprecated. Use numpy.dtype.name instead.")
def typename(char: L["S"]) -> L["string"]: ...
@overload
@deprecated("numpy.typename is deprecated. Use numpy.dtype.name instead.")
def typename(char: L["U"]) -> L["unicode"]: ...
@overload
@deprecated("numpy.typename is deprecated. Use numpy.dtype.name instead.")
def typename(char: L["V"]) -> L["void"]: ...
@overload
@deprecated("numpy.typename is deprecated. Use numpy.dtype.name instead.")
def typename(char: L["O"]) -> L["object"]: ...

# NOTE: The [overload-overlap] mypy errors are false positives
Expand Down
8 changes: 4 additions & 4 deletions numpy/typing/tests/data/reveal/type_check.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ assert_type(np.real_if_close(AR_c16), npt.NDArray[np.float64 | np.complex128])
assert_type(np.real_if_close(AR_c8), npt.NDArray[np.float32 | np.complex64])
assert_type(np.real_if_close(AR_LIKE_f), npt.NDArray[Any])

assert_type(np.typename("h"), Literal["short"])
assert_type(np.typename("B"), Literal["unsigned char"])
assert_type(np.typename("V"), Literal["void"])
assert_type(np.typename("S1"), Literal["character"])
assert_type(np.typename("h"), Literal["short"]) # type: ignore[deprecated]
assert_type(np.typename("B"), Literal["unsigned char"]) # type: ignore[deprecated]
assert_type(np.typename("V"), Literal["void"]) # type: ignore[deprecated]
assert_type(np.typename("S1"), Literal["character"]) # type: ignore[deprecated]

assert_type(np.common_type(AR_i4), type[np.float64])
assert_type(np.common_type(AR_f2), type[np.float16])
Expand Down
Loading