Fix numpy typing future annotations - #1425
Open
xyf5432 wants to merge 2 commits into
Open
Conversation
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.
Description
Add
from __future__ import annotations(PEP 563) tosrc/deepforest/visualize.py(5np.typing.NDArrayannotation sites at lines 17/20/136/306/373) andsrc/deepforest/datasets/training.py(1 site at line 119), so type annotations are no longer evaluated at module import time.np.typing.XXXis only accessible on numpy < 2.0 ifnumpy.typinghas been explicitly imported earlier.pyproject.tomldeclaresnumpyandpandaswithout version constraints, so environments with numpy 1.x are allowed, where these annotations raise:when evaluated at module import time.
Whether the crash manifests depends on the installed pandas version: pandas 3.0.x imports
numpy.typingat module level (protecting subsequent accesses), while pandas 2.x does not (the import sits underTYPE_CHECKING). Sincemain.pyimports pandas before the affected modules, pandas 3.x masks the issue and pandas 2.x leaves it exposed. Deferring annotation evaluation removes this version-dependent reliance entirely.Verification:
np.typing.NDArray[np.bool_]raisesAttributeError: module 'numpy' has no attribute 'typing'unlessnumpy.typinghas been imported firstnumpy.typingat import, pandas 2.x does notimport deepforestreproduction with the complete dependency set (torch, pytorch-lightning, etc.) has not been run; other dependencies could theoretically provide the same incidental protectionRelated Issue(s)
Fixes #1422
AI-Assisted Development
AI tools used:
Deepseek — used for mechanism investigation, dependency analysis, and drafting this PR; the one-line change in each file was manually reviewed and validated.