From 3413b045341a918aa4b1a9879c8a785abc815368 Mon Sep 17 00:00:00 2001 From: Omar Younis <42100908+younik@users.noreply.github.com> Date: Sun, 8 Jan 2023 20:56:52 +0100 Subject: [PATCH] ENH: allow metadata["render_modes"] to be a set (#248) --- docs/tutorials/environment_creation.py | 3 +-- gymnasium/core.py | 2 +- gymnasium/envs/box2d/bipedal_walker.py | 2 +- gymnasium/envs/box2d/car_racing.py | 4 ++-- gymnasium/envs/box2d/lunar_lander.py | 2 +- gymnasium/envs/classic_control/acrobot.py | 2 +- gymnasium/envs/classic_control/cartpole.py | 2 +- .../envs/classic_control/continuous_mountain_car.py | 2 +- gymnasium/envs/classic_control/mountain_car.py | 2 +- gymnasium/envs/classic_control/pendulum.py | 2 +- gymnasium/envs/mujoco/ant.py | 4 ++-- gymnasium/envs/mujoco/ant_v3.py | 4 ++-- gymnasium/envs/mujoco/ant_v4.py | 4 ++-- gymnasium/envs/mujoco/half_cheetah.py | 4 ++-- gymnasium/envs/mujoco/half_cheetah_v3.py | 4 ++-- gymnasium/envs/mujoco/half_cheetah_v4.py | 4 ++-- gymnasium/envs/mujoco/hopper.py | 4 ++-- gymnasium/envs/mujoco/hopper_v3.py | 4 ++-- gymnasium/envs/mujoco/hopper_v4.py | 4 ++-- gymnasium/envs/mujoco/humanoid.py | 4 ++-- gymnasium/envs/mujoco/humanoid_v3.py | 4 ++-- gymnasium/envs/mujoco/humanoid_v4.py | 4 ++-- gymnasium/envs/mujoco/humanoidstandup.py | 4 ++-- gymnasium/envs/mujoco/humanoidstandup_v4.py | 4 ++-- gymnasium/envs/mujoco/inverted_double_pendulum.py | 4 ++-- gymnasium/envs/mujoco/inverted_double_pendulum_v4.py | 4 ++-- gymnasium/envs/mujoco/inverted_pendulum.py | 4 ++-- gymnasium/envs/mujoco/inverted_pendulum_v4.py | 4 ++-- gymnasium/envs/mujoco/mujoco_env.py | 4 ++-- gymnasium/envs/mujoco/pusher.py | 4 ++-- gymnasium/envs/mujoco/pusher_v4.py | 4 ++-- gymnasium/envs/mujoco/reacher.py | 4 ++-- gymnasium/envs/mujoco/reacher_v4.py | 4 ++-- gymnasium/envs/mujoco/swimmer.py | 4 ++-- gymnasium/envs/mujoco/swimmer_v3.py | 4 ++-- gymnasium/envs/mujoco/swimmer_v4.py | 4 ++-- gymnasium/envs/mujoco/walker2d.py | 4 ++-- gymnasium/envs/mujoco/walker2d_v3.py | 4 ++-- gymnasium/envs/mujoco/walker2d_v4.py | 4 ++-- gymnasium/envs/phys2d/cartpole.py | 2 +- gymnasium/envs/phys2d/pendulum.py | 2 +- gymnasium/envs/toy_text/blackjack.py | 2 +- gymnasium/envs/toy_text/cliffwalking.py | 2 +- gymnasium/envs/toy_text/frozen_lake.py | 2 +- gymnasium/envs/toy_text/taxi.py | 2 +- gymnasium/utils/passive_env_checker.py | 4 ++-- gymnasium/wrappers/compatibility.py | 2 +- gymnasium/wrappers/human_rendering.py | 4 +++- gymnasium/wrappers/render_collection.py | 2 +- tests/envs/test_make.py | 9 ++++----- tests/envs/utils_envs.py | 6 +++--- tests/test_core.py | 6 +++--- tests/testing_env.py | 2 +- tests/utils/test_passive_env_checker.py | 12 ++++++------ tests/utils/test_play.py | 2 +- tests/wrappers/test_passive_env_checker.py | 2 +- tests/wrappers/test_video_recorder.py | 4 ++-- 57 files changed, 103 insertions(+), 103 deletions(-) diff --git a/docs/tutorials/environment_creation.py b/docs/tutorials/environment_creation.py index 19e65145a..b1c703090 100644 --- a/docs/tutorials/environment_creation.py +++ b/docs/tutorials/environment_creation.py @@ -100,7 +100,7 @@ class GridWorldEnv(gym.Env): - metadata = {"render_modes": ["human", "rgb_array"], "render_fps": 4} + metadata = {"render_modes": {"human", "rgb_array"}, "render_fps": 4} def __init__(self, render_mode=None, size=5): self.size = size # The size of the square grid @@ -130,7 +130,6 @@ def __init__(self, render_mode=None, size=5): 3: np.array([0, -1]), } - assert render_mode is None or render_mode in self.metadata["render_modes"] self.render_mode = render_mode """ diff --git a/gymnasium/core.py b/gymnasium/core.py index f2bc4063f..b989711cd 100644 --- a/gymnasium/core.py +++ b/gymnasium/core.py @@ -47,7 +47,7 @@ class Env(Generic[ObsType, ActType]): """ # Set this in SOME subclasses - metadata: dict[str, Any] = {"render_modes": []} + metadata: dict[str, Any] = {"render_modes": {}} # define render_mode if your environment supports rendering render_mode: str | None = None reward_range = (-float("inf"), float("inf")) diff --git a/gymnasium/envs/box2d/bipedal_walker.py b/gymnasium/envs/box2d/bipedal_walker.py index 7ebc7d120..5de3f5976 100644 --- a/gymnasium/envs/box2d/bipedal_walker.py +++ b/gymnasium/envs/box2d/bipedal_walker.py @@ -166,7 +166,7 @@ class BipedalWalker(gym.Env, EzPickle): """ metadata = { - "render_modes": ["human", "rgb_array"], + "render_modes": {"human", "rgb_array"}, "render_fps": FPS, } diff --git a/gymnasium/envs/box2d/car_racing.py b/gymnasium/envs/box2d/car_racing.py index 4a9950447..19ac988b1 100644 --- a/gymnasium/envs/box2d/car_racing.py +++ b/gymnasium/envs/box2d/car_racing.py @@ -193,11 +193,11 @@ class CarRacing(gym.Env, EzPickle): """ metadata = { - "render_modes": [ + "render_modes": { "human", "rgb_array", "state_pixels", - ], + }, "render_fps": FPS, } diff --git a/gymnasium/envs/box2d/lunar_lander.py b/gymnasium/envs/box2d/lunar_lander.py index 13094c7f1..65e204ded 100644 --- a/gymnasium/envs/box2d/lunar_lander.py +++ b/gymnasium/envs/box2d/lunar_lander.py @@ -190,7 +190,7 @@ class LunarLander(gym.Env, EzPickle): """ metadata = { - "render_modes": ["human", "rgb_array"], + "render_modes": {"human", "rgb_array"}, "render_fps": FPS, } diff --git a/gymnasium/envs/classic_control/acrobot.py b/gymnasium/envs/classic_control/acrobot.py index f807f6748..1d50dc56a 100644 --- a/gymnasium/envs/classic_control/acrobot.py +++ b/gymnasium/envs/classic_control/acrobot.py @@ -141,7 +141,7 @@ class AcrobotEnv(Env): """ metadata = { - "render_modes": ["human", "rgb_array"], + "render_modes": {"human", "rgb_array"}, "render_fps": 15, } diff --git a/gymnasium/envs/classic_control/cartpole.py b/gymnasium/envs/classic_control/cartpole.py index 939de53eb..f215418e8 100644 --- a/gymnasium/envs/classic_control/cartpole.py +++ b/gymnasium/envs/classic_control/cartpole.py @@ -82,7 +82,7 @@ class CartPoleEnv(gym.Env[np.ndarray, Union[int, np.ndarray]]): """ metadata = { - "render_modes": ["human", "rgb_array"], + "render_modes": {"human", "rgb_array"}, "render_fps": 50, } diff --git a/gymnasium/envs/classic_control/continuous_mountain_car.py b/gymnasium/envs/classic_control/continuous_mountain_car.py index 6908c7b31..ed825c31e 100644 --- a/gymnasium/envs/classic_control/continuous_mountain_car.py +++ b/gymnasium/envs/classic_control/continuous_mountain_car.py @@ -105,7 +105,7 @@ class Continuous_MountainCarEnv(gym.Env): """ metadata = { - "render_modes": ["human", "rgb_array"], + "render_modes": {"human", "rgb_array"}, "render_fps": 30, } diff --git a/gymnasium/envs/classic_control/mountain_car.py b/gymnasium/envs/classic_control/mountain_car.py index 213a2c047..de03e0c74 100644 --- a/gymnasium/envs/classic_control/mountain_car.py +++ b/gymnasium/envs/classic_control/mountain_car.py @@ -97,7 +97,7 @@ class MountainCarEnv(gym.Env): """ metadata = { - "render_modes": ["human", "rgb_array"], + "render_modes": {"human", "rgb_array"}, "render_fps": 30, } diff --git a/gymnasium/envs/classic_control/pendulum.py b/gymnasium/envs/classic_control/pendulum.py index 01147587a..99aee931c 100644 --- a/gymnasium/envs/classic_control/pendulum.py +++ b/gymnasium/envs/classic_control/pendulum.py @@ -93,7 +93,7 @@ class PendulumEnv(gym.Env): """ metadata = { - "render_modes": ["human", "rgb_array"], + "render_modes": {"human", "rgb_array"}, "render_fps": 30, } diff --git a/gymnasium/envs/mujoco/ant.py b/gymnasium/envs/mujoco/ant.py index 348512f1b..fc2885895 100644 --- a/gymnasium/envs/mujoco/ant.py +++ b/gymnasium/envs/mujoco/ant.py @@ -7,11 +7,11 @@ class AntEnv(MuJocoPyEnv, utils.EzPickle): metadata = { - "render_modes": [ + "render_modes": { "human", "rgb_array", "depth_array", - ], + }, "render_fps": 20, } diff --git a/gymnasium/envs/mujoco/ant_v3.py b/gymnasium/envs/mujoco/ant_v3.py index 65f51bc36..9de97e1ba 100644 --- a/gymnasium/envs/mujoco/ant_v3.py +++ b/gymnasium/envs/mujoco/ant_v3.py @@ -12,11 +12,11 @@ class AntEnv(MuJocoPyEnv, utils.EzPickle): metadata = { - "render_modes": [ + "render_modes": { "human", "rgb_array", "depth_array", - ], + }, "render_fps": 20, } diff --git a/gymnasium/envs/mujoco/ant_v4.py b/gymnasium/envs/mujoco/ant_v4.py index bfb2290e5..a2dbaeb91 100644 --- a/gymnasium/envs/mujoco/ant_v4.py +++ b/gymnasium/envs/mujoco/ant_v4.py @@ -182,11 +182,11 @@ class AntEnv(MujocoEnv, utils.EzPickle): """ metadata = { - "render_modes": [ + "render_modes": { "human", "rgb_array", "depth_array", - ], + }, "render_fps": 20, } diff --git a/gymnasium/envs/mujoco/half_cheetah.py b/gymnasium/envs/mujoco/half_cheetah.py index f4ae8e791..fa440e324 100644 --- a/gymnasium/envs/mujoco/half_cheetah.py +++ b/gymnasium/envs/mujoco/half_cheetah.py @@ -7,11 +7,11 @@ class HalfCheetahEnv(MuJocoPyEnv, utils.EzPickle): metadata = { - "render_modes": [ + "render_modes": { "human", "rgb_array", "depth_array", - ], + }, "render_fps": 20, } diff --git a/gymnasium/envs/mujoco/half_cheetah_v3.py b/gymnasium/envs/mujoco/half_cheetah_v3.py index d6328b1a1..043ab0236 100644 --- a/gymnasium/envs/mujoco/half_cheetah_v3.py +++ b/gymnasium/envs/mujoco/half_cheetah_v3.py @@ -14,11 +14,11 @@ class HalfCheetahEnv(MuJocoPyEnv, utils.EzPickle): metadata = { - "render_modes": [ + "render_modes": { "human", "rgb_array", "depth_array", - ], + }, "render_fps": 20, } diff --git a/gymnasium/envs/mujoco/half_cheetah_v4.py b/gymnasium/envs/mujoco/half_cheetah_v4.py index 2227a4820..3225bb37e 100644 --- a/gymnasium/envs/mujoco/half_cheetah_v4.py +++ b/gymnasium/envs/mujoco/half_cheetah_v4.py @@ -136,11 +136,11 @@ class HalfCheetahEnv(MujocoEnv, utils.EzPickle): """ metadata = { - "render_modes": [ + "render_modes": { "human", "rgb_array", "depth_array", - ], + }, "render_fps": 20, } diff --git a/gymnasium/envs/mujoco/hopper.py b/gymnasium/envs/mujoco/hopper.py index 11f4c4080..1b6078869 100644 --- a/gymnasium/envs/mujoco/hopper.py +++ b/gymnasium/envs/mujoco/hopper.py @@ -7,11 +7,11 @@ class HopperEnv(MuJocoPyEnv, utils.EzPickle): metadata = { - "render_modes": [ + "render_modes": { "human", "rgb_array", "depth_array", - ], + }, "render_fps": 125, } diff --git a/gymnasium/envs/mujoco/hopper_v3.py b/gymnasium/envs/mujoco/hopper_v3.py index 330aeb0a8..81dfae44d 100644 --- a/gymnasium/envs/mujoco/hopper_v3.py +++ b/gymnasium/envs/mujoco/hopper_v3.py @@ -17,11 +17,11 @@ class HopperEnv(MuJocoPyEnv, utils.EzPickle): metadata = { - "render_modes": [ + "render_modes": { "human", "rgb_array", "depth_array", - ], + }, "render_fps": 125, } diff --git a/gymnasium/envs/mujoco/hopper_v4.py b/gymnasium/envs/mujoco/hopper_v4.py index 895acb329..e5b44d165 100644 --- a/gymnasium/envs/mujoco/hopper_v4.py +++ b/gymnasium/envs/mujoco/hopper_v4.py @@ -142,11 +142,11 @@ class HopperEnv(MujocoEnv, utils.EzPickle): """ metadata = { - "render_modes": [ + "render_modes": { "human", "rgb_array", "depth_array", - ], + }, "render_fps": 125, } diff --git a/gymnasium/envs/mujoco/humanoid.py b/gymnasium/envs/mujoco/humanoid.py index 59370b211..f627ed5ec 100644 --- a/gymnasium/envs/mujoco/humanoid.py +++ b/gymnasium/envs/mujoco/humanoid.py @@ -13,11 +13,11 @@ def mass_center(model, sim): class HumanoidEnv(MuJocoPyEnv, utils.EzPickle): metadata = { - "render_modes": [ + "render_modes": { "human", "rgb_array", "depth_array", - ], + }, "render_fps": 67, } diff --git a/gymnasium/envs/mujoco/humanoid_v3.py b/gymnasium/envs/mujoco/humanoid_v3.py index a83d5c161..e9dba0353 100644 --- a/gymnasium/envs/mujoco/humanoid_v3.py +++ b/gymnasium/envs/mujoco/humanoid_v3.py @@ -21,11 +21,11 @@ def mass_center(model, sim): class HumanoidEnv(MuJocoPyEnv, utils.EzPickle): metadata = { - "render_modes": [ + "render_modes": { "human", "rgb_array", "depth_array", - ], + }, "render_fps": 67, } diff --git a/gymnasium/envs/mujoco/humanoid_v4.py b/gymnasium/envs/mujoco/humanoid_v4.py index 2dbd605ac..7812324c8 100644 --- a/gymnasium/envs/mujoco/humanoid_v4.py +++ b/gymnasium/envs/mujoco/humanoid_v4.py @@ -216,11 +216,11 @@ class HumanoidEnv(MujocoEnv, utils.EzPickle): """ metadata = { - "render_modes": [ + "render_modes": { "human", "rgb_array", "depth_array", - ], + }, "render_fps": 67, } diff --git a/gymnasium/envs/mujoco/humanoidstandup.py b/gymnasium/envs/mujoco/humanoidstandup.py index 574a500a1..998d19e68 100644 --- a/gymnasium/envs/mujoco/humanoidstandup.py +++ b/gymnasium/envs/mujoco/humanoidstandup.py @@ -7,11 +7,11 @@ class HumanoidStandupEnv(MuJocoPyEnv, utils.EzPickle): metadata = { - "render_modes": [ + "render_modes": { "human", "rgb_array", "depth_array", - ], + }, "render_fps": 67, } diff --git a/gymnasium/envs/mujoco/humanoidstandup_v4.py b/gymnasium/envs/mujoco/humanoidstandup_v4.py index d10c9c40f..874cbb089 100644 --- a/gymnasium/envs/mujoco/humanoidstandup_v4.py +++ b/gymnasium/envs/mujoco/humanoidstandup_v4.py @@ -192,11 +192,11 @@ class HumanoidStandupEnv(MujocoEnv, utils.EzPickle): """ metadata = { - "render_modes": [ + "render_modes": { "human", "rgb_array", "depth_array", - ], + }, "render_fps": 67, } diff --git a/gymnasium/envs/mujoco/inverted_double_pendulum.py b/gymnasium/envs/mujoco/inverted_double_pendulum.py index d0b6ca4b7..35662b9d1 100644 --- a/gymnasium/envs/mujoco/inverted_double_pendulum.py +++ b/gymnasium/envs/mujoco/inverted_double_pendulum.py @@ -7,11 +7,11 @@ class InvertedDoublePendulumEnv(MuJocoPyEnv, utils.EzPickle): metadata = { - "render_modes": [ + "render_modes": { "human", "rgb_array", "depth_array", - ], + }, "render_fps": 20, } diff --git a/gymnasium/envs/mujoco/inverted_double_pendulum_v4.py b/gymnasium/envs/mujoco/inverted_double_pendulum_v4.py index fd732dbee..373e2c698 100644 --- a/gymnasium/envs/mujoco/inverted_double_pendulum_v4.py +++ b/gymnasium/envs/mujoco/inverted_double_pendulum_v4.py @@ -125,11 +125,11 @@ class InvertedDoublePendulumEnv(MujocoEnv, utils.EzPickle): """ metadata = { - "render_modes": [ + "render_modes": { "human", "rgb_array", "depth_array", - ], + }, "render_fps": 20, } diff --git a/gymnasium/envs/mujoco/inverted_pendulum.py b/gymnasium/envs/mujoco/inverted_pendulum.py index 343515d09..a34b0c654 100644 --- a/gymnasium/envs/mujoco/inverted_pendulum.py +++ b/gymnasium/envs/mujoco/inverted_pendulum.py @@ -7,11 +7,11 @@ class InvertedPendulumEnv(MuJocoPyEnv, utils.EzPickle): metadata = { - "render_modes": [ + "render_modes": { "human", "rgb_array", "depth_array", - ], + }, "render_fps": 25, } diff --git a/gymnasium/envs/mujoco/inverted_pendulum_v4.py b/gymnasium/envs/mujoco/inverted_pendulum_v4.py index 3ff34d510..2ff82b5b5 100644 --- a/gymnasium/envs/mujoco/inverted_pendulum_v4.py +++ b/gymnasium/envs/mujoco/inverted_pendulum_v4.py @@ -94,11 +94,11 @@ class InvertedPendulumEnv(MujocoEnv, utils.EzPickle): """ metadata = { - "render_modes": [ + "render_modes": { "human", "rgb_array", "depth_array", - ], + }, "render_fps": 25, } diff --git a/gymnasium/envs/mujoco/mujoco_env.py b/gymnasium/envs/mujoco/mujoco_env.py index 2a3882157..7ccc27182 100644 --- a/gymnasium/envs/mujoco/mujoco_env.py +++ b/gymnasium/envs/mujoco/mujoco_env.py @@ -56,11 +56,11 @@ def __init__( self.frame_skip = frame_skip - assert self.metadata["render_modes"] == [ + assert self.metadata["render_modes"] == { "human", "rgb_array", "depth_array", - ], self.metadata["render_modes"] + }, self.metadata["render_modes"] assert ( int(np.round(1.0 / self.dt)) == self.metadata["render_fps"] ), f'Expected value: {int(np.round(1.0 / self.dt))}, Actual value: {self.metadata["render_fps"]}' diff --git a/gymnasium/envs/mujoco/pusher.py b/gymnasium/envs/mujoco/pusher.py index 2c446bdc2..7fa3a269a 100644 --- a/gymnasium/envs/mujoco/pusher.py +++ b/gymnasium/envs/mujoco/pusher.py @@ -7,11 +7,11 @@ class PusherEnv(MuJocoPyEnv, utils.EzPickle): metadata = { - "render_modes": [ + "render_modes": { "human", "rgb_array", "depth_array", - ], + }, "render_fps": 20, } diff --git a/gymnasium/envs/mujoco/pusher_v4.py b/gymnasium/envs/mujoco/pusher_v4.py index 28d2a9b9b..22c940287 100644 --- a/gymnasium/envs/mujoco/pusher_v4.py +++ b/gymnasium/envs/mujoco/pusher_v4.py @@ -140,11 +140,11 @@ class PusherEnv(MujocoEnv, utils.EzPickle): """ metadata = { - "render_modes": [ + "render_modes": { "human", "rgb_array", "depth_array", - ], + }, "render_fps": 20, } diff --git a/gymnasium/envs/mujoco/reacher.py b/gymnasium/envs/mujoco/reacher.py index 342a82a29..a16d6c2a4 100644 --- a/gymnasium/envs/mujoco/reacher.py +++ b/gymnasium/envs/mujoco/reacher.py @@ -7,11 +7,11 @@ class ReacherEnv(MuJocoPyEnv, utils.EzPickle): metadata = { - "render_modes": [ + "render_modes": { "human", "rgb_array", "depth_array", - ], + }, "render_fps": 50, } diff --git a/gymnasium/envs/mujoco/reacher_v4.py b/gymnasium/envs/mujoco/reacher_v4.py index 162f7a7cd..a4d3f2a89 100644 --- a/gymnasium/envs/mujoco/reacher_v4.py +++ b/gymnasium/envs/mujoco/reacher_v4.py @@ -122,11 +122,11 @@ class ReacherEnv(MujocoEnv, utils.EzPickle): """ metadata = { - "render_modes": [ + "render_modes": { "human", "rgb_array", "depth_array", - ], + }, "render_fps": 50, } diff --git a/gymnasium/envs/mujoco/swimmer.py b/gymnasium/envs/mujoco/swimmer.py index a82bab533..baa253f58 100644 --- a/gymnasium/envs/mujoco/swimmer.py +++ b/gymnasium/envs/mujoco/swimmer.py @@ -7,11 +7,11 @@ class SwimmerEnv(MuJocoPyEnv, utils.EzPickle): metadata = { - "render_modes": [ + "render_modes": { "human", "rgb_array", "depth_array", - ], + }, "render_fps": 25, } diff --git a/gymnasium/envs/mujoco/swimmer_v3.py b/gymnasium/envs/mujoco/swimmer_v3.py index 2b710f650..184c1e792 100644 --- a/gymnasium/envs/mujoco/swimmer_v3.py +++ b/gymnasium/envs/mujoco/swimmer_v3.py @@ -12,11 +12,11 @@ class SwimmerEnv(MuJocoPyEnv, utils.EzPickle): metadata = { - "render_modes": [ + "render_modes": { "human", "rgb_array", "depth_array", - ], + }, "render_fps": 25, } diff --git a/gymnasium/envs/mujoco/swimmer_v4.py b/gymnasium/envs/mujoco/swimmer_v4.py index ea8c9c921..53e276cbb 100644 --- a/gymnasium/envs/mujoco/swimmer_v4.py +++ b/gymnasium/envs/mujoco/swimmer_v4.py @@ -125,11 +125,11 @@ class SwimmerEnv(MujocoEnv, utils.EzPickle): """ metadata = { - "render_modes": [ + "render_modes": { "human", "rgb_array", "depth_array", - ], + }, "render_fps": 25, } diff --git a/gymnasium/envs/mujoco/walker2d.py b/gymnasium/envs/mujoco/walker2d.py index 74d0890df..be8526f0e 100644 --- a/gymnasium/envs/mujoco/walker2d.py +++ b/gymnasium/envs/mujoco/walker2d.py @@ -7,11 +7,11 @@ class Walker2dEnv(MuJocoPyEnv, utils.EzPickle): metadata = { - "render_modes": [ + "render_modes": { "human", "rgb_array", "depth_array", - ], + }, "render_fps": 125, } diff --git a/gymnasium/envs/mujoco/walker2d_v3.py b/gymnasium/envs/mujoco/walker2d_v3.py index 26c11d0e5..9d5346ab4 100644 --- a/gymnasium/envs/mujoco/walker2d_v3.py +++ b/gymnasium/envs/mujoco/walker2d_v3.py @@ -15,11 +15,11 @@ class Walker2dEnv(MuJocoPyEnv, utils.EzPickle): metadata = { - "render_modes": [ + "render_modes": { "human", "rgb_array", "depth_array", - ], + }, "render_fps": 125, } diff --git a/gymnasium/envs/mujoco/walker2d_v4.py b/gymnasium/envs/mujoco/walker2d_v4.py index 329687dba..83e7368b8 100644 --- a/gymnasium/envs/mujoco/walker2d_v4.py +++ b/gymnasium/envs/mujoco/walker2d_v4.py @@ -147,11 +147,11 @@ class Walker2dEnv(MujocoEnv, utils.EzPickle): """ metadata = { - "render_modes": [ + "render_modes": { "human", "rgb_array", "depth_array", - ], + }, "render_fps": 125, } diff --git a/gymnasium/envs/phys2d/cartpole.py b/gymnasium/envs/phys2d/cartpole.py index dfe4d3a70..f603aec53 100644 --- a/gymnasium/envs/phys2d/cartpole.py +++ b/gymnasium/envs/phys2d/cartpole.py @@ -240,7 +240,7 @@ def render_close(self, render_state: RenderStateType) -> None: class CartPoleJaxEnv(FunctionalJaxEnv, EzPickle): """Jax-based implementation of the CartPole environment.""" - metadata = {"render_modes": ["rgb_array"], "render_fps": 50} + metadata = {"render_modes": {"rgb_array"}, "render_fps": 50} def __init__(self, render_mode: str | None = None, **kwargs: Any): """Constructor for the CartPole where the kwargs are applied to the functional environment.""" diff --git a/gymnasium/envs/phys2d/pendulum.py b/gymnasium/envs/phys2d/pendulum.py index 9a1f1f8db..3f1d7af2a 100644 --- a/gymnasium/envs/phys2d/pendulum.py +++ b/gymnasium/envs/phys2d/pendulum.py @@ -192,7 +192,7 @@ def render_close(self, render_state: RenderStateType): class PendulumJaxEnv(FunctionalJaxEnv, EzPickle): """Jax-based pendulum environment using the functional version as base.""" - metadata = {"render_modes": ["rgb_array"], "render_fps": 30} + metadata = {"render_modes": {"rgb_array"}, "render_fps": 30} def __init__(self, render_mode: str | None = None, **kwargs: Any): """Constructor where the kwargs are passed to the base environment to modify the parameters.""" diff --git a/gymnasium/envs/toy_text/blackjack.py b/gymnasium/envs/toy_text/blackjack.py index e911c8637..62c025339 100644 --- a/gymnasium/envs/toy_text/blackjack.py +++ b/gymnasium/envs/toy_text/blackjack.py @@ -145,7 +145,7 @@ class BlackjackEnv(gym.Env): """ metadata = { - "render_modes": ["human", "rgb_array"], + "render_modes": {"human", "rgb_array"}, "render_fps": 4, } diff --git a/gymnasium/envs/toy_text/cliffwalking.py b/gymnasium/envs/toy_text/cliffwalking.py index 7b3c855b4..9d7423255 100644 --- a/gymnasium/envs/toy_text/cliffwalking.py +++ b/gymnasium/envs/toy_text/cliffwalking.py @@ -91,7 +91,7 @@ class CliffWalkingEnv(Env): """ metadata = { - "render_modes": ["human", "rgb_array", "ansi"], + "render_modes": {"human", "rgb_array", "ansi"}, "render_fps": 4, } diff --git a/gymnasium/envs/toy_text/frozen_lake.py b/gymnasium/envs/toy_text/frozen_lake.py index fdebe72b9..f72c868a3 100644 --- a/gymnasium/envs/toy_text/frozen_lake.py +++ b/gymnasium/envs/toy_text/frozen_lake.py @@ -211,7 +211,7 @@ class FrozenLakeEnv(Env): """ metadata = { - "render_modes": ["human", "ansi", "rgb_array"], + "render_modes": {"human", "ansi", "rgb_array"}, "render_fps": 4, } diff --git a/gymnasium/envs/toy_text/taxi.py b/gymnasium/envs/toy_text/taxi.py index 8cf9cb663..f249737ba 100644 --- a/gymnasium/envs/toy_text/taxi.py +++ b/gymnasium/envs/toy_text/taxi.py @@ -152,7 +152,7 @@ class TaxiEnv(Env): """ metadata = { - "render_modes": ["human", "ansi", "rgb_array"], + "render_modes": {"human", "ansi", "rgb_array"}, "render_fps": 4, } diff --git a/gymnasium/utils/passive_env_checker.py b/gymnasium/utils/passive_env_checker.py index 46d220ae0..03e1ccca3 100644 --- a/gymnasium/utils/passive_env_checker.py +++ b/gymnasium/utils/passive_env_checker.py @@ -341,9 +341,9 @@ def env_render_passive_checker(env): "No render modes was declared in the environment (env.metadata['render_modes'] is None or not defined), you may have trouble when calling `.render()`." ) else: - if not isinstance(render_modes, (list, tuple)): + if not isinstance(render_modes, (set, list, tuple)): logger.warn( - f"Expects the render_modes to be a sequence (i.e. list, tuple), actual type: {type(render_modes)}" + f"Expects the render_modes to be a set, list or tuple, actual type: {type(render_modes)}" ) elif not all(isinstance(mode, str) for mode in render_modes): logger.warn( diff --git a/gymnasium/wrappers/compatibility.py b/gymnasium/wrappers/compatibility.py index 4cb325abf..d446e2e79 100644 --- a/gymnasium/wrappers/compatibility.py +++ b/gymnasium/wrappers/compatibility.py @@ -68,7 +68,7 @@ def __init__(self, old_env: LegacyEnv, render_mode: Optional[str] = None): "The `gymnasium.make(..., apply_api_compatibility=...)` parameter is deprecated and will be removed in v28. " "Instead use `gym.make('GymV22Environment-v0', env_name=...)` or `from shimmy import GymV26CompatibilityV0`" ) - self.metadata = getattr(old_env, "metadata", {"render_modes": []}) + self.metadata = getattr(old_env, "metadata", {"render_modes": {}}) self.render_mode = render_mode self.reward_range = getattr(old_env, "reward_range", None) self.spec = getattr(old_env, "spec", None) diff --git a/gymnasium/wrappers/human_rendering.py b/gymnasium/wrappers/human_rendering.py index ff38c3e80..1ebdda041 100644 --- a/gymnasium/wrappers/human_rendering.py +++ b/gymnasium/wrappers/human_rendering.py @@ -62,7 +62,9 @@ def __init__(self, env): self.metadata = copy.deepcopy(self.env.metadata) if "human" not in self.metadata["render_modes"]: - self.metadata["render_modes"].append("human") + self.metadata["render_modes"] = set(self.metadata["render_modes"]).add( + "human" + ) @property def render_mode(self): diff --git a/gymnasium/wrappers/render_collection.py b/gymnasium/wrappers/render_collection.py index ee23cc939..0cc4db4f5 100644 --- a/gymnasium/wrappers/render_collection.py +++ b/gymnasium/wrappers/render_collection.py @@ -26,7 +26,7 @@ def __init__(self, env: gym.Env, pop_frames: bool = True, reset_clean: bool = Tr self.metadata = copy.deepcopy(self.env.metadata) if f"{self.env.render_mode}_list" not in self.metadata["render_modes"]: - self.metadata["render_modes"].append(f"{self.env.render_mode}_list") + set(self.metadata["render_modes"]).add(f"{self.env.render_mode}_list") @property def render_mode(self): diff --git a/tests/envs/test_make.py b/tests/envs/test_make.py index 043c30886..ed95fe0cf 100644 --- a/tests/envs/test_make.py +++ b/tests/envs/test_make.py @@ -250,11 +250,10 @@ def test_make_render_mode(register_make_testing_envs): env.close() assert len(valid_render_modes) > 0 + valid_mode = next(iter(valid_render_modes)) with warnings.catch_warnings(record=True) as caught_warnings: - env = gym.make( - "CartPole-v1", render_mode=valid_render_modes[0], disable_env_checker=True - ) - assert env.render_mode == valid_render_modes[0] + env = gym.make("CartPole-v1", render_mode=valid_mode, disable_env_checker=True) + assert env.render_mode == valid_mode env.close() for warning in caught_warnings: @@ -314,7 +313,7 @@ def test_make_render_mode(register_make_testing_envs): with pytest.warns( UserWarning, match=re.escape( - "\x1b[33mWARN: The environment is being initialised with render_mode='rgb_array' that is not in the possible render_modes ([]).\x1b[0m" + "\x1b[33mWARN: The environment is being initialised with render_mode='rgb_array' that is not in the possible render_modes ({}).\x1b[0m" ), ): gym.make("test/NoRenderModesMetadata-v0", render_mode="rgb_array") diff --git a/tests/envs/utils_envs.py b/tests/envs/utils_envs.py index fd573ff92..4881e690e 100644 --- a/tests/envs/utils_envs.py +++ b/tests/envs/utils_envs.py @@ -23,7 +23,7 @@ def __init__(self, arg1, arg2, arg3): class NoHuman(gym.Env): """Environment that does not have human-rendering.""" - metadata = {"render_modes": ["rgb_array_list"], "render_fps": 4} + metadata = {"render_modes": {"rgb_array_list"}, "render_fps": 4} def __init__(self, render_mode=None): assert render_mode in self.metadata["render_modes"] @@ -33,7 +33,7 @@ def __init__(self, render_mode=None): class NoHumanOldAPI(gym.Env): """Environment that does not have human-rendering.""" - metadata = {"render_modes": ["rgb_array_list"], "render_fps": 4} + metadata = {"render_modes": {"rgb_array_list"}, "render_fps": 4} def __init__(self): pass @@ -42,7 +42,7 @@ def __init__(self): class NoHumanNoRGB(gym.Env): """Environment that has neither human- nor rgb-rendering""" - metadata = {"render_modes": ["ascii"], "render_fps": 4} + metadata = {"render_modes": {"ascii"}, "render_fps": 4} def __init__(self, render_mode=None): assert render_mode in self.metadata["render_modes"] diff --git a/tests/test_core.py b/tests/test_core.py index da2aa0e4c..a37704302 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -136,7 +136,7 @@ def test_env_instantiation(): }, {"action_space": spaces.Discrete(2)}, {"reward_range": (-1.0, 1.0)}, - {"metadata": {"render_modes": ["human", "rgb_array_list"]}}, + {"metadata": {"render_modes": {"human", "rgb_array_list"}}}, { "observation_space": spaces.Box( low=0.0, high=1.0, shape=(64, 64, 3), dtype=np.float32 @@ -207,7 +207,7 @@ def test_gymnasium_env(): """Tests a gymnasium environment.""" env = ExampleEnv() - assert env.metadata == {"render_modes": []} + assert env.metadata == {"render_modes": {}} assert env.render_mode is None assert env.reward_range == (-float("inf"), float("inf")) assert env.spec is None @@ -247,7 +247,7 @@ def test_gymnasium_wrapper(): wrapper_env = ExampleWrapper(env) assert env.metadata == wrapper_env.metadata - wrapper_env.metadata = {"render_modes": ["rgb_array"]} + wrapper_env.metadata = {"render_modes": {"rgb_array"}} assert env.metadata != wrapper_env.metadata assert env.render_mode == wrapper_env.render_mode diff --git a/tests/testing_env.py b/tests/testing_env.py index a066f4306..7be440e94 100644 --- a/tests/testing_env.py +++ b/tests/testing_env.py @@ -48,7 +48,7 @@ def __init__( reset_func: callable = basic_reset_func, step_func: callable = new_step_func, render_func: callable = basic_render_func, - metadata: dict[str, Any] = {"render_modes": []}, + metadata: dict[str, Any] = {"render_modes": {}}, render_mode: str | None = None, spec: EnvSpec = EnvSpec( "TestingEnv-v0", "testing-env-no-entry-point", max_episode_steps=100 diff --git a/tests/utils/test_passive_env_checker.py b/tests/utils/test_passive_env_checker.py index 96710ca40..d036b71e0 100644 --- a/tests/utils/test_passive_env_checker.py +++ b/tests/utils/test_passive_env_checker.py @@ -402,7 +402,7 @@ def test_passive_env_step_checker( [ UserWarning, GenericTestEnv(metadata={"render_modes": "Testing mode"}), - "Expects the render_modes to be a sequence (i.e. list, tuple), actual type: ", + "Expects the render_modes to be a set, list or tuple, actual type: ", ], [ UserWarning, @@ -414,7 +414,7 @@ def test_passive_env_step_checker( [ UserWarning, GenericTestEnv( - metadata={"render_modes": ["Testing mode"], "render_fps": None}, + metadata={"render_modes": {"Testing mode"}, "render_fps": None}, render_mode="Testing mode", render_func=lambda self: 0, ), @@ -423,24 +423,24 @@ def test_passive_env_step_checker( [ UserWarning, GenericTestEnv( - metadata={"render_modes": ["Testing mode"], "render_fps": "fps"} + metadata={"render_modes": {"Testing mode"}, "render_fps": "fps"} ), "Expects the `env.metadata['render_fps']` to be an integer or a float, actual type: ", ], [ AssertionError, GenericTestEnv( - metadata={"render_modes": [], "render_fps": 30}, render_mode="Test" + metadata={"render_modes": set(), "render_fps": 30}, render_mode="Test" ), "With no render_modes, expects the Env.render_mode to be None, actual value: Test", ], [ AssertionError, GenericTestEnv( - metadata={"render_modes": ["Testing mode"], "render_fps": 30}, + metadata={"render_modes": {"Testing mode"}, "render_fps": 30}, render_mode="Non mode", ), - "The environment was initialized successfully however with an unsupported render mode. Render mode: Non mode, modes: ['Testing mode']", + "The environment was initialized successfully however with an unsupported render mode. Render mode: Non mode, modes: {'Testing mode'}", ], ], ) diff --git a/tests/utils/test_play.py b/tests/utils/test_play.py index 39a10c249..d07b4a664 100644 --- a/tests/utils/test_play.py +++ b/tests/utils/test_play.py @@ -20,7 +20,7 @@ PlayableEnv = partial( GenericTestEnv, - metadata={"render_modes": ["rgb_array"]}, + metadata={"render_modes": {"rgb_array"}}, render_func=lambda self: np.ones((10, 10, 3)), ) diff --git a/tests/wrappers/test_passive_env_checker.py b/tests/wrappers/test_passive_env_checker.py index f62c09d63..a78f68bae 100644 --- a/tests/wrappers/test_passive_env_checker.py +++ b/tests/wrappers/test_passive_env_checker.py @@ -95,7 +95,7 @@ def test_api_failures(): with pytest.warns( UserWarning, - match=r"Expects the render_modes to be a sequence \(i\.e\. list, tuple\), actual type: ", + match=r"Expects the render_modes to be a set, list or tuple, actual type: ", ): env.render() assert env.checked_render diff --git a/tests/wrappers/test_video_recorder.py b/tests/wrappers/test_video_recorder.py index 14e31f9ee..d277e1248 100644 --- a/tests/wrappers/test_video_recorder.py +++ b/tests/wrappers/test_video_recorder.py @@ -8,7 +8,7 @@ class BrokenRecordableEnv(gym.Env): - metadata = {"render_modes": ["rgb_array_list"]} + metadata = {"render_modes": {"rgb_array_list"}} def __init__(self, render_mode="rgb_array_list"): self.render_mode = render_mode @@ -18,7 +18,7 @@ def render(self): class UnrecordableEnv(gym.Env): - metadata = {"render_modes": [None]} + metadata = {"render_modes": {}} def __init__(self, render_mode=None): self.render_mode = render_mode