Static typing compatibility layer for older versions of NumPy
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.
[!TIP]
numpy-typing-compatis intended as a helper package for theoptypelibrary, which provides high-level typing utilities for annotating NumPy operations. In most situations, it is recommended to useoptypeinstead ofnumpy-typing-compat. See theoptype.numpydocumentation for more details.
Installation
Modern package managers such as uv will
automatically install the appropriate version of numpy-typing-compat that matches your
installed NumPy version, in order to satisfy the numpy dependency restrictions of
numpy-typing-compat.
For example, the numpy-typing-compat==2.1.* distribution specifies numpy>=2.1,<2.2
as a required dependency. Modern package managers will ensure that these dependency
restrictions are satisfied. That way, if you upgrade numpy from 2.1 to 2.3, e.g.
by running uv sync --upgrade, then uv will also automatically look for a version of
numpy-typing-compat that satisfies the new numpy version, which in this case
would be numpy-typing-compat==2.3.*.
[!WARNING] Legacy package managers such as
pipdon't respect dependency restrictions. Runningpip install --upgrade numpywill not automatically upgradenumpy-typing-compatto the correct version. If for some reason you need to usepip, then be sure to manually install the correct version ofnumpy-typing-compatthat matches your installed NumPy version. Runningpip checkwill tell you whether the installednumpy-typing-compatandnumpyversions are compatible.
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.*.
long and ulong
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.
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.*.
ABCPolyBase
In NumPy 2.1, the numpy.polynomial._polybase.ABCPolyBase was made into a generic type,
and in NumPy 2.2 the type parameter was made optional. This can be problematic on
numpy==2.1.* if you also require support for numpy < 2.1.
To work around this, the numpy_typing_compat.ABCPolyBase is a type alias for
numpy.polynomial._polybase.ABCPolyBase[LiteralString | None] on numpy==2.1.*, and
a direct re-export of numpy.polynomial._polybase.ABCPolyBase otherwise. This way,
type checkers (when configured in strict mode) won't report an error on numpy==2.1.*
when using ABCPolyBase. Note that numpy_typing_compat.ABCPolyBase does not accept a
type parameter, even on numpy >= 2.1, for the sake of consistency.
LiteralTrue and LiteralFalse
In NumPy 2.2, the np.bool scalar type became a generic type that accepts a type
parameter of either True or False. For compatibility with numpy < 2.2, the
numpy_typing_compat.LiteralTrue and numpy_typing_compat.LiteralFalse types are
provided. These are type aliases for Literal[True] and Literal[False] on
numpy < 2.2, and Literal[True] | np.bool[Literal[True]] and
Literal[False] | np.bool[Literal[False]] on numpy >= 2.2, respectively.
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, so that static type-checkers are able to understand the NumPy version being
used.
[!WARNING] At the moment, mypy and pyright only have limited support for
Literaltype conditions, and will not treatif NUMPY_GE_2_0: ...in the same way asif sys.version_info >= (3, 12): ...orif sys.platform == "win32": ....
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.1.20250728.tar.gz.
File metadata
- Download URL: numpy_typing_compat-2.1.20250728.tar.gz
- Upload date:
- Size: 4.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7d580021b6347eac865063b084f99fe28c1e67083654166b1b6ca8d82edfc444
|
|
| MD5 |
1edebbfd8d764b1280ba2c9a6754d1c6
|
|
| BLAKE2b-256 |
42a2ec739d7f3c66d5dcafd6575639668531f7c0aaef40310143da436fbacf56
|
Provenance
The following attestation bundles were made for numpy_typing_compat-2.1.20250728.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.1.20250728.tar.gz -
Subject digest:
7d580021b6347eac865063b084f99fe28c1e67083654166b1b6ca8d82edfc444 - Sigstore transparency entry: 321746572
- Sigstore integration time:
-
Permalink:
jorenham/numpy-typing-compat@0d0d0355ac30b4ab360270de4524eaf280a80c56 -
Branch / Tag:
refs/tags/v20250728 - Owner: https://github.com/jorenham
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-pypi.yml@0d0d0355ac30b4ab360270de4524eaf280a80c56 -
Trigger Event:
release
-
Statement type:
File details
Details for the file numpy_typing_compat-2.1.20250728-py3-none-any.whl.
File metadata
- Download URL: numpy_typing_compat-2.1.20250728-py3-none-any.whl
- Upload date:
- Size: 6.1 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 |
0a909c7ef046ece2119a7ee6eff143a6152a27a5ec53dac5a05548c14c019b20
|
|
| MD5 |
550c05cb149db4df02aa8ceeae225992
|
|
| BLAKE2b-256 |
4674e754ad04b8771e8864350007f71050cfbf6a9b9806657a42d7636fc71b31
|
Provenance
The following attestation bundles were made for numpy_typing_compat-2.1.20250728-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.1.20250728-py3-none-any.whl -
Subject digest:
0a909c7ef046ece2119a7ee6eff143a6152a27a5ec53dac5a05548c14c019b20 - Sigstore transparency entry: 321746770
- Sigstore integration time:
-
Permalink:
jorenham/numpy-typing-compat@0d0d0355ac30b4ab360270de4524eaf280a80c56 -
Branch / Tag:
refs/tags/v20250728 - Owner: https://github.com/jorenham
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-pypi.yml@0d0d0355ac30b4ab360270de4524eaf280a80c56 -
Trigger Event:
release
-
Statement type: