Skip to content

Commit

Permalink
Remove shimmy as a core dependency (openai#272)
Browse files Browse the repository at this point in the history
  • Loading branch information
pseudo-rnd-thoughts authored Jan 20, 2023
1 parent 4b5abb6 commit 1b28369
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 4 deletions.
12 changes: 12 additions & 0 deletions gymnasium/envs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,5 +348,17 @@
)


# --- For shimmy compatibility
def _raise_shimmy_error():
raise ImportError(
"To use the gym compatibility environments, run `pip install shimmy[gym]`"
)


# When installed, shimmy will re-register these environments with the correct entry_point
register(id="GymV22Environment-v0", entry_point=_raise_shimmy_error)
register(id="GymV26Environment-v0", entry_point=_raise_shimmy_error)


# Hook to load plugins from entry points
load_env_plugins()
4 changes: 2 additions & 2 deletions gymnasium/wrappers/compatibility.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ def __init__(self, old_env: LegacyEnv, render_mode: Optional[str] = None):
render_mode (str): the render mode to use when rendering the environment, passed automatically to env.render
"""
logger.warn(
"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`"
"The `gymnasium.make(..., apply_api_compatibility=...)` parameter is deprecated and will be removed in v0.28. "
"Instead use `gym.make('GymV22Environment-v0', env_name=...)` or `from shimmy import GymV22CompatibilityV0`"
)
self.metadata = getattr(old_env, "metadata", {"render_modes": []})
self.render_mode = render_mode
Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ dependencies = [
"importlib-metadata >=4.8.0; python_version < '3.10'",
"typing-extensions >=4.3.0",
"gymnasium-notices >=0.0.1",
"shimmy >=0.1.0,<1.0",
]
dynamic = ["version"]

Expand Down
54 changes: 54 additions & 0 deletions tests/envs/test_compatibility.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
import re
from typing import Any, Dict, Optional, Tuple

import numpy as np
import pytest

import gymnasium
from gymnasium.error import DependencyNotInstalled
from gymnasium.spaces import Discrete
from gymnasium.wrappers.compatibility import EnvCompatibility, LegacyEnv


try:
import gym
except ImportError:
gym = None


try:
import shimmy
except ImportError:
shimmy = None


class LegacyEnvExplicit(LegacyEnv, gymnasium.Env):
"""Legacy env that explicitly implements the old API."""

Expand Down Expand Up @@ -125,3 +140,42 @@ def test_make_compatibility_in_make():
assert img.shape == (1, 1, 3) # type: ignore
env.close()
del gymnasium.envs.registration.registry["LegacyTestEnv-v0"]


def test_shimmy_gym_compatibility():
assert gymnasium.spec("GymV22Environment-v0") is not None
assert gymnasium.spec("GymV26Environment-v0") is not None

if shimmy is None:
with pytest.raises(
ImportError,
match=re.escape(
"To use the gym compatibility environments, run `pip install shimmy[gym]`"
),
):
gymnasium.make("GymV22Environment-v0")
with pytest.raises(
ImportError,
match=re.escape(
"To use the gym compatibility environments, run `pip install shimmy[gym]`"
),
):
gymnasium.make("GymV26Environment-v0")
elif gym is None:
with pytest.raises(
DependencyNotInstalled,
match=re.escape(
"No module named 'gym' (Hint: You need to install gym with `pip install gym` to use gym environments"
),
):
gymnasium.make("GymV22Environment-v0", env_id="CartPole-v1")
with pytest.raises(
DependencyNotInstalled,
match=re.escape(
"No module named 'gym' (Hint: You need to install gym with `pip install gym` to use gym environments"
),
):
gymnasium.make("GymV26Environment-v0", env_id="CartPole-v1")
else:
gymnasium.make("GymV22Environment-v0", env_id="CartPole-v1")
gymnasium.make("GymV26Environment-v0", env_id="CartPole-v1")
6 changes: 6 additions & 0 deletions tests/envs/test_make.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@
from tests.wrappers.utils import has_wrapper


try:
import shimmy
except ImportError:
shimmy = None


@pytest.fixture(scope="function")
def register_make_testing_envs():
"""Registers testing envs for `gym.make`"""
Expand Down
5 changes: 4 additions & 1 deletion tests/envs/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ def try_make_env(env_spec: EnvSpec) -> Optional[gym.Env]:
Warning the environments have no wrappers, including time limit and order enforcing.
"""
# To avoid issues with registered environments during testing, we check that the spec entry points are from gymnasium.envs.
if "gymnasium.envs." in env_spec.entry_point:
if (
isinstance(env_spec.entry_point, str)
and "gymnasium.envs." in env_spec.entry_point
):
try:
return env_spec.make(disable_env_checker=True).unwrapped
except (
Expand Down

0 comments on commit 1b28369

Please sign in to comment.