Skip to content

fix: make numpy.typing import explicit - #46

Merged
domrachev03 merged 4 commits into
based-robotics:mainfrom
xyf5432:fix/numpy-typing-import
Aug 23, 2026
Merged

fix: make numpy.typing import explicit#46
domrachev03 merged 4 commits into
based-robotics:mainfrom
xyf5432:fix/numpy-typing-import

Conversation

@xyf5432

@xyf5432 xyf5432 commented Aug 15, 2026

Copy link
Copy Markdown
Contributor

Fixes #45

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: mjinx/typing.py

  from __future__ import annotations
+ import numpy.typing
  import jax.typing

  ArrayLike: TypeAlias = np.typing.ArrayLike | jax.typing.ArrayLike

(ArrayLike: TypeAlias = ... is a module-level expression — PEP 563 does not defer it. Today the alias only resolves through an incidental side effect: trimesh, pulled in via mujoco-mjx, imports numpy.typing at startup. The explicit import removes that hidden reliance.)

Verification

  • Verified in a real environment (numpy 1.26.4 + jax 0.7.0 + mujoco 3.11.0 + mujoco-mjx 3.11.0): from mjinx import typing succeeds today because trimesh (a transitive dependency via mujoco-mjx) executes import numpy.typing at import time — line 31's np.typing.ArrayLike only resolves through that side effect.
  • With the explicit import, the ArrayLike alias no longer depends on trimesh's internals or the import order inside mujoco-mjx — if that transitive chain changes in the future, this module keeps working.
  • The change only adds an import — no behavioral change.

ArrayLike: TypeAlias = np.typing.ArrayLike | jax.typing.ArrayLike is a
module-level expression evaluated at import time. On numpy < 2.0,
np.typing only resolves if numpy.typing has been imported earlier;
today this happens incidentally via trimesh (a transitive dependency
of mujoco-mjx), which imports numpy.typing at startup. Add an explicit
import so the alias no longer depends on that side effect — if
mujoco-mjx changes its dependencies or import order, this module keeps
working.
mjx.kinematics expects mjx Model/Data wrapper types (which expose the
.impl attribute), not the raw mujoco.MjModel. Wrap the model with
mjx.put_model() before calling kinematics.
jax.Array is not a collections.abc.Sequence, so mypy rejected
self.target_q = jnp.zeros(...) against the Sequence-typed setters.
The setters already convert via jnp.array(), so accepting
jnp.ndarray | Sequence is accurate and keeps list inputs valid.
pytest >= 9 strictly validates filterwarnings as a linelist; the
single-string form makes pytest fail at startup. The list form is the
standard syntax and is accepted by both pytest 8 and 9.
@xyf5432

xyf5432 commented Aug 21, 2026

Copy link
Copy Markdown
Contributor Author

I dug into the three failing CI checks and pushed fixes (commits b4c7482, 7239283, d60c179). All were pre-existing issues, unrelated to the one-line import numpy.typing change:

  1. Unit testsAttributeError: 'mujoco._structs.MjModel' object has no attribute 'impl' in test_get_distance_with_hfield. Root cause: mjx.kinematics() expects the mjx wrapper types (mujoco.mjx.Model/Data, which expose .impl), but the test passes the raw mujoco.MjModel. This is version-independent (the .impl reference exists in mujoco_mjx 3.7+), so pinning mujoco was not a fix. The test now wraps the model with mjx.put_model() before calling kinematics — verified the full unittest suite passes.
  2. test (3.10) — pytest 9.1.1 rejects the single-string filterwarnings in pyproject.toml at startup. Changed it to the standard list form (accepted by pytest 8 and 9) — pytest tests/example_tests now runs and passes (4 passed).
  3. mypy — two Incompatible types in assignment errors: jax.Array is not a collections.abc.Sequence, so assigning jnp.zeros(...) to the Sequence-typed setters failed. Widened the setter signatures to jnp.ndarray | Sequence (the setters already convert via jnp.array()) — mypy mjinx now reports no issues.

All three checks were verified locally on the same dependency versions CI resolves (mujoco 3.12.0, pytest 9.1.1, jax 0.11.1).

@domrachev03

domrachev03 commented Aug 22, 2026

Copy link
Copy Markdown
Member

Thank you for addressing them! Looks good to me

@domrachev03
domrachev03 merged commit db53314 into based-robotics:main Aug 23, 2026
8 checks passed
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] np.typing.ArrayLike relies on an incidental import side effect — fragile on numpy<2

2 participants