Skip to content

fix: import numpy.typing for numpy<2 compatibility - #161

Open
xyf5432 wants to merge 1 commit into
Ancientea:mainfrom
xyf5432:fix/numpy-typing-import
Open

fix: import numpy.typing for numpy<2 compatibility#161
xyf5432 wants to merge 1 commit into
Ancientea:mainfrom
xyf5432:fix/numpy-typing-import

Conversation

@xyf5432

@xyf5432 xyf5432 commented Aug 15, 2026

Copy link
Copy Markdown

Fix #160

Problem

np.typing.XXX is only accessible on numpy < 2.0 if numpy.typing has been explicitly imported earlier.

The README's uv sync install path is safe — uv.lock pins numpy 2.x, so uv users always get a numpy where the annotations resolve. However, pyproject.toml declares numpy with no version constraint, and pip users are not covered by the lock file: a pip install on a machine with numpy 1.x already present (pip keeps existing versions without upgrading) can hit:

AttributeError: module 'numpy' has no attribute 'typing'

when the annotations in this module are evaluated at import time. Declaring a bare dependency without a floor also leaves future lock re-resolutions exposed — a change in the dependency graph that resolves numpy < 2 would silently break the import.

Fix

Add an explicit import numpy.typing before the first use. This injects typing into numpy.__dict__, making all subsequent np.typing.XXX accesses work on every supported numpy version — no reliance on import order or upstream dependency side effects.

  import numpy as np
+ import numpy.typing
  import torch

Verification

  • Verified the failure mechanism in isolation on numpy 1.26.4: np.typing.ArrayLike raises AttributeError: module 'numpy' has no attribute 'typing' unless numpy.typing has been imported first; with the import added, the same expression resolves successfully.
  • The modified file passes a syntax check.
  • The change only adds an import — no behavioral change for environments where the annotation already resolved (numpy >= 2, or when an upstream dependency already imported numpy.typing).

np.typing is only accessible as a module attribute on numpy >= 2.0.
On numpy 1.x, function annotations in core/predict.py that reference
np.typing.ArrayLike raise AttributeError at import time unless
numpy.typing has been imported first. Add an explicit import so the
module works on every numpy version, independent of import order.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Compatibility Issue] Missing numpy>=2 constraint — np.typing may crash on numpy<2

1 participant