Browse Source

fix rebase pre-commit

Rodrigo Perez-Vicente 2 years ago
parent
commit
8c879a1fa2

+ 11 - 11
.pre-commit-config.yaml

@@ -40,14 +40,14 @@ repos:
     hooks:
       - id: pyupgrade
         args: ["--py37-plus"]
- - repo: local
-   hooks:
-     - id: pyright
-       name: pyright
-       entry: pyright
-       language: node
-       pass_filenames: false
-       types: [python]
-       additional_dependencies: ["pyright"]
-       args:
-         - --project=pyproject.toml
+  - repo: local
+    hooks:
+      - id: pyright
+        name: pyright
+        entry: pyright
+        language: node
+        pass_filenames: false
+        types: [python]
+        additional_dependencies: ["pyright"]
+        args:
+          - --project=pyproject.toml

+ 3 - 1
gym_minigrid/envs/fetch.py

@@ -43,7 +43,9 @@ class FetchEnv(MiniGridEnv):
                 obj = Ball(objColor)
             else:
                 raise ValueError(
-                    "{} object type given. Object type can only be of values key and ball.".format(objType)
+                    "{} object type given. Object type can only be of values key and ball.".format(
+                        objType
+                    )
                 )
 
             self.place_obj(obj)

+ 0 - 4
gym_minigrid/envs/fourrooms.py

@@ -65,10 +65,6 @@ class FourRoomsEnv(MiniGridEnv):
             self.place_obj(Goal())
 
         self.mission = "reach the goal"
-<<<<<<< HEAD
-        self.mission = "Reach the goal"
-=======
->>>>>>> Add pyright to pre-commit
 
     def step(self, action):
         obs, reward, done, info = MiniGridEnv.step(self, action)

+ 3 - 1
gym_minigrid/envs/gotoobject.py

@@ -48,7 +48,9 @@ class GoToObjectEnv(MiniGridEnv):
                 obj = Box(objColor)
             else:
                 raise ValueError(
-                    "{} object type given. Object type can only be of values key, ball and box.".format(objType)
+                    "{} object type given. Object type can only be of values key, ball and box.".format(
+                        objType
+                    )
                 )
 
             pos = self.place_obj(obj)

+ 3 - 1
gym_minigrid/envs/playground.py

@@ -64,7 +64,9 @@ class PlaygroundEnv(MiniGridEnv):
                 obj = Box(objColor)
             else:
                 raise ValueError(
-                    "{} object type given. Object type can only be of values key, ball and box.".format(objType)
+                    "{} object type given. Object type can only be of values key, ball and box.".format(
+                        objType
+                    )
                 )
             self.place_obj(obj)
 

+ 3 - 1
gym_minigrid/envs/putnear.py

@@ -59,7 +59,9 @@ class PutNearEnv(MiniGridEnv):
                 obj = Box(objColor)
             else:
                 raise ValueError(
-                    "{} object type given. Object type can only be of values key, ball and box.".format(objType)
+                    "{} object type given. Object type can only be of values key, ball and box.".format(
+                        objType
+                    )
                 )
 
             pos = self.place_obj(obj, reject_fn=near_obj)

+ 11 - 10
gym_minigrid/minigrid.py

@@ -5,6 +5,7 @@ from enum import IntEnum
 
 import gym
 import numpy as np
+import numpy.typing as npt
 from gym import spaces
 
 # Size in pixels of a tile in the full-scale human view
@@ -254,7 +255,7 @@ class Door(WorldObj):
             state = 2
         # if door is closed and unlocked
         else:
-            state = 1 
+            state = 1
 
         return (OBJECT_TO_IDX[self.type], COLOR_TO_IDX[self.color], state)
 
@@ -586,8 +587,8 @@ class Grid:
 
         mask[agent_pos[0], agent_pos[1]] = True
 
-        for j in reversed(range(0, grid.height)):
-            for i in range(0, grid.width - 1):
+        for j in reversed(range(0, self.height)):
+            for i in range(0, self.width - 1):
                 if not mask[i, j]:
                     continue
 
@@ -653,13 +654,13 @@ class MiniGridEnv(gym.Env):
 
     def __init__(
         self,
-        grid_size: int=None,
-        width: int=None,
-        height: int=None,
-        max_steps: int=100,
-        see_through_walls: bool=False,
-        agent_view_size: int=7,
-        render_mode: str=None,
+        grid_size: int = None,
+        width: int = None,
+        height: int = None,
+        max_steps: int = 100,
+        see_through_walls: bool = False,
+        agent_view_size: int = 7,
+        render_mode: str = None,
         **kwargs
     ):
         # Can't set both grid_size and width/height

