fix: defer np.typing annotation evaluation - #128
Open
xyf5432 wants to merge 1 commit into
Open
Conversation
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.
|
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. |
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.
Fixes #127
Problem
np.typing.XXXis only accessible on numpy < 2.0 ifnumpy.typinghas been explicitly imported earlier. Without a version constraint forcing numpy >= 2, users with numpy 1.x installed can hit:when the annotation is evaluated at import time.
Fix
File:
ffn/training/examples.py(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.ArrayLikedoes not formally cover.np.typing.ArrayLike | jax.typing.ArrayLikewould describe the accepted inputs more accurately.)Verification
np.typing.ArrayLikeraisesAttributeError: module 'numpy' has no attribute 'typing'unlessnumpy.typinghas 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.from __future__ import annotations, the annotation is no longer evaluated whenBatchExampleIteris defined, making the training entry points (train.py, JAX path) safe on every numpy version.