Skip to content

fix: defer np.typing annotation evaluation - #128

Open
xyf5432 wants to merge 1 commit into
google:masterfrom
xyf5432:fix/numpy-typing-future-annotations
Open

fix: defer np.typing annotation evaluation#128
xyf5432 wants to merge 1 commit into
google:masterfrom
xyf5432:fix/numpy-typing-future-annotations

Conversation

@xyf5432

@xyf5432 xyf5432 commented Aug 15, 2026

Copy link
Copy Markdown

Fixes #127

Problem

np.typing.XXX is only accessible on numpy < 2.0 if numpy.typing has been explicitly imported earlier. Without a version constraint forcing numpy >= 2, users with numpy 1.x installed can hit:

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

when the annotation is evaluated at import time.

Fix

File: ffn/training/examples.py

+ from __future__ import annotations
+
  def update_seeds(self, batched_seeds: np.typing.ArrayLike):

(Optional follow-up: the annotation is also narrower than the documented contract — the docstring says the argument may be an "array-like object backed by accelerator memory" (i.e. a JAX array), which np.typing.ArrayLike does not formally cover. np.typing.ArrayLike | jax.typing.ArrayLike would describe the accepted inputs more accurately.)

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. Contrast tests confirmed that importing pandas 3.x or xarray beforehand resolves it, while importing scipy does not — i.e. whether training crashes today depends on import order, which this fix eliminates.
  • With from __future__ import annotations, the annotation is no longer evaluated when BatchExampleIter is defined, making the training entry points (train.py, JAX path) safe on every numpy version.
  • The change is annotation-only — no behavioral change.

The np.typing.ArrayLike annotation on update_seeds is evaluated when
the BatchExampleIter class body executes at module import time. On
numpy < 2.0, np.typing only resolves after an explicit
'import numpy.typing', which nothing on the training import path
provides. Add 'from __future__ import annotations' so the annotation
is deferred and the training entry points work on every numpy version,
independent of import order.
@google-cla

google-cla Bot commented Aug 15, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

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 training entry points on numpy<2

1 participant