Lazifies IsaacLab import system#4741
Draft
ooctipus wants to merge 8 commits intoisaac-sim:developfrom
Draft
Conversation
Collaborator
AntoineRichard
left a comment
There was a problem hiding this comment.
I'm a bit concerned about user friendlyness of this PR. This looks inelegant, and not very user friendly.
import lazy_loader as lazy
__getattr__, __dir__, __all__ = lazy.attach(
__name__,
submod_attrs={
"actuator_base": ["ActuatorBase"],
"actuator_base_cfg": ["ActuatorBaseCfg"],
"actuator_net": ["ActuatorNetLSTM", "ActuatorNetMLP"],
"actuator_net_cfg": ["ActuatorNetLSTMCfg", "ActuatorNetMLPCfg"],
"actuator_pd": ["DCMotor", "DelayedPDActuator", "IdealPDActuator", "ImplicitActuator", "RemotizedPDActuator"],
"actuator_pd_cfg": [
"DCMotorCfg",
"DelayedPDActuatorCfg",
"IdealPDActuatorCfg",
"ImplicitActuatorCfg",
"RemotizedPDActuatorCfg",
],
},
)How about we do something like that:
# lazy_imports.py
from __future__ import annotations
from collections.abc import Iterable
import sys
import lazy_loader as lazy
def lazy_export(*imports: tuple[str, str | Iterable[str]]) -> None:
"""
Usage in __init__.py:
from .lazy_imports import lazy_export
lazy_export(("foo", "A"), ("bar", ["B", "C"]))
"""
caller_globals = sys._getframe(1).f_globals
package_name = caller_globals["__name__"]
submod_attrs: dict[str, list[str]] = {}
for submod, names in imports:
submod_attrs[submod] = [names] if isinstance(names, str) else list(names)
__getattr__, __dir__, __all__ = lazy.attach(package_name, submod_attrs=submod_attrs)
mod = sys.modules[package_name]
mod.__getattr__ = __getattr__
mod.__dir__ = __dir__
mod.__all__ = __all__This would simplify the code to:
from .lazy_imports import lazy_export
lazy_export(
("actuator_base", "ActuatorBase"),
("actuator_base_cfg", "ActuatorBaseCfg"),
("actuator_net", ["ActuatorNetLSTM", "ActuatorNetMLP"]),
("actuator_net_cfg", ["ActuatorNetLSTMCfg", "ActuatorNetMLPCfg"]),
("actuator_pd", ["DCMotor", "DelayedPDActuator", "IdealPDActuator", "ImplicitActuator", "RemotizedPDActuator"]),
("actuator_pd_cfg", ["DCMotorCfg", "DelayedPDActuatorCfg", "IdealPDActuatorCfg", "ImplicitActuatorCfg", "RemotizedPDActuatorCfg"]),
)
Collaborator
|
ooctipus#6 I have a PR that makes this a bit nicer. |
Collaborator
|
This would likely solve #4742. But I'm wondering if we should have these imports inside our inits. What do we gain from these? |
Better looking imports
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.
Description
This PR improves lazy-loading and config-only import behavior so environment config construction stays backend-free and deterministic.
isaaclab.utils.module.attach_cascadingwith strict fail-fast imports.__copy__/__deepcopy__behavior inResolvableString.isaaclab.sim.utilscascading lookup to prefer lightweight modules first.terminations) and removes a backend-heavy module-level import path in pick-place terminations.Fixes # (issue)
Type of change
Screenshots
Please attach before and after screenshots of the change if applicable.
Checklist
pre-commitchecks with./isaaclab.sh --formatconfig/extension.tomlfileCONTRIBUTORS.mdor my name already exists there