Skip to content

Conversation

@pseudo-rnd-thoughts
Copy link
Member

Description

As noted by DLR-RM/stable-baselines3#1327 (comment), there are significant backward compatibility issues with #292

This PR updates EnvSpec such that it is a "full description of the whole env". Using the EnvSpec in make is now identical to make(str) as before. Critically, the difference with make in v0.27 is that the EnvSpec changes with each wrapper applied, including the default wrappers. As a result, env.unwrapped.spec's EnvSpec has all parameters disabled such that gym.make(env.unwrapped.spec) is equivalent to just the base environment.

The advantage of this implementation is that users can define an environment, env and perfectly replicate each stage

while env is not env.unwrapped:
    recreated_env = gym.make(env.spec)
    assert recreated_env == env  # (this line will fail due to the environment equivalent not having a custom function)
    assert recreated_env.spec == env.spec

    env = env.env

Additionally, this PR updates the testing for make such that we check for both strings and EnvSpec.

@pseudo-rnd-thoughts
Copy link
Member Author

Note, this breaks backward compatibility for TimeLimit as previously users were able to do TimeLimit(env) if the env.spec.max_episode_steps is not None as internally TimeLimit would use this value.
Due to the changes in this PR, we need to remove this functionality and personally, I don't think that it makes much sense before.

Copy link
Member

@rodrigodelazcano rodrigodelazcano left a comment

Choose a reason for hiding this comment

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

The PR looks ready to be merged. I've been testing it with Minari to recreate environments from json environment specs and it works perfectly. Thanks @pseudo-rnd-thoughts !

Copy link
Member

@RedTachyon RedTachyon left a comment

Choose a reason for hiding this comment

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

Two minor comments, but I wasn't able to look at this in proper depth. If it works for "normal" usage (i.e. with the branch installed you can create an env, get the spec, recreate it and do regular stuff without any obvious bugs or redundant warnings), and it works for Minari purposes, then looks good to me as well

assert env_spec == recreated_env_spec


def test_wrapped_env_entry_point():
Copy link
Member

Choose a reason for hiding this comment

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

Why is this test removed?

Copy link
Member Author

Choose a reason for hiding this comment

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

I moved the tests to test_make.py I believe

# to avoid reference issues we deepcopy the prior environments spec and add the new information
env_spec = deepcopy(env_spec)
env_spec.applied_wrappers += (wrapper_spec,)
if self._cached_spec is None:
Copy link
Member

Choose a reason for hiding this comment

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

Minor comment for slight unspagghettification - it might be better to do

if self._cached_spec is not None:
    return self._cached_spec

env_spec = self.env.spec
...

Basically just flip the condition and do an early return to remove one level of indentation from the whole code block.

Copy link
Member Author

Choose a reason for hiding this comment

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

Updated

@pseudo-rnd-thoughts pseudo-rnd-thoughts merged commit 9d8db14 into Farama-Foundation:main Mar 8, 2023
snorkelopstesting1-a11y pushed a commit to snorkel-marlin-repos/Farama-Foundation_Gymnasium_pr_355_c87acba3-9097-48db-a58e-a747fc777152 that referenced this pull request Oct 2, 2025
Original PR #355 by pseudo-rnd-thoughts
Original: Farama-Foundation/Gymnasium#355
snorkelopstesting1-a11y added a commit to snorkel-marlin-repos/Farama-Foundation_Gymnasium_pr_355_c87acba3-9097-48db-a58e-a747fc777152 that referenced this pull request Oct 2, 2025
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.

3 participants