+ 3 - 4
gym_minigrid/roomgrid.py

@@ -1,7 +1,6 @@
 from gym_minigrid.minigrid import COLOR_NAMES, Ball, Box, Door, Grid, Key, MiniGridEnv
 
 
-
 def reject_next_to(env, pos):
     """
     Function to filter out object positions that are right next to
@@ -70,7 +69,7 @@ class RoomGrid(MiniGridEnv):
         num_cols=3,
         max_steps=100,
         agent_view_size=7,
-        **kwargs
+        **kwargs,
     ):
         assert room_size > 0
         assert room_size >= 3
@@ -92,7 +91,7 @@ class RoomGrid(MiniGridEnv):
             max_steps=max_steps,
             see_through_walls=False,
             agent_view_size=agent_view_size,
-            **kwargs
+            **kwargs,
         )
 
     def room_from_pos(self, x, y):
@@ -205,7 +204,7 @@ class RoomGrid(MiniGridEnv):
         elif kind == "box":
             obj = Box(color)
         else:
-            raise "{} object kind is not available in this environment.".format(kind)
+            raise f"{kind} object kind is not available in this environment."
 
         return self.place_in_room(i, j, obj)
 

+ 0 - 1
gym_minigrid/window.py

@@ -7,7 +7,6 @@ except ImportError:
     )
 
 
-
 class Window:
     """
     Window to draw a gridworld instance using Matplotlib

+ 17 - 15
gym_minigrid/wrappers.py

@@ -8,10 +8,8 @@ from gym import spaces
 
 from gym_minigrid.minigrid import COLOR_TO_IDX, OBJECT_TO_IDX, STATE_TO_IDX, Goal
 
-from gym_minigrid.minigrid import COLOR_TO_IDX, OBJECT_TO_IDX, STATE_TO_IDX, Goal
-
 
-class ReseedWrapper(Wrapper):
+class ReseedWrapper(gym.Wrapper):
     """
     Wrapper to always regenerate an environment with the same set of seeds.
     This can be used to force an environment to always keep the same
@@ -33,7 +31,7 @@ class ReseedWrapper(Wrapper):
         return obs, reward, done, info
 
 
-class ActionBonus(Wrapper):
+class ActionBonus(gym.Wrapper):
     """
     Wrapper which adds an exploration bonus.
     This is a reward to encourage exploration of less
@@ -68,7 +66,7 @@ class ActionBonus(Wrapper):
         return self.env.reset(**kwargs)
 
 
-class StateBonus(Wrapper):
+class StateBonus(gym.Wrapper):
     """
     Adds an exploration bonus based on which positions
     are visited on the grid.
@@ -104,7 +102,7 @@ class StateBonus(Wrapper):
         return self.env.reset(**kwargs)
 
 
-class ImgObsWrapper(ObservationWrapper):
+class ImgObsWrapper(gym.core.ObservationWrapper):
     """
     Use the image as the only observation output, no language/mission.
     """
@@ -117,7 +115,7 @@ class ImgObsWrapper(ObservationWrapper):
         return obs["image"]
 
 
-class OneHotPartialObsWrapper(ObservationWrapper):
+class OneHotPartialObsWrapper(gym.core.ObservationWrapper):
     """
     Wrapper to get a one-hot encoding of a partially observable
     agent view as observation.
@@ -157,7 +155,7 @@ class OneHotPartialObsWrapper(ObservationWrapper):
         return {**obs, "image": out}
 
 
-class RGBImgObsWrapper(ObservationWrapper):
+class RGBImgObsWrapper(gym.core.ObservationWrapper):
     """
     Wrapper to use fully observable RGB image as observation,
     This can be used to have the agent to solve the gridworld in pixel space.
@@ -189,7 +187,7 @@ class RGBImgObsWrapper(ObservationWrapper):
         return {**obs, "image": rgb_img}
 
 
-class RGBImgPartialObsWrapper(ObservationWrapper):
+class RGBImgPartialObsWrapper(gym.core.ObservationWrapper):
     """
     Wrapper to use partially observable RGB image as observation.
     This can be used to have the agent to solve the gridworld in pixel space.
@@ -220,7 +218,7 @@ class RGBImgPartialObsWrapper(ObservationWrapper):
         return {**obs, "image": rgb_img_partial}
 
 
-class FullyObsWrapper(ObservationWrapper):
+class FullyObsWrapper(gym.core.ObservationWrapper):
     """
     Fully observable gridworld using a compact grid encoding
     """
@@ -249,7 +247,7 @@ class FullyObsWrapper(ObservationWrapper):
         return {**obs, "image": full_grid}
 
 
