NumPy version information that type-checkers understand
Project description
numpy-typing-compat
NumPy version information that type-checkers understand
Overview
This package provides version-specific boolean constants that allow library authors to
write NumPy-version-dependent static type annotations. Similar to how you might use
if sys.version_info >= (3, 12): for Python version checks, numpy-typing-compat
enables static type-checkers to understand which NumPy version is being used and apply
appropriate type annotations.
Use Case
This package is particularly useful for libraries that need to support multiple NumPy
versions, for example because they follow
SPEC 0. However, there may have been
changes in NumPy that affect type annotations. For instance, the numpy.exceptions
module was introduced in NumPy 1.25, and contains the exceptions that were previously in
the global numpy namespace. In NumPy 2.0, these exceptions were removed from the
global namespace. So if you support <1.25 and also >=2.0, you will need to
conditionally import these exceptions to ensure compatibility across versions.
from numpy_typing_compat import NUMPY_GE_1_25
if NUMPY_GE_1_25:
from numpy.exceptions import AxisError
else:
from numpy import AxisError
Type checkers like mypy, pyright, and basedpyright understand these patterns and will apply the correct type annotations based on the installed NumPy version.
Installation
pip install numpy-typing-compat
The package automatically selects the appropriate version constants based on your
installed NumPy version. It does so by requiring a specific version of NumPy in its
pyproject.toml file, so that your package manager will install the correct version of
numpy-typing-compat that matches the NumPy version you have installed.
For example, if you have numpy==2.1.3 pinned in your pyproject.toml, and you run
uv add numpy-typing-compat, it will install numpy-typing-compat==2.1.*. That
specific version has NUMPY_GE_2_1 = True, and NUMPY_GE_2_2 = False set.
Note that numpy-typing-compat does not import numpy, and instead relies on the
package manager to install the correct version of numpy-typing-compat that matches the
installed NumPy version.
Reference
Array API
Additionally, the package provides a numpy_typing_compat.array_api namespace that's a
re-export of the numpy.array_api module on numpy < 2.0, or the main numpy module
on numpy >= 2.0. Note that the numpy.array_api module was introduced in
numpy >= 1.23, so it isn't available in numpy-typing-compat==1.22.*.
numpy.long
NumPy 2.0 introduced the new long and ulong scalar types, which are not available in
numpy < 2.0, and instead went by the names int_ and uint (which in numpy >= 2.0
are aliases for intp and uintp).
If you need to support both NumPy versions, you can use the long and ulong types
from numpy_typing_compat, which on numpy < 2.0 are aliases for np.int_ and
np.uint, and on numpy >= 2.0 are re-exports of np.long and np.ulong.
numpy.dtypes.StringDType
In NumPy 2.0, the numpy.dtypes.StringDType was introduced, but it wasn't until
NumPy 2.1 that it was also available in the numpy stubs. The
numpy_typing_compat.StringDType is a re-export of numpy.dtypes.StringDType on
numpy >= 2.1, and an alias of np.dtype[Never] on numpy < 2.1. This allows type
checkers to also accept StringDType as a valid type on numpy == 2.0.*.
Version constants
The following low-level boolean version constants are available:
| Constant | True when |
|---|---|
NUMPY_GE_1_22 |
numpy >= 1.22 |
NUMPY_GE_1_23 |
numpy >= 1.23 |
NUMPY_GE_1_25 |
numpy >= 1.25 |
NUMPY_GE_2_0 |
numpy >= 2.0 |
NUMPY_GE_2_1 |
numpy >= 2.1 |
NUMPY_GE_2_2 |
numpy >= 2.2 |
NUMPY_GE_2_3 |
numpy >= 2.3 |
Each constant is typed as Literal[True] or Literal[False] depending on your NumPy
version, allowing type checkers to perform accurate type narrowing.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file numpy_typing_compat-2.3.20250726.tar.gz.
File metadata
- Download URL: numpy_typing_compat-2.3.20250726.tar.gz
- Upload date:
- Size: 3.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ffa89ab34e5f13549f84b9fd10be1d9873971055e6df305c9c9d4a88f10665a8
|
|
| MD5 |
a6d23e03bc9bfb904ee41791f3f5c448
|
|
| BLAKE2b-256 |
f7651b9914c6edb875e36b24496ab63fd507a5ea1c2c45ddb11fe91ad554ec1c
|
Provenance
The following attestation bundles were made for numpy_typing_compat-2.3.20250726.tar.gz:
Publisher:
publish-pypi.yml on jorenham/numpy-typing-compat
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
numpy_typing_compat-2.3.20250726.tar.gz -
Subject digest:
ffa89ab34e5f13549f84b9fd10be1d9873971055e6df305c9c9d4a88f10665a8 - Sigstore transparency entry: 315042623
- Sigstore integration time:
-
Permalink:
jorenham/numpy-typing-compat@a90f86c0f0e718ddd526fcd3c7001ee25611b329 -
Branch / Tag:
refs/tags/v20250726 - Owner: https://github.com/jorenham
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-pypi.yml@a90f86c0f0e718ddd526fcd3c7001ee25611b329 -
Trigger Event:
release
-
Statement type:
File details
Details for the file numpy_typing_compat-2.3.20250726-py3-none-any.whl.
File metadata
- Download URL: numpy_typing_compat-2.3.20250726-py3-none-any.whl
- Upload date:
- Size: 5.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
def439bdd78ece8893619515d2c6f856b1acf749ed10e26a4b76f5e3d87a312e
|
|
| MD5 |
8f415b835313e7adbcc664b650398ea0
|
|
| BLAKE2b-256 |
d2778f52c7537dbe6982c1bb8c7c875ed1d6dc6470875e0742ebf922dc451080
|
Provenance
The following attestation bundles were made for numpy_typing_compat-2.3.20250726-py3-none-any.whl:
Publisher:
publish-pypi.yml on jorenham/numpy-typing-compat
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
numpy_typing_compat-2.3.20250726-py3-none-any.whl -
Subject digest:
def439bdd78ece8893619515d2c6f856b1acf749ed10e26a4b76f5e3d87a312e - Sigstore transparency entry: 315042634
- Sigstore integration time:
-
Permalink:
jorenham/numpy-typing-compat@a90f86c0f0e718ddd526fcd3c7001ee25611b329 -
Branch / Tag:
refs/tags/v20250726 - Owner: https://github.com/jorenham
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-pypi.yml@a90f86c0f0e718ddd526fcd3c7001ee25611b329 -
Trigger Event:
release
-
Statement type: