Skip to content

Commit

Permalink
Type hint fixes and added __all__ dunder (#321)
Browse files Browse the repository at this point in the history
  • Loading branch information
howardh authored Feb 12, 2023
1 parent 9bc0bf3 commit 79ae76e
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 7 deletions.
3 changes: 3 additions & 0 deletions gymnasium/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@
# that verify that our dependencies are actually present.
from gymnasium.utils.colorize import colorize
from gymnasium.utils.ezpickle import EzPickle


__all__ = ["colorize", "EzPickle"]
5 changes: 3 additions & 2 deletions gymnasium/vector/async_vector_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
import time
from copy import deepcopy
from enum import Enum
from typing import Callable, List, Optional, Sequence, Tuple, Union
from typing import Any, Callable, List, Optional, Sequence, Tuple, Union

import numpy as np
from numpy.typing import NDArray

import gymnasium as gym
from gymnasium import logger
Expand Down Expand Up @@ -287,7 +288,7 @@ def step_async(self, actions: np.ndarray):

def step_wait(
self, timeout: Optional[Union[int, float]] = None
) -> Tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray, List[dict]]:
) -> Tuple[Any, NDArray[Any], NDArray[Any], NDArray[Any], dict]:
"""Wait for the calls to :obj:`step` in each sub-environment to finish.
Args:
Expand Down
5 changes: 3 additions & 2 deletions gymnasium/vector/sync_vector_env.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
"""A synchronous vector environment."""
from copy import deepcopy
from typing import Any, Callable, Iterable, List, Optional, Sequence, Union
from typing import Any, Callable, Iterable, List, Optional, Sequence, Tuple, Union

import numpy as np
from numpy.typing import NDArray

from gymnasium import Env
from gymnasium.spaces import Space
Expand Down Expand Up @@ -132,7 +133,7 @@ def step_async(self, actions):
"""Sets :attr:`_actions` for use by the :meth:`step_wait` by converting the ``actions`` to an iterable version."""
self._actions = iterate(self.action_space, actions)

def step_wait(self):
def step_wait(self) -> Tuple[Any, NDArray[Any], NDArray[Any], NDArray[Any], dict]:
"""Steps through each of the environments returning the batched results.
Returns:
Expand Down
10 changes: 8 additions & 2 deletions gymnasium/vector/vector_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from typing import Any, List, Optional, Tuple, Union

import numpy as np
from numpy.typing import NDArray

import gymnasium as gym
from gymnasium.vector.utils.spaces import batch_space
Expand Down Expand Up @@ -146,7 +147,9 @@ def step_async(self, actions):
actions: The actions to take asynchronously
"""

def step_wait(self, **kwargs):
def step_wait(
self, **kwargs
) -> Tuple[Any, NDArray[Any], NDArray[Any], NDArray[Any], dict]:
"""Retrieves the results of a :meth:`step_async` call.
A call to this method must always be preceded by a call to :meth:`step_async`.
Expand All @@ -157,8 +160,11 @@ def step_wait(self, **kwargs):
Returns:
The results from the :meth:`step_async` call
"""
raise NotImplementedError()

def step(self, actions):
def step(
self, actions
) -> Tuple[Any, NDArray[Any], NDArray[Any], NDArray[Any], dict]:
"""Take an action for each parallel environment.
Args:
Expand Down
1 change: 1 addition & 0 deletions tests/vector/test_async_vector_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def test_reset_async_vector_env(shared_memory):

assert isinstance(env.observation_space, Box)
assert isinstance(observations, np.ndarray)
assert isinstance(infos, dict)
assert observations.dtype == env.observation_space.dtype
assert observations.shape == (8,) + env.single_observation_space.shape
assert observations.shape == env.observation_space.shape
Expand Down
6 changes: 5 additions & 1 deletion tests/vector/test_sync_vector_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def test_reset_sync_vector_env():

assert isinstance(env.observation_space, Box)
assert isinstance(observations, np.ndarray)
assert isinstance(infos, dict)
assert observations.dtype == env.observation_space.dtype
assert observations.shape == (8,) + env.single_observation_space.shape
assert observations.shape == env.observation_space.shape
Expand Down Expand Up @@ -130,15 +131,18 @@ def test_custom_space_sync_vector_env():

assert isinstance(env.single_action_space, CustomSpace)
assert isinstance(env.action_space, Tuple)
assert isinstance(infos, dict)

actions = ("action-2", "action-3", "action-5", "action-7")
step_observations, rewards, terminateds, truncateds, _ = env.step(actions)
step_observations, rewards, terminateds, truncateds, infos = env.step(actions)

env.close()

assert isinstance(env.single_observation_space, CustomSpace)
assert isinstance(env.observation_space, Tuple)

assert isinstance(infos, dict)

assert isinstance(reset_observations, tuple)
assert reset_observations == ("reset", "reset", "reset", "reset")

Expand Down

0 comments on commit 79ae76e

Please sign in to comment.