Skip to content

Enhances lazy export to automatically parses absolute external package export, and relative wild export. - #4882

Merged
ooctipus merged 7 commits into
isaac-sim:developfrom
ooctipus:feature/develop/nicer_lazy_export
Mar 9, 2026
Merged

ooctipus merged 7 commits into
isaac-sim:developfrom
ooctipus:feature/develop/nicer_lazy_export

Conversation

@ooctipus

@ooctipus ooctipus commented Mar 8, 2026

Copy link
Copy Markdown
Collaborator

Description

This PR makes lazy export even nicer

  • Refactored lazy_export() in isaaclab.utils.module to auto-infer fallback packages from absolute wildcard imports (from pkg import *) and eagerly resolve relative wildcard re-exports (from .mod import *) found in .pyi stubs. The packages= argument is deprecated.

  • Added from isaaclab.envs.mdp import * to all 14 task MDP init.pyi stubs, fixing broken type hints for base MDP symbols (e.g. mdp.time_out, mdp.joint_pos).

  • Simplified all task MDP init.py files from lazy_export(packages=[...]) to lazy_export().
    Created two missing .pyi stubs for isaaclab.devices.openxr.retargeters modules.

  • Added test_lazy_export_stubs.py — a static analysis test that discovers all lazy_export() call sites and ensures none pass arguments.

Fixes # (issue)

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • Documentation update

Screenshots

Please attach before and after screenshots of the change if applicable.

Checklist

  • I have read and understood the contribution guidelines
  • I have run the pre-commit checks with ./isaaclab.sh --format
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • I have updated the changelog and the corresponding version in the extension's config/extension.toml file
  • I have added my name to the CONTRIBUTORS.md or my name already exists there

@ooctipus
ooctipus requested a review from Mayankm96 as a code owner March 8, 2026 23:20
@github-actions github-actions Bot added enhancement New feature or request isaac-mimic Related to Isaac Mimic team isaac-lab Related to Isaac Lab team labels Mar 8, 2026
@ooctipus ooctipus changed the title Made lazy export a lot more automatic in parsing absolute external package export, and relative wild export. Enhances lazy export to automatically parses absolute external package export, and relative wild export. Mar 8, 2026
@ooctipus
ooctipus requested review from kellyguo11 and removed request for Mayankm96 March 8, 2026 23:22
@greptile-apps

greptile-apps Bot commented Mar 8, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR refactors the lazy_export() mechanism in isaaclab.utils.module so that the .pyi stub is the single source of truth: absolute wildcard imports (from pkg import *) are automatically parsed to infer fallback packages, and relative wildcard imports (from .mod import *) are eagerly resolved at import time. The packages= argument is deprecated. All 14 task MDP __init__.py files are simplified to lazy_export(), and two missing .pyi stubs for the OpenXR retargeters are added. A new static-analysis test enforces the no-argument convention going forward.

Key changes and observations:

  • _filter_stub is replaced by the more capable _parse_stub, which extracts fallback_packages and relative_wildcards in a single AST pass.
  • The eager relative-wildcard import loop (for from .mod import *) has no ImportError / ModuleNotFoundError guard, unlike the lazy fallback path which wraps every importlib.import_module call in a try/except. This is a regression in robustness for optional submodules.
  • The new test test_lazy_export_stubs.py runs ast.parse at module-import/collection time without catching SyntaxError; a malformed __init__.py anywhere in the source tree would crash pytest collection.
  • isaaclab_tasks version jumps from 1.5.1 → 1.5.3, skipping 1.5.2, which is already documented in the CHANGELOG but was apparently never applied to the toml.

Confidence Score: 3/5

  • The PR is safe in its current deployments (no existing stubs use from .mod import *), but contains a robustness gap in the new eager-import path and a minor test-collection fragility.
  • Score reflects that the core logic change is sound and well-tested for the patterns used in the current codebase, but the missing ImportError guard in the relative-wildcard eager-import loop is a latent bug that will surface the first time a from .mod import * stub entry is used with an optional or failing submodule. The test file also has a fragile ast.parse call at collection time.
  • source/isaaclab/isaaclab/utils/module.py (relative-wildcard import error handling), source/isaaclab_tasks/test/test_lazy_export_stubs.py (SyntaxError guard in ast.parse)

Important Files Changed

Filename Overview
source/isaaclab/isaaclab/utils/module.py Core refactor: _filter_stub replaced by _parse_stub which extracts fallback_packages from absolute wildcard imports and relative_wildcards from relative wildcard imports in a single AST pass. lazy_export() now infers everything from the stub and deprecates the packages= argument. Eager relative-wildcard import loop lacks ImportError handling.
source/isaaclab_tasks/test/test_lazy_export_stubs.py New static-analysis test that walks the source tree at collection time and fails for any lazy_export(...) call that still passes arguments. Discovery guard is a nice addition; ast.parse is called without catching SyntaxError, which would crash collection if a malformed file is encountered.
source/isaaclab/isaaclab/devices/openxr/retargeters/init.pyi New .pyi stub created to pair with the updated __init__.py; moves the isaaclab_teleop fallback package declaration out of the Python call into the stub as from isaaclab_teleop.deprecated.openxr.retargeters import *.
source/isaaclab/isaaclab/devices/openxr/retargeters/manipulator/init.pyi New .pyi stub for the manipulator retargeter sub-package, declaring from isaaclab_teleop.deprecated.openxr.retargeters.manipulator import * as the fallback source.
source/isaaclab_tasks/isaaclab_tasks/manager_based/drone_arl/mdp/init.pyi Adds both from isaaclab.envs.mdp import * and from isaaclab_contrib.mdp import *, correctly reflecting the two fallback packages that were previously passed explicitly to lazy_export(packages=[...]) in the corresponding .py file.
source/isaaclab_tasks/config/extension.toml Version bumped from 1.5.1 to 1.5.3, which skips 1.5.2 — a release entry that already exists in the CHANGELOG but apparently was never reflected in this file.
source/isaaclab/config/extension.toml Version bumped from 4.5.8 to 4.5.9 to accompany the lazy_export refactor; straightforward change.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A["lazy_export() called"] --> B{packages= arg?}
    B -- yes --> C[Emit DeprecationWarning\nadd to fallback_packages]
    B -- no --> D{.pyi stub exists?}
    C --> D
    D -- yes --> E["_parse_stub(stub_file)"]
    E --> F["filtered_path\nfallback_packages from\n'from pkg import *'\nrelative_wildcards from\n'from .mod import *'"]
    F --> G["lazy.attach_stub()\n→ stub_getattr, stub_dir, __all__"]
    G --> H{relative_wildcards?}
    H -- yes --> I["eagerly importlib.import_module()\npopulate mod.__dict__ + __all__\n⚠️ no ImportError guard"]
    H -- no --> J{fallback_packages?}
    I --> J
    D -- no --> K["__all__ = []"]
    K --> J
    J -- yes --> L["define _pkg_getattr / _pkg_dir\nclosures (with try/except)"]
    L --> M{has_stub?}
    M -- yes --> N["__getattr__ chains\nstub → pkg fallback\n__dir__ merges both"]
    M -- no --> O["__getattr__ = _pkg_getattr\n__dir__ = _pkg_dir"]
    J -- no --> P{has_stub?}
    P -- yes --> Q["__getattr__ = stub_getattr\n__dir__ = stub_dir"]
    P -- no --> R["__getattr__ raises AttributeError\n__dir__ returns []"]
    N --> S["setattr mod: __getattr__, __dir__, __all__"]
    O --> S
    Q --> S
    R --> S
Loading

Last reviewed commit: 71be9fd

