{"id":3179,"library":"numpy-typing-compat","title":"NumPy Typing Compatibility Layer","description":"numpy-typing-compat provides a static typing compatibility layer for older versions of NumPy. Its current version is `20251206.2.4`, and it sees frequent releases, often coinciding with new NumPy pre-releases or critical bug fixes to ensure type hint accuracy across various NumPy versions.","status":"active","version":"20251206.2.4","language":"en","source_language":"en","source_url":"https://github.com/jorenham/numpy-typing-compat","tags":["numpy","typing","type-hints","compatibility","static-analysis"],"install":[{"cmd":"pip install numpy-typing-compat","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Requires Python 3.11 or newer.","package":"python","optional":false},{"reason":"This library provides typing stubs and compatibility types for NumPy. While not a direct runtime dependency, it is useless without NumPy installed.","package":"numpy","optional":false}],"imports":[{"note":"A primary type alias for `np.ndarray`, offering subscriptability (`CanArray[DType]`) for compatibility with older NumPy versions where such typing might be missing or incorrect.","symbol":"CanArray","correct":"from numpy_typing_compat import CanArray"},{"note":"Type alias representing various ways to specify a NumPy dtype.","symbol":"DTypeLike","correct":"from numpy_typing_compat import DTypeLike"},{"note":"Type alias representing objects that can be converted to a NumPy array.","symbol":"ArrayLike","correct":"from numpy_typing_compat import ArrayLike"},{"note":"Compatibility alias for NumPy's integer type, useful for older NumPy versions.","symbol":"int_","correct":"from numpy_typing_compat import int_"}],"quickstart":{"code":"import numpy as np\nfrom numpy_typing_compat import CanArray, DTypeLike\n\ndef process_numeric_array(data: CanArray[DTypeLike]) -> CanArray[DTypeLike]:\n    \"\"\"\n    Processes a NumPy array, using CanArray for type hinting\n    compatibility, especially with older NumPy versions where\n    type stubs might be less complete.\n    \"\"\"\n    print(f\"Input array type: {type(data)}, dtype: {data.dtype}\")\n    # Perform some array operation\n    return data * 2\n\nif __name__ == \"__main__\":\n    # Example with a float array\n    my_float_array = np.array([1.0, 2.5, 3.0], dtype=np.float64)\n    result_float_array = process_numeric_array(my_float_array)\n    print(f\"Result float array: {result_float_array}, dtype: {result_float_array.dtype}\\n\")\n\n    # Example with an integer array\n    my_int_array = np.array([10, 20, 30], dtype=np.int32)\n    result_int_array = process_numeric_array(my_int_array)\n    print(f\"Result int array: {result_int_array}, dtype: {result_int_array.dtype}\")","lang":"python","description":"This quickstart demonstrates how to use `CanArray` and `DTypeLike` for type hinting NumPy arrays. The library primarily provides these compatibility types for static analysis on projects using older NumPy versions, where native type hints might be less mature."},"warnings":[{"fix":"Ensure you are using `numpy-typing-compat` version `v20250730` or newer for consistent `CanArray` behavior, especially when targeting NumPy versions older than 2.1.","message":"The `CanArray` type's subscriptability (e.g., `CanArray[np.float64]`) and its direct aliasing behavior for `np.ndarray` were improved and fixed in `v20250729` and `v20250730`. Earlier versions of `numpy-typing-compat` might exhibit unexpected behavior or errors when using `CanArray` with older NumPy versions (specifically `<2.1`).","severity":"gotcha","affected_versions":"<v20250730"},{"fix":"Understand that `numpy-typing-compat` fills gaps in older NumPy's typing. For newer NumPy versions, consult NumPy's official typing documentation first.","message":"This library is designed as a *compatibility layer* for *older* NumPy versions to improve static typing. If you are using recent versions of NumPy (e.g., NumPy 2.x), many of these types might already be natively available or handled differently. Relying solely on `numpy-typing-compat` without checking NumPy's native typing capabilities for your specific version might introduce unnecessary abstractions or mask issues.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Upgrade to `numpy-typing-compat` `v20250818` or newer, and ensure your Python environment is `3.11` or above.","message":"Prior to `v20250818`, users running `numpy-typing-compat` on Python versions older than 3.10 could encounter `ImportError` runtime errors. The library now officially requires Python 3.11 or newer.","severity":"breaking","affected_versions":"<v20250818` when used with Python `<3.10`. (Note: current version requires `Python >=3.11`.)"},{"fix":"Update `numpy-typing-compat` to `v20250725` or newer to access these specific typing aliases.","message":"Certain types like `long`, `ulong`, `StringDType`, and `array_api` were added to the `numpy_typing_compat` namespace in `v20250725`. If your codebase relies on these specific aliases for older NumPy compatibility, ensure you're on a sufficiently recent version.","severity":"gotcha","affected_versions":"<v20250725"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}