-class DictObservationSpaceWrapper(ObservationWrapper):
+class DictObservationSpaceWrapper(gym.core.ObservationWrapper):
     """
     Transforms the observation space (that has a textual component) to a fully numerical observation space,
     where the textual instructions are replaced by arrays representing the indices of each word in a fixed vocabulary.
@@ -367,7 +365,7 @@ class DictObservationSpaceWrapper(ObservationWrapper):
         return obs
 
 
-class FlatObsWrapper(ObservationWrapper):
+class FlatObsWrapper(gym.core.ObservationWrapper):
     """
     Encode mission strings using a one-hot scheme,
     and combine these with observed images into one flat array
@@ -411,6 +409,10 @@ class FlatObsWrapper(ObservationWrapper):
                     chNo = ord(ch) - ord("a")
                 elif ch == " ":
                     chNo = ord("z") - ord("a") + 1
+                else:
+                    raise ValueError(
+                        f"Character {ch} is not available in mission string."
+                    )
                 assert chNo < self.numCharCodes, "%s : %d" % (ch, chNo)
                 strArray[idx, chNo] = 1
 
@@ -422,7 +424,7 @@ class FlatObsWrapper(ObservationWrapper):
         return obs
 
 
-class ViewSizeWrapper(Wrapper):
+class ViewSizeWrapper(gym.Wrapper):
     """
     Wrapper to customize the agent field of view size.
     This cannot be used with fully observable wrappers.
@@ -457,7 +459,7 @@ class ViewSizeWrapper(Wrapper):
         return {**obs, "image": image}
 
 
-class DirectionObsWrapper(ObservationWrapper):
+class DirectionObsWrapper(gym.core.ObservationWrapper):
     """
     Provides the slope/angular direction to the goal with the observations as modeled by (y2 - y2 )/( x2 - x1)
     type = {slope , angle}
@@ -491,7 +493,7 @@ class DirectionObsWrapper(ObservationWrapper):
         return obs
 
 