Comment on lines +135 to +142
for rel_mod_name in relative_wildcards:
fq_name = f"{package_name}.{rel_mod_name}"
sub = importlib.import_module(fq_name)
exported = getattr(sub, "__all__", [n for n in dir(sub) if not n.startswith("_")])
for name in exported:
mod.__dict__[name] = getattr(sub, name)
if name not in __all__:
__all__.append(name)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No error handling for ImportError in eager relative-wildcard loop

relative_wildcards submodules are eagerly imported at lazy_export() call time with no try/except. If any listed submodule fails to import (e.g. an optional dependency is missing, or the submodule itself has an import-time error), this will immediately propagate an unhandled ImportError, causing the caller's import to fail hard.

Contrast this with the _pkg_getattr / _pkg_dir closures further below (lines 148–166), which explicitly continue on ImportError / ModuleNotFoundError. The same defensive treatment should apply here:

    for rel_mod_name in relative_wildcards:
        fq_name = f"{package_name}.{rel_mod_name}"
        try:
            sub = importlib.import_module(fq_name)
        except (ImportError, ModuleNotFoundError):
            continue
        exported = getattr(sub, "__all__", [n for n in dir(sub) if not n.startswith("_")])
        for name in exported:
            mod.__dict__[name] = getattr(sub, name)
            if name not in __all__:
                __all__.append(name)

if "lazy_export" not in source:
continue

tree = ast.parse(source, filename=str(path))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ast.parse called without SyntaxError guard at module level

_find_lazy_export_calls() is executed at module import time (line 57). If any __init__.py in the scanned tree contains a syntax error, ast.parse will raise SyntaxError, which will propagate out of the function and prevent _VIOLATIONS from ever being assigned. This would crash pytest during collection with a confusing NameError / unhandled exception rather than a clean test failure.

Suggested change
tree = ast.parse(source, filename=str(path))
try:
tree = ast.parse(source, filename=str(path))
except SyntaxError:
continue

@@ -1,7 +1,7 @@
[package]

# Note: Semantic Versioning is used: https://semver.org/
version = "1.5.1"
version = "1.5.3"

# Description

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Version bump skips 1.5.2

The CHANGELOG.rst on the base branch already documents a 1.5.2 (2026-03-05) release entry, yet extension.toml on the base branch is still at 1.5.1. This PR therefore bumps directly from 1.5.1 → 1.5.3, skipping 1.5.2 entirely. If 1.5.2 was intentionally never published, the CHANGELOG entry should be removed or merged into 1.5.3; otherwise the published package will miss version 1.5.2, which can confuse downstream consumers.

@ooctipus
ooctipus requested a review from jtigue-bdai as a code owner March 8, 2026 23:40
@github-actions github-actions Bot added the documentation Improvements or additions to documentation label Mar 8, 2026
@ooctipus
ooctipus force-pushed the feature/develop/nicer_lazy_export branch from 68c3613 to 5fe25cf Compare March 9, 2026 08:16

@AntoineRichard AntoineRichard left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm really not certain doing things like this is a good idea:

from .rewards import joint_pos_target_l2
from isaaclab.envs.mdp import *

In general, we could just import the mdps we want, I don't see why we need a from isaaclab.envs.mdp import * here. It's extra lazy. This code knows perfectly well which term it wants.

Comment thread docs/source/refs/contributing.rst Resolved
Comment thread docs/source/refs/contributing.rst Resolved
@pascal-roth

Copy link
Copy Markdown
Collaborator

LGTM

@ooctipus
ooctipus force-pushed the feature/develop/nicer_lazy_export branch from 5fe25cf to 79a97c3 Compare March 9, 2026 21:59
@ooctipus
ooctipus force-pushed the feature/develop/nicer_lazy_export branch from f407d7b to 38f9e62 Compare March 9, 2026 22:58
@ooctipus
ooctipus merged commit 39278a2 into isaac-sim:develop Mar 9, 2026
9 of 10 checks passed
@ooctipus
ooctipus deleted the feature/develop/nicer_lazy_export branch October 14, 2026 14:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation enhancement New feature or request isaac-lab Related to Isaac Lab team isaac-mimic Related to Isaac Mimic team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants