Skip to content

Commit 81b87ef

Browse files
Fix __getattr__ warning (#676)
1 parent 7ddce8c commit 81b87ef

File tree

5 files changed

+46
-5
lines changed

5 files changed

+46
-5
lines changed

Diff for: gymnasium/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
"error",
5252
"logger",
5353
]
54-
__version__ = "0.29.0"
54+
__version__ = "0.29.1"
5555

5656

5757
# Initializing pygame initializes audio connections through SDL. SDL uses alsa by default on all Linux systems

Diff for: gymnasium/core.py

+16-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,20 @@
1313
if TYPE_CHECKING:
1414
from gymnasium.envs.registration import EnvSpec, WrapperSpec
1515

16+
17+
__all__ = [
18+
"Env",
19+
"Wrapper",
20+
"ObservationWrapper",
21+
"RewardWrapper",
22+
"ActionWrapper",
23+
"ObsType",
24+
"ActType",
25+
"RenderFrame",
26+
"WrapperObsType",
27+
"WrapperActType",
28+
]
29+
1630
ObsType = TypeVar("ObsType")
1731
ActType = TypeVar("ActType")
1832
RenderFrame = TypeVar("RenderFrame")
@@ -296,7 +310,7 @@ def __getattr__(self, name: str) -> Any:
296310
raise AttributeError(f"accessing private attribute '{name}' is prohibited")
297311
logger.warn(
298312
f"env.{name} to get variables from other wrappers is deprecated and will be removed in v1.0, "
299-
f"to get this variable you can do `env.unwrapped.{name}` for environment variables or `env.get_attr('{name}')` that will search the reminding wrappers."
313+
f"to get this variable you can do `env.unwrapped.{name}` for environment variables or `env.get_wrapper_attr('{name}')` that will search the reminding wrappers."
300314
)
301315
return getattr(self.env, name)
302316

@@ -309,7 +323,7 @@ def get_wrapper_attr(self, name: str) -> Any:
309323
Returns:
310324
The variable with name in wrapper or lower environments
311325
"""
312-
if hasattr(self, name):
326+
if name in self.__dir__(): # todo change in v1.0.0 to `hasattr`
313327
return getattr(self, name)
314328
else:
315329
try:

Diff for: gymnasium/vector/vector_env.py

+5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from numpy.typing import NDArray
66

77
import gymnasium as gym
8+
from gymnasium import logger
89
from gymnasium.vector.utils.spaces import batch_space
910

1011

@@ -385,6 +386,10 @@ def set_attr(self, name, values):
385386
def __getattr__(self, name):
386387
if name.startswith("_"):
387388
raise AttributeError(f"attempted to get missing private attribute '{name}'")
389+
logger.warn(
390+
f"env.{name} to get variables from other wrappers is deprecated and will be removed in v1.0, "
391+
f"to get this variable you can do `env.unwrapped.{name}` for environment variables."
392+
)
388393
return getattr(self.env, name)
389394

390395
@property

Diff for: pyproject.toml

+3-2
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ accept-rom-license = ["autorom[accept-rom-license] ~=0.4.2"]
3939
box2d = ["box2d-py ==2.3.5", "pygame >=2.1.3", "swig ==4.*"]
4040
classic-control = ["pygame >=2.1.3"]
4141
classic_control = ["pygame >=2.1.3"] # kept for backward compatibility
42-
mujoco-py = ["mujoco-py >=2.1,<2.2"]
43-
mujoco_py = ["mujoco-py >=2.1,<2.2"] # kept for backward compatibility
42+
mujoco-py = ["mujoco-py >=2.1,<2.2", "cython<3"]
43+
mujoco_py = ["mujoco-py >=2.1,<2.2", "cython<3"] # kept for backward compatibility
4444
mujoco = ["mujoco >=2.3.3", "imageio >=2.14.1"]
4545
toy-text = ["pygame >=2.1.3"]
4646
toy_text = ["pygame >=2.1.3"] # kept for backward compatibility
@@ -65,6 +65,7 @@ all = [
6565
"pygame >=2.1.3",
6666
# mujoco-py
6767
"mujoco-py >=2.1,<2.2",
68+
"cython<3",
6869
# mujoco
6970
"mujoco >=2.3.3",
7071
"imageio >=2.14.1",

Diff for: tests/test_core.py

+21
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
"""Checks that the core Gymnasium API is implemented as expected."""
22
import re
3+
import warnings
34
from typing import Any, Dict, Optional, SupportsFloat, Tuple
45

56
import numpy as np
67
import pytest
78

9+
import gymnasium as gym
810
from gymnasium import Env, ObservationWrapper, RewardWrapper, Wrapper, spaces
911
from gymnasium.core import (
1012
ActionWrapper,
@@ -326,3 +328,22 @@ def test_wrapper_types():
326328
action_env = ExampleActionWrapper(env)
327329
obs, _, _, _, _ = action_env.step(0)
328330
assert obs == np.array([1])
331+
332+
333+
def test_get_wrapper_attr():
334+
env = gym.make("CartPole-v1")
335+
336+
with warnings.catch_warnings(record=True) as caught_warnings:
337+
gravity = env.get_wrapper_attr("gravity")
338+
339+
assert gravity == env.unwrapped.gravity
340+
assert len(caught_warnings) == 0
341+
342+
with pytest.warns(
343+
UserWarning,
344+
match=re.escape(
345+
"env.gravity to get variables from other wrappers is deprecated and will be removed in v1.0, to get this variable you can do `env.unwrapped.gravity` for environment variables or `env.get_wrapper_attr('gravity')` that will search the reminding wrappers."
346+
),
347+
):
348+
gravity = env.gravity
349+
assert gravity == env.unwrapped.gravity

0 commit comments

Comments
 (0)