Repository navigation
Defer scipy imports so import spatialmath is faster - #244
Conversation
|
Thank you for your interest in SMTB and for this contribution. It's great to see a first PR from you! I've reviewed it and run the suite locally: scipy is no longer in One note for context: #234 stays open since the SymPy half is still to do. This PR is already part of the integration branch in #246. Thanks again, and I'd be happy to see a PR for the SymPy part too if you're interested. |
Add a test to ensure scipy is not loaded when importing spatialmath.
e5802a5 to
cf3c701
Compare
|
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Part of #234, covering the SymPy half only. The SciPy work remains separate in #244. `import spatialmath` no longer loads SymPy just to check scalar types. `issymbol()` checks the already-loaded module, and scalar/vector checks use it dynamically so importing SymPy after SpatialMath works too. The other eager SymPy imports in vector norms, object-matrix determinants and 2D pose extraction are deferred. Explicit symbolic helpers load the optional dependency when called; explicit access to `Symbol` and `symtype` remains available. Added nine isolated-process test cases covering numeric imports and operations, both import orders, symbolic arithmetic and transforms, explicit helpers and legacy aliases, and numeric use with SymPy blocked. Validation: Python 3.10/Linux, `MPLBACKEND=Agg python -m pytest tests/ -p no:xvfb -q`: 389 passed, 3 skipped, 18 subtests passed. No timing claim is made here. Other Python versions and platforms were not tested. **Note on star-imports:** `from spatialmath.base.symbolic import *` no longer brings `sympy`, `Symbol` and `symtype` into the importing namespace, since star-imports don't go through the module-level `__getattr__`. Explicit `from spatialmath.base.symbolic import Symbol, symtype` still works, and no users of the old behavior turned up in RTB, MVTB, bdsim or Swift.
Part of #234 (the SciPy half; SymPy is not changed here).
spatialmath/base/quaternions.pyandspatialmath/spline.pyimportedscipy.interpolate/scipy.spatial.transformat module level, soimport spatialmathpulled in scipy even though onlyqrand(with a theta range),SplineSE3andBSplineSE3use it. These imports now happen inside those functions. Theinterpolate.interp1dreturn annotation stays as a string underTYPE_CHECKING.Measured with
python -X importtime -c "import spatialmath"on one machine, single run each: 1.42 s before, 0.40 s after.scipyis no longer insys.modulesafterimport spatialmath.sympystill is, so the remaining part of #234 (issymbol/argcheck) is untouched.Added
tests/test_import.py, which imports spatialmath in a subprocess and checks that no scipy module is loaded. It fails on master and passes here.Run: full
tests/suite withMPLBACKEND=Agg, 348 passed, 3 skipped; black 23.10.0 clean on the changed files;qrand(theta_range=...)and importingSplineSE3/BSplineSE3smoke-checked. Not run: the spline classes were not exercised by hand beyond what the existing tests cover, and only Python 3 on Linux was tried.