Skip to content

Fix numpy typing future annotations - #1425

Open
xyf5432 wants to merge 2 commits into
weecology:mainfrom
xyf5432:fix/numpy-typing-future-annotations
Open

Fix numpy typing future annotations#1425
xyf5432 wants to merge 2 commits into
weecology:mainfrom
xyf5432:fix/numpy-typing-future-annotations

Conversation

@xyf5432

@xyf5432 xyf5432 commented Aug 16, 2026

Copy link
Copy Markdown

Description

Add from __future__ import annotations (PEP 563) to src/deepforest/visualize.py (5 np.typing.NDArray annotation sites at lines 17/20/136/306/373) and src/deepforest/datasets/training.py (1 site at line 119), so type annotations are no longer evaluated at module import time.

np.typing.XXX is only accessible on numpy < 2.0 if numpy.typing has been explicitly imported earlier. pyproject.toml declares numpy and pandas without version constraints, so environments with numpy 1.x are allowed, where these annotations raise:

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

when evaluated at module import time.

Whether the crash manifests depends on the installed pandas version: pandas 3.0.x imports numpy.typing at module level (protecting subsequent accesses), while pandas 2.x does not (the import sits under TYPE_CHECKING). Since main.py imports 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:

  • Verified the failure mechanism in isolation on numpy 1.26.4: np.typing.NDArray[np.bool_] raises AttributeError: module 'numpy' has no attribute 'typing' unless numpy.typing has been imported first
  • Verified the version-dependent protection gap: pandas 3.0.x triggers numpy.typing at import, pandas 2.x does not
  • The change is annotation-only — no behavioral change
  • Note: a full import deepforest reproduction with the complete dependency set (torch, pytorch-lightning, etc.) has not been run; other dependencies could theoretically provide the same incidental protection

Related Issue(s)

Fixes #1422

AI-Assisted Development

  • [√] I used AI tools Deepseek in developing this PR
  • [√] I understand all the code I'm submitting
  • [√] I have reviewed and validated all AI-generated code

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.

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 crashes on numpy<2

1 participant