-class SymbolicObsWrapper(ObservationWrapper):
+class SymbolicObsWrapper(gym.core.ObservationWrapper):
     """
     Fully observable grid with a symbolic state representation.
     The symbol is a triple of (X, Y, IDX), where X and Y are

+ 0 - 4
manual_control.py

@@ -28,11 +28,7 @@ def reset():
 
 def step(action):
     obs, reward, done, info = env.step(action)
-<<<<<<< HEAD
     print(f"step={env.step_count}, reward={reward:.2f}")
-=======
-    print("step={}, reward={:.2f}".format(env.step_count, reward))
->>>>>>> Add pyright to pre-commit
 
     if done:
         print("done!")

+ 0 - 4
run_tests.py

@@ -139,11 +139,7 @@ for env_idx, env_name in enumerate(env_list):
         obs_space, wrapper_name = env.observation_space, wrapper.__name__
         assert isinstance(
             obs_space, spaces.Dict
-<<<<<<< HEAD
         ), f"Observation space for {wrapper_name} is not a Dict: {obs_space}."
-=======
-        ), "Observation space for {} is not a Dict: {}.".format(wrapper_name, obs_space)
->>>>>>> Add pyright to pre-commit
         # This should not fail either
         ImgObsWrapper(env)
         env.reset()

+ 0 - 0
tests/__init__.py


+ 0 - 0
tests/envs/__init__.py


+ 0 - 103
tests/envs/test_envs.py

@@ -1,103 +0,0 @@
-import gym
-import pytest
-from gym.envs.registration import EnvSpec
-from gym.utils.env_checker import check_env
-
-from tests.envs.utils import all_testing_env_specs, assert_equals
-
-# This runs a smoketest on each official registered env. We may want
-# to try also running environments which are not officially registered envs.
-IGNORE_WARNINGS = [
-    "Agent's minimum observation space value is -infinity. This is probably too low.",
-    "Agent's maximum observation space value is infinity. This is probably too high.",
-    "We recommend you to use a symmetric and normalized Box action space (range=[-1, 1]) https://stable-baselines3.readthedocs.io/en/master/guide/rl_tips.html",
-]
-IGNORE_WARNINGS = [f"\x1b[33mWARN: {message}\x1b[0m" for message in IGNORE_WARNINGS]
-
-
-@pytest.mark.parametrize(
-    "spec", all_testing_env_specs, ids=[spec.id for spec in all_testing_env_specs]
-)
-def test_env(spec):
-    # Capture warnings
-    env = spec.make(disable_env_checker=True).unwrapped
-
-    # Test if env adheres to Gym API
-    with pytest.warns(None) as warnings:
-        check_env(env)
-
-    for warning in warnings.list:
-        if warning.message.args[0] not in IGNORE_WARNINGS:
-            raise gym.error.Error(f"Unexpected warning: {warning.message}")
-
-
-# Note that this precludes running this test in multiple threads.
-# However, we probably already can't do multithreading due to some environments.
-SEED = 0
-NUM_STEPS = 50
-
-
-@pytest.mark.parametrize(
-    "env_spec", all_testing_env_specs, ids=[env.id for env in all_testing_env_specs]
-)
-def test_env_determinism_rollout(env_spec: EnvSpec):
-    """Run a rollout with two environments and assert equality.
-
-    This test run a rollout of NUM_STEPS steps with two environments
-    initialized with the same seed and assert that:
-
-    - observation after first reset are the same
-    - same actions are sampled by the two envs
-    - observations are contained in the observation space
-    - obs, rew, done and info are equals between the two envs
-    """
-    # Don't check rollout equality if it's a nondeterministic environment.
-    if env_spec.nondeterministic is True:
-        return
-
-    env_1 = env_spec.make(disable_env_checker=True)
-    env_2 = env_spec.make(disable_env_checker=True)
-
-    initial_obs_1 = env_1.reset(seed=SEED)
-    initial_obs_2 = env_2.reset(seed=SEED)
-    assert_equals(initial_obs_1, initial_obs_2)
-
-    env_1.action_space.seed(SEED)
-
-    for time_step in range(NUM_STEPS):
-        # We don't evaluate the determinism of actions
-        action = env_1.action_space.sample()
-
-        obs_1, rew_1, done_1, info_1 = env_1.step(action)
-        obs_2, rew_2, done_2, info_2 = env_2.step(action)
-
-        assert_equals(obs_1, obs_2, f"[{time_step}] ")
-        assert env_1.observation_space.contains(
-            obs_1
-        )  # obs_2 verified by previous assertion
-
-        assert rew_1 == rew_2, f"[{time_step}] reward 1={rew_1}, reward 2={rew_2}"
-        assert done_1 == done_2, f"[{time_step}] done 1={done_1}, done 2={done_2}"
-        assert_equals(info_1, info_2, f"[{time_step}] ")
-
-        if done_1:  # done_2 verified by previous assertion
-            env_1.reset(seed=SEED)
-            env_2.reset(seed=SEED)
-
-    env_1.close()
-    env_2.close()
-
-
-@pytest.mark.parametrize(
-    "spec", all_testing_env_specs, ids=[spec.id for spec in all_testing_env_specs]
-)
-def test_render_modes(spec):
-    env = spec.make()
-
-    for mode in env.metadata.get("render_modes", []):
-        if mode != "human":
-            new_env = spec.make(render_mode=mode)
-
-            new_env.reset()
-            new_env.step(new_env.action_space.sample())
-            new_env.render()

+ 0 - 55
tests/envs/utils.py

@@ -1,55 +0,0 @@
-"""Finds all the specs that we can test with"""
-from typing import Optional
-
-import gym
-import numpy as np
-from gym import logger
-from gym.envs.registration import EnvSpec
-
-
-def try_make_env(env_spec: EnvSpec) -> Optional[gym.Env]:
-    """Tries to make the environment showing if it is possible. Warning the environments have no wrappers, including time limit and order enforcing."""
-    try:
-        return env_spec.make(disable_env_checker=True).unwrapped
-    except ImportError as e:
-        logger.warn(f"Not testing {env_spec.id} due to error: {e}")
-        return None
-
-
-# Tries to make all gym_minigrid environment to test with
-all_testing_initialised_envs = list(
-    filter(
-        None,
-        [
-            try_make_env(env_spec)
-            for env_spec in gym.envs.registry.values()
-            if env_spec.entry_point.startswith("gym_minigrid.envs")
-        ],
-    )
-)
-all_testing_env_specs = [env.spec for env in all_testing_initialised_envs]
-
-
-def assert_equals(a, b, prefix=None):
-    """Assert equality of data structures `a` and `b`.
-
-    Args:
-        a: first data structure
-        b: second data structure
-        prefix: prefix for failed assertion message for types and dicts
-    """
-    assert type(a) == type(b), f"{prefix}Differing types: {a} and {b}"
-    if isinstance(a, dict):
-        assert list(a.keys()) == list(b.keys()), f"{prefix}Key sets differ: {a} and {b}"
-
-        for k in a.keys():
-            v_a = a[k]
-            v_b = b[k]
-            assert_equals(v_a, v_b)
-    elif isinstance(a, np.ndarray):
-        np.testing.assert_array_equal(a, b)
-    elif isinstance(a, tuple):
-        for elem_from_a, elem_from_b in zip(a, b):
-            assert_equals(elem_from_a, elem_from_b)
-    else:
-        assert a == b