fix: import numpy.typing for numpy<2 compatibility - #161
Open
xyf5432 wants to merge 1 commit into
Open
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix #160
Problem
np.typing.XXXis only accessible on numpy < 2.0 ifnumpy.typinghas been explicitly imported earlier.The README's
uv syncinstall path is safe —uv.lockpins numpy 2.x, so uv users always get a numpy where the annotations resolve. However,pyproject.tomldeclaresnumpywith 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: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.typingbefore the first use. This injectstypingintonumpy.__dict__, making all subsequentnp.typing.XXXaccesses work on every supported numpy version — no reliance on import order or upstream dependency side effects.import numpy as np + import numpy.typing import torchVerification
np.typing.ArrayLikeraisesAttributeError: module 'numpy' has no attribute 'typing'unlessnumpy.typinghas been imported first; with the import added, the same expression resolves successfully.numpy.typing).