Skip to content

Commit 1b28369

Browse files
Remove shimmy as a core dependency (#272)
1 parent 4b5abb6 commit 1b28369

File tree

6 files changed

+78
-4
lines changed

6 files changed

+78
-4
lines changed

Diff for: gymnasium/envs/__init__.py

+12
Original file line numberDiff line numberDiff line change
@@ -348,5 +348,17 @@
348348
)
349349

350350

351+
# --- For shimmy compatibility
352+
def _raise_shimmy_error():
353+
raise ImportError(
354+
"To use the gym compatibility environments, run `pip install shimmy[gym]`"
355+
)
356+
357+
358+
# When installed, shimmy will re-register these environments with the correct entry_point
359+
register(id="GymV22Environment-v0", entry_point=_raise_shimmy_error)
360+
register(id="GymV26Environment-v0", entry_point=_raise_shimmy_error)
361+
362+
351363
# Hook to load plugins from entry points
352364
load_env_plugins()

Diff for: gymnasium/wrappers/compatibility.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ def __init__(self, old_env: LegacyEnv, render_mode: Optional[str] = None):
6565
render_mode (str): the render mode to use when rendering the environment, passed automatically to env.render
6666
"""
6767
logger.warn(
68-
"The `gymnasium.make(..., apply_api_compatibility=...)` parameter is deprecated and will be removed in v28. "
69-
"Instead use `gym.make('GymV22Environment-v0', env_name=...)` or `from shimmy import GymV26CompatibilityV0`"
68+
"The `gymnasium.make(..., apply_api_compatibility=...)` parameter is deprecated and will be removed in v0.28. "
69+
"Instead use `gym.make('GymV22Environment-v0', env_name=...)` or `from shimmy import GymV22CompatibilityV0`"
7070
)
7171
self.metadata = getattr(old_env, "metadata", {"render_modes": []})
7272
self.render_mode = render_mode

Diff for: pyproject.toml

-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ dependencies = [
3131
"importlib-metadata >=4.8.0; python_version < '3.10'",
3232
"typing-extensions >=4.3.0",
3333
"gymnasium-notices >=0.0.1",
34-
"shimmy >=0.1.0,<1.0",
3534
]
3635
dynamic = ["version"]
3736

Diff for: tests/envs/test_compatibility.py

+54
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,27 @@
1+
import re
12
from typing import Any, Dict, Optional, Tuple
23

34
import numpy as np
5+
import pytest
46

57
import gymnasium
8+
from gymnasium.error import DependencyNotInstalled
69
from gymnasium.spaces import Discrete
710
from gymnasium.wrappers.compatibility import EnvCompatibility, LegacyEnv
811

912

13+
try:
14+
import gym
15+
except ImportError:
16+
gym = None
17+
18+
19+
try:
20+
import shimmy
21+
except ImportError:
22+
shimmy = None
23+
24+
1025
class LegacyEnvExplicit(LegacyEnv, gymnasium.Env):
1126
"""Legacy env that explicitly implements the old API."""
1227

@@ -125,3 +140,42 @@ def test_make_compatibility_in_make():
125140
assert img.shape == (1, 1, 3) # type: ignore
126141
env.close()
127142
del gymnasium.envs.registration.registry["LegacyTestEnv-v0"]
143+
144+
145+
def test_shimmy_gym_compatibility():
146+
assert gymnasium.spec("GymV22Environment-v0") is not None
147+
assert gymnasium.spec("GymV26Environment-v0") is not None
148+
149+
if shimmy is None:
150+
with pytest.raises(
151+
ImportError,
152+
match=re.escape(
153+
"To use the gym compatibility environments, run `pip install shimmy[gym]`"
154+
),
155+
):
156+
gymnasium.make("GymV22Environment-v0")
157+
with pytest.raises(
158+
ImportError,
159+
match=re.escape(
160+
"To use the gym compatibility environments, run `pip install shimmy[gym]`"
161+
),
162+
):
163+
gymnasium.make("GymV26Environment-v0")
164+
elif gym is None:
165+
with pytest.raises(
166+
DependencyNotInstalled,
167+
match=re.escape(
168+
"No module named 'gym' (Hint: You need to install gym with `pip install gym` to use gym environments"
169+
),
170+
):
171+
gymnasium.make("GymV22Environment-v0", env_id="CartPole-v1")
172+
with pytest.raises(
173+
DependencyNotInstalled,
174+
match=re.escape(
175+
"No module named 'gym' (Hint: You need to install gym with `pip install gym` to use gym environments"
176+
),
177+
):
178+
gymnasium.make("GymV26Environment-v0", env_id="CartPole-v1")
179+
else:
180+
gymnasium.make("GymV22Environment-v0", env_id="CartPole-v1")
181+
gymnasium.make("GymV26Environment-v0", env_id="CartPole-v1")

Diff for: tests/envs/test_make.py

+6
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@
2323
from tests.wrappers.utils import has_wrapper
2424

2525

26+
try:
27+
import shimmy
28+
except ImportError:
29+
shimmy = None
30+
31+
2632
@pytest.fixture(scope="function")
2733
def register_make_testing_envs():
2834
"""Registers testing envs for `gym.make`"""

Diff for: tests/envs/utils.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ def try_make_env(env_spec: EnvSpec) -> Optional[gym.Env]:
1414
Warning the environments have no wrappers, including time limit and order enforcing.
1515
"""
1616
# To avoid issues with registered environments during testing, we check that the spec entry points are from gymnasium.envs.
17-
if "gymnasium.envs." in env_spec.entry_point:
17+
if (
18+
isinstance(env_spec.entry_point, str)
19+
and "gymnasium.envs." in env_spec.entry_point
20+
):
1821
try:
1922
return env_spec.make(disable_env_checker=True).unwrapped
2023
except (

0 commit comments

Comments
 (0)