Repository navigation
Read argparse environment configs from the registry - #1716
Conversation
[Medium impact] The PR appears safe to merge within its stated scope. SummaryThe CLI now reads typed environment configs from
Review was based on source inspection; tests were not run. Reviews (1) · Last reviewed commit: "Read argparse environment configs from t..." · Reviewed by Greptile |
🤖 Isaac Lab-Arena Review BotSummaryThis PR changes how the argparse adapter finds a typed factory's config: it now reads it from CI limitation: the test jobs for Findings🔵 Improvement: isaaclab_arena_environments/cli.py:54
VerdictShip it |
The argparse adapter in isaaclab_arena_environments/cli.py found each typed factory's config through a private _legacy_argparse_cfg_type attribute. That repeated the config type that the factory's ArenaEnvironmentFactory[Cfg] base already declares and that @register_environment records in EnvironmentRegistry. Read it from EnvironmentRegistry instead and drop the 19 duplicate declarations. A typed factory registered with @register_environment no longer needs the private attribute. Before, one without it made the environment subparser build assert for every environment. Signed-off-by: Clemens Volk <cvolk@nvidia.com>
d83682b to
c08442a
Compare
…es yet --external_environment_class_path registers its class without a config, so a typed factory loaded through it, including a subclass of a first-party factory, fails the config lookup. The next PR makes the flag use the factory's own @register_environment registration. Until then, the error says that the flag does not support typed factories. Signed-off-by: Clemens Volk <cvolk@nvidia.com>
c08442a to
926c11d
Compare
Let environments defined outside Arena use a config dataclass and a
factory class with `build(cfg)`, like the built-in environments, and
delete `ExampleEnvironmentBase`.
Loading the class with `--external_environment_class_path` registers it
with its config, so it doesn't need `@register_environment`. Migrate
both external environment examples.
## Why this change?
Environments defined outside Arena subclass `ExampleEnvironmentBase`. It
builds the environment from an argparse `Namespace` in
`get_env(args_cli)` and declares its flags in `add_cli_args(parser)`.
Both methods are abstract, so every external environment writes its own
argparse flags by hand, one `parser.add_argument` per value, while
built-in environments get their flags generated from their config.
Built-in environments already moved to a config dataclass and
`build(cfg)`. External environments have no config dataclass, so the
Experiment Runner cannot build them.
An external environment also cannot be written as a config dataclass and
a factory class today. `--external_environment_class_path` imports the
class and registers it with plain `EnvironmentRegistry.register()`,
which records no config.
The next PR lets an Experiment select an external environment by class
path. It needs external environments to have a config dataclass.
## What changes?
- **Breaking change:** `ExampleEnvironmentBase` is deleted. An
environment defined outside Arena now needs a config dataclass that
subclasses `ArenaEnvironmentCfg` and a factory that subclasses
`ArenaEnvironmentFactory[Cfg]` and implements `build(cfg)`, as built-in
environments do. It does not need `@register_environment`: loading the
factory by path registers it. `add_environment_cli_args` and
`build_environment_from_cli` in `isaaclab_arena_environments/cli.py`
lose their `ExampleEnvironmentBase` branches. Flags for external
environments are generated from their config fields, as for built-in
ones.
- **Breaking change:** `--external_environment_class_path` takes
`module_path.ClassName`, like `policy.type` and `--policy_type`, instead
of `module_path:ClassName`. A class path with a colon fails with the
dotted form to use, and `import_class_from_path` loses its `separator`
argument.
- `import_external_environment_factory` replaces
`parse_and_return_external_environment_from_string`. It registers the
built-in environments first, imports the class with
`import_class_from_path(path)`, asserts that it is an
`ArenaEnvironmentFactory` subclass, and registers it with its config
under its `name`. A factory that its module already registered with
`@register_environment` is used as is. A factory whose name is taken,
for example by a built-in environment, gets an error that names the
class that owns it. Repeated calls return the same class.
`add_example_environments_cli_args` and the Isaac Lab
`environment_registration_callback` use it. The `--help` text of the
flag now describes the `module_path.ClassName` format instead of asking
for an environment name.
- `basic.py` and `advanced.py` in
`isaaclab_arena_examples/external_environments/` each have a config
dataclass and a factory class. Their config has an `object` field that
defaults to `cracker_box`, so the `--object` flag keeps working. The
commands in their module docstrings now show the generated `--object`
flag.
- `external_environments.rst` opens with what a user can do, describes
the two classes, and shows the run command with one class path example.
The steps for migrating from `ExampleEnvironmentBase` go into the
release migration guide, not this page. The locomanipulation and
reinforcement learning environment setup pages showed outdated
`ExampleEnvironmentBase` copies of their classes. They now include the
real classes with `literalinclude`.
- The flag tests in `test_environment_cfgs.py` import a module written
to `tmp_path`, so only the flag can register it. The Isaac Lab callback
test now selects the advanced example, which no earlier test imports.
`test_typed_experiment_ignores_unconfigured_external_environment` uses a
local factory, because the basic example now registers itself with a
config.
Experiments still cannot select external environments. The next PR adds
that. Until then, use the Policy Runner to evaluate one.
## Usage example
Before:
```python
import argparse
from isaaclab_arena_environments.example_environment_base import ExampleEnvironmentBase
class ExternalFrankaTableEnvironment(ExampleEnvironmentBase):
name: str = "franka_table"
def get_env(self, args_cli: argparse.Namespace):
...
pick_up_object = self.asset_registry.get_asset_by_name(args_cli.object)()
...
@staticmethod
def add_cli_args(parser: argparse.ArgumentParser) -> None:
parser.add_argument("--object", type=str, default="cracker_box")
```
After:
```python
from dataclasses import dataclass
from isaaclab_arena.environments.arena_environment_factory import ArenaEnvironmentCfg, ArenaEnvironmentFactory
@DataClass
class ExternalFrankaTableEnvironmentCfg(ArenaEnvironmentCfg):
object: str = "cracker_box"
class ExternalFrankaTableEnvironment(ArenaEnvironmentFactory[ExternalFrankaTableEnvironmentCfg]):
name: str = "franka_table"
def build(self, cfg: ExternalFrankaTableEnvironmentCfg):
...
pick_up_object = self.asset_registry.get_asset_by_name(cfg.object)()
...
```
The flags come from the config fields, so only the class path in the
command changes, from `module_path:ClassName` to
`module_path.ClassName`:
```bash
python isaaclab_arena/evaluation/policy_runner.py \
--policy_type zero_action --num_steps 10 \
--external_environment_class_path isaaclab_arena_examples.external_environments.basic.ExternalFrankaTableEnvironment \
franka_table --object cracker_box
```
After #1730, an Experiment can also select the environment by its class
path and set `object` in YAML.
## Validation
- On `main` at `21b745cbb6` (#1716 merged). The changes are the same as
when they were tested on #1716's head. Locally,
`test_environment_cfgs.py`, `test_external_environment.py` (which runs
both examples, now without the decorator, through `policy_runner.py`
with the flag, and the Isaac Lab callback),
`test_environment_registry.py`, `test_isaaclab_interop.py`,
`test_arena_experiment_config_loader.py`, `test_environment_runner.py`,
`test_legacy_graph_environment_cli.py`,
`test_all_environments_have_default_args`, and the docs build with `-W`
pass.
- Removing the `import_external_environment_factory` call from
`add_example_environments_cli_args` made the flag test fail with
`invalid choice`, so the test depends on the flag. The test, now
`test_external_environment_flag_registers_the_factory`, loads a factory
only through the flag, with and without `@register_environment`.
- A script checked each field type listed in `external_environments.rst`
against `add_dataclass_cli_args`. The listed types parse correctly. A
plain `Enum`, an `IntEnum`, and `list[Literal[...]]` reject every value.
`list[bool]` and `Literal[True, False]` read `False` as `True`, and a
nested dataclass gets the text in its first field. `tuple`, `dict`, and
`int | str` fields fail while the parser is built.
- The `literalinclude` blocks on the locomanipulation and reinforcement
learning pages show the intended lines.
- Not checked: `environment_runner.py` with
`--external_environment_class_path`, because it needs the Kit GUI, and
Isaac Lab training scripts with an external environment beyond
`test_external_environment_registration_callback`.
---------
Signed-off-by: Clemens Volk <cvolk@nvidia.com>
Read each typed environment's argparse config from
EnvironmentRegistryand remove the_legacy_argparse_cfg_typeattribute from the 19 environment factories.Why this change?
The argparse adapter in
isaaclab_arena_environments/cli.pyfound a factory's config through a private_legacy_argparse_cfg_typeclass attribute. Each factory already declares its config in itsArenaEnvironmentFactory[Cfg]base, and@register_environmentrecords it inEnvironmentRegistry, so the attribute repeated it.A typed factory registered with
@register_environmentbut without the attribute broke the environment CLI.add_example_environments_cli_argsbuilds a subparser for every registered environment, so that one factory made the build assert for all of them.This is the first of three PRs that replace #1453 and let an Experiment select an external environment by class path. The next one, #1729, turns the external environment examples into typed factories, which will not set the private attribute. The third, #1730, adds the class path to Experiments.
What changes?
_registered_environment_cfg_typeincli.pyreads the config fromEnvironmentRegistry.get_environment_cfg_types()by the factory'sname.add_environment_cli_argsand_environment_cfg_from_cliuse it._legacy_argparse_cfg_type.test_every_registered_cli_adapter_uses_its_typed_cfg_defaultsno longer compares the attribute with the registry. It still checks that every registered factory gets its registered config back from the CLI.--external_environment_class_pathstill registers its class without a config, so a typed factory loaded through that flag gets no CLI flags yet. Building the environment subcommands then fails with an error saying that the flag does not support typed factories yet. This includes a subclass of a first-party factory, which onmaininherited_legacy_argparse_cfg_typeand worked. The next PR, #1729, makes the flag use the factory's own@register_environmentregistration.Validation
For all 19 registered environments, the registry lookup returns the same config class as the removed attribute on
main.In the Arena Docker image, the related tests pass when run locally:
test_environment_cfgs.py,test_environment_registry.py,test_legacy_graph_environment_cli.py,test_arena_env_graph_spec.py,test_isaaclab_interop.py,test_arena_experiment_config_loader.py,test_asset_registry.py,test_environment_runner.py, andtest_all_environments_have_default_args.Rebased onto
mainata170ca3610(2026-11-27). No_legacy_argparse_cfg_typeis left. The same tests andtest_external_environment.pypass locally.