diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md new file mode 100644 index 000000000..996c690ec --- /dev/null +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -0,0 +1,45 @@ +# Description + +Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. List any dependencies that are required for this change. + +Fixes # (issue) + +## Type of change + +Please delete options that are not relevant. + +- [ ] Bug fix (non-breaking change which fixes an issue) +- [ ] New feature (non-breaking change which adds functionality) +- [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected) +- [ ] This change requires a documentation update + +### Screenshots +Please attach before and after screenshots of the change if applicable. + + + +# Checklist: + +- [ ] I have run the [`pre-commit` checks](https://pre-commit.com/) with `pre-commit run --all-files` (see `CONTRIBUTING.md` instructions to set it up) +- [ ] I have commented my code, particularly in hard-to-understand areas +- [ ] I have made corresponding changes to the documentation +- [ ] My changes generate no new warnings +- [ ] I have added tests that prove my fix is effective or that my feature works +- [ ] New and existing unit tests pass locally with my changes + + diff --git a/.github/workflows/lint_python.yml b/.github/workflows/lint_python.yml index 60eee2c07..65d69454c 100644 --- a/.github/workflows/lint_python.yml +++ b/.github/workflows/lint_python.yml @@ -1,25 +1,14 @@ +--- name: lint_python on: [pull_request, push] jobs: - lint_python: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - uses: actions/setup-python@v2 - - run: pip install isort pytest pyupgrade safety - - run: isort --check-only --profile black . || true - - run: pip install -e .[nomujoco] - - run: pytest . || true - - run: pytest --doctest-modules . || true - - run: shopt -s globstar && pyupgrade --py36-plus **/*.py || true - pyright: name: Check types with pyright runs-on: ubuntu-latest strategy: matrix: - python-platform: [ "Linux" ] - python-version: [ "3.7"] + python-platform: ["Linux"] + python-version: ["3.7"] fail-fast: false env: PYRIGHT_VERSION: 1.1.204 diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 4654f0348..9b665399d 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,15 +1,7 @@ +--- repos: - - repo: https://github.com/PyCQA/bandit/ - rev: 1.7.0 - hooks: - - id: bandit - args: - - --recursive - - --skip - - B101,B108,B301,B403,B404,B603 - - . - repo: https://github.com/python/black - rev: 21.7b0 + rev: 22.1.0 hooks: - id: black - repo: https://github.com/codespell-project/codespell @@ -28,3 +20,14 @@ repos: - --max-line-length=456 - --show-source - --statistics + - repo: https://github.com/PyCQA/isort + rev: 5.10.1 + hooks: + - id: isort + args: ["--profile", "black"] + - repo: https://github.com/asottile/pyupgrade + rev: v2.31.0 + hooks: + - id: pyupgrade + # TODO: remove `--keep-runtime-typing` option + args: ["--py37-plus", "--keep-runtime-typing"] diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 181698a91..38db52b0a 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -22,7 +22,7 @@ This section contains technical instructions & hints for the contributors. ## Type checking The project uses `pyright` to check types. -To type check locally, install pyright per official [instructions](https://github.com/microsoft/pyright#command-line). +To type check locally, install `pyright` per official [instructions](https://github.com/microsoft/pyright#command-line). It's configuration lives under a section of `pyproject.toml`. It includes list of files currently supporting type checks. Use `pyright` CLI command to launch type checks. ### Adding typing to more modules and packages @@ -32,3 +32,9 @@ add the path to the file(s) in the include section (pyproject.toml -> [tool.pyright] -> include). Then you can run `pyright` to see list of type problems in the newly added file, and fix them. +## Git hooks +The CI will run several checks on the new code pushed to the Gym repository. These checks can also be run locally without waiting for the CI by following the steps below: +1. [install `pre-commit`](https://pre-commit.com/#install), +2. install the Git hooks by running `pre-commit install`. + +Once those two steps are done, the Git hooks will be run automatically at every new commit. The Git hooks can also be run manually with `pre-commit run --all-files`, and if needed they can be skipped (not recommended) with `git commit --no-verify`. **Note:** you may have to run `pre-commit run --all-files` manually a couple of times to make it pass when you commit, as each formatting tool will first format the code and fail the first time but should pass the second time. diff --git a/README.md b/README.md index bc07bf0a7..06555fe02 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,5 @@ +[![pre-commit](https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit&logoColor=white)](https://pre-commit.com/) [![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black) + ## Gym Gym is an open source Python library for developing and comparing reinforcement learning algorithms by providing a standard API to communicate between learning algorithms and environments, as well as a standard set of environments compliant with that API. Since its release, Gym's API has become the field standard for doing this. diff --git a/gym/__init__.py b/gym/__init__.py index 42f0bd3fc..53d771865 100644 --- a/gym/__init__.py +++ b/gym/__init__.py @@ -1,3 +1,5 @@ +# isort: skip_file + from gym import error from gym.version import VERSION as __version__ diff --git a/gym/core.py b/gym/core.py index e8d2ff0f2..12fe0b51f 100644 --- a/gym/core.py +++ b/gym/core.py @@ -1,12 +1,12 @@ from __future__ import annotations from abc import abstractmethod -from typing import TypeVar, Generic, Tuple, Union, Optional, SupportsFloat +from typing import Generic, Optional, SupportsFloat, Tuple, TypeVar, Union import gym from gym import spaces -from gym.utils import seeding from gym.logger import deprecation +from gym.utils import seeding from gym.utils.seeding import RandomNumberGenerator ObsType = TypeVar("ObsType") diff --git a/gym/envs/__init__.py b/gym/envs/__init__.py index fb48aa61e..78fe08c68 100644 --- a/gym/envs/__init__.py +++ b/gym/envs/__init__.py @@ -1,10 +1,5 @@ -from gym.envs.registration import ( - registry, - register, - make, - spec, - load_env_plugins as _load_env_plugins, -) +from gym.envs.registration import load_env_plugins as _load_env_plugins +from gym.envs.registration import make, register, registry, spec # Hook to load plugins from entry points _load_env_plugins() diff --git a/gym/envs/box2d/__init__.py b/gym/envs/box2d/__init__.py index bfe3df2ae..6d98240a7 100644 --- a/gym/envs/box2d/__init__.py +++ b/gym/envs/box2d/__init__.py @@ -1,7 +1,8 @@ try: import Box2D - from gym.envs.box2d.lunar_lander import LunarLander, LunarLanderContinuous + from gym.envs.box2d.bipedal_walker import BipedalWalker, BipedalWalkerHardcore from gym.envs.box2d.car_racing import CarRacing + from gym.envs.box2d.lunar_lander import LunarLander, LunarLanderContinuous except ImportError: Box2D = None diff --git a/gym/envs/box2d/bipedal_walker.py b/gym/envs/box2d/bipedal_walker.py index 253614980..c2afa2224 100644 --- a/gym/envs/box2d/bipedal_walker.py +++ b/gym/envs/box2d/bipedal_walker.py @@ -1,27 +1,25 @@ __credits__ = ["Andrea PIERRÉ"] -import sys import math +import sys from typing import Optional +import Box2D import numpy as np import pygame -from pygame import gfxdraw - -import Box2D from Box2D.b2 import ( - edgeShape, circleShape, + contactListener, + edgeShape, fixtureDef, polygonShape, revoluteJointDef, - contactListener, ) +from pygame import gfxdraw import gym from gym import error, spaces -from gym.utils import colorize, seeding, EzPickle - +from gym.utils import EzPickle, colorize, seeding FPS = 50 SCALE = 30.0 # affects how fast-paced the game is, forces should be adjusted as well @@ -582,7 +580,7 @@ class BipedalWalker(gym.Env, EzPickle): continue scaled_poly = [] for coord in poly: - scaled_poly.append(([coord[0] * SCALE, coord[1] * SCALE])) + scaled_poly.append([coord[0] * SCALE, coord[1] * SCALE]) pygame.draw.polygon(self.surf, color=color, points=scaled_poly) gfxdraw.aapolygon(self.surf, scaled_poly, color) diff --git a/gym/envs/box2d/car_dynamics.py b/gym/envs/box2d/car_dynamics.py index 3ac38f887..b1290356c 100644 --- a/gym/envs/box2d/car_dynamics.py +++ b/gym/envs/box2d/car_dynamics.py @@ -7,17 +7,18 @@ This simulation is a bit more detailed, with wheels rotation. Created by Oleg Klimov """ -import numpy as np import math -import pygame.draw + import Box2D +import numpy as np +import pygame.draw from Box2D.b2 import ( - edgeShape, circleShape, + contactListener, + edgeShape, fixtureDef, polygonShape, revoluteJointDef, - contactListener, shape, ) diff --git a/gym/envs/box2d/car_racing.py b/gym/envs/box2d/car_racing.py index a36af7c97..8857695a9 100644 --- a/gym/envs/box2d/car_racing.py +++ b/gym/envs/box2d/car_racing.py @@ -1,22 +1,19 @@ __credits__ = ["Andrea PIERRÉ"] -import sys import math +import sys from typing import Optional +import Box2D import numpy as np import pygame +from Box2D.b2 import contactListener, fixtureDef, polygonShape from pygame import gfxdraw -import Box2D -from Box2D.b2 import fixtureDef -from Box2D.b2 import polygonShape -from Box2D.b2 import contactListener - import gym from gym import spaces from gym.envs.box2d.car_dynamics import Car -from gym.utils import seeding, EzPickle +from gym.utils import EzPickle, seeding STATE_W = 96 # less than Atari 160x192 STATE_H = 96 diff --git a/gym/envs/box2d/lunar_lander.py b/gym/envs/box2d/lunar_lander.py index 244140aee..171736f39 100644 --- a/gym/envs/box2d/lunar_lander.py +++ b/gym/envs/box2d/lunar_lander.py @@ -4,23 +4,22 @@ import math import sys from typing import Optional +import Box2D import numpy as np import pygame -from pygame import gfxdraw - -import Box2D from Box2D.b2 import ( - edgeShape, circleShape, + contactListener, + edgeShape, fixtureDef, polygonShape, revoluteJointDef, - contactListener, ) +from pygame import gfxdraw import gym from gym import error, spaces -from gym.utils import seeding, EzPickle +from gym.utils import EzPickle, seeding FPS = 50 SCALE = 30.0 # affects how fast-paced the game is, forces should be adjusted as well diff --git a/gym/envs/classic_control/__init__.py b/gym/envs/classic_control/__init__.py index a94cb47b2..308185f39 100644 --- a/gym/envs/classic_control/__init__.py +++ b/gym/envs/classic_control/__init__.py @@ -1,5 +1,5 @@ -from gym.envs.classic_control.cartpole import CartPoleEnv -from gym.envs.classic_control.mountain_car import MountainCarEnv -from gym.envs.classic_control.continuous_mountain_car import Continuous_MountainCarEnv -from gym.envs.classic_control.pendulum import PendulumEnv from gym.envs.classic_control.acrobot import AcrobotEnv +from gym.envs.classic_control.cartpole import CartPoleEnv +from gym.envs.classic_control.continuous_mountain_car import Continuous_MountainCarEnv +from gym.envs.classic_control.mountain_car import MountainCarEnv +from gym.envs.classic_control.pendulum import PendulumEnv diff --git a/gym/envs/classic_control/acrobot.py b/gym/envs/classic_control/acrobot.py index 12563b68d..a5bb1e4b7 100644 --- a/gym/envs/classic_control/acrobot.py +++ b/gym/envs/classic_control/acrobot.py @@ -3,8 +3,8 @@ from typing import Optional import numpy as np import pygame +from numpy import cos, pi, sin from pygame import gfxdraw -from numpy import sin, cos, pi from gym import core, spaces from gym.utils import seeding @@ -240,15 +240,15 @@ class AcrobotEnv(core.Env): dtheta1 = s[2] dtheta2 = s[3] d1 = ( - m1 * lc1 ** 2 - + m2 * (l1 ** 2 + lc2 ** 2 + 2 * l1 * lc2 * cos(theta2)) + m1 * lc1**2 + + m2 * (l1**2 + lc2**2 + 2 * l1 * lc2 * cos(theta2)) + I1 + I2 ) - d2 = m2 * (lc2 ** 2 + l1 * lc2 * cos(theta2)) + I2 + d2 = m2 * (lc2**2 + l1 * lc2 * cos(theta2)) + I2 phi2 = m2 * lc2 * g * cos(theta1 + theta2 - pi / 2.0) phi1 = ( - -m2 * l1 * lc2 * dtheta2 ** 2 * sin(theta2) + -m2 * l1 * lc2 * dtheta2**2 * sin(theta2) - 2 * m2 * l1 * lc2 * dtheta2 * dtheta1 * sin(theta2) + (m1 * lc1 + m2 * l1) * g * cos(theta1 - pi / 2) + phi2 @@ -256,13 +256,13 @@ class AcrobotEnv(core.Env): if self.book_or_nips == "nips": # the following line is consistent with the description in the # paper - ddtheta2 = (a + d2 / d1 * phi1 - phi2) / (m2 * lc2 ** 2 + I2 - d2 ** 2 / d1) + ddtheta2 = (a + d2 / d1 * phi1 - phi2) / (m2 * lc2**2 + I2 - d2**2 / d1) else: # the following line is consistent with the java implementation and the # book ddtheta2 = ( - a + d2 / d1 * phi1 - m2 * l1 * lc2 * dtheta1 ** 2 * sin(theta2) - phi2 - ) / (m2 * lc2 ** 2 + I2 - d2 ** 2 / d1) + a + d2 / d1 * phi1 - m2 * l1 * lc2 * dtheta1**2 * sin(theta2) - phi2 + ) / (m2 * lc2**2 + I2 - d2**2 / d1) ddtheta1 = -(d2 * ddtheta2 + phi1) / d1 return (dtheta1, dtheta2, ddtheta1, ddtheta2, 0.0) diff --git a/gym/envs/classic_control/cartpole.py b/gym/envs/classic_control/cartpole.py index 3773b7bd3..c39bbc265 100644 --- a/gym/envs/classic_control/cartpole.py +++ b/gym/envs/classic_control/cartpole.py @@ -11,7 +11,7 @@ import pygame from pygame import gfxdraw import gym -from gym import spaces, logger +from gym import logger, spaces from gym.utils import seeding @@ -125,10 +125,10 @@ class CartPoleEnv(gym.Env[np.ndarray, Union[int, np.ndarray]]): # For the interested reader: # https://coneural.org/florian/papers/05_cart_pole.pdf temp = ( - force + self.polemass_length * theta_dot ** 2 * sintheta + force + self.polemass_length * theta_dot**2 * sintheta ) / self.total_mass thetaacc = (self.gravity * sintheta - costheta * temp) / ( - self.length * (4.0 / 3.0 - self.masspole * costheta ** 2 / self.total_mass) + self.length * (4.0 / 3.0 - self.masspole * costheta**2 / self.total_mass) ) xacc = temp - self.polemass_length * thetaacc * costheta / self.total_mass diff --git a/gym/envs/classic_control/pendulum.py b/gym/envs/classic_control/pendulum.py index f1526ff8e..1d75f6c03 100644 --- a/gym/envs/classic_control/pendulum.py +++ b/gym/envs/classic_control/pendulum.py @@ -1,7 +1,7 @@ __credits__ = ["Carlos Luis"] -from typing import Optional from os import path +from typing import Optional import numpy as np import pygame @@ -113,9 +113,9 @@ class PendulumEnv(gym.Env): u = np.clip(u, -self.max_torque, self.max_torque)[0] self.last_u = u # for rendering - costs = angle_normalize(th) ** 2 + 0.1 * thdot ** 2 + 0.001 * (u ** 2) + costs = angle_normalize(th) ** 2 + 0.1 * thdot**2 + 0.001 * (u**2) - newthdot = thdot + (3 * g / (2 * l) * np.sin(th) + 3.0 / (m * l ** 2) * u) * dt + newthdot = thdot + (3 * g / (2 * l) * np.sin(th) + 3.0 / (m * l**2) * u) * dt newthdot = np.clip(newthdot, -self.max_speed, self.max_speed) newth = th + newthdot * dt diff --git a/gym/envs/mujoco/__init__.py b/gym/envs/mujoco/__init__.py index 97579470a..c81f0dec3 100644 --- a/gym/envs/mujoco/__init__.py +++ b/gym/envs/mujoco/__init__.py @@ -1,15 +1,14 @@ -from gym.envs.mujoco.mujoco_env import MujocoEnv - # ^^^^^ so that user gets the correct error # message if mujoco is not installed correctly from gym.envs.mujoco.ant import AntEnv from gym.envs.mujoco.half_cheetah import HalfCheetahEnv from gym.envs.mujoco.hopper import HopperEnv -from gym.envs.mujoco.walker2d import Walker2dEnv from gym.envs.mujoco.humanoid import HumanoidEnv -from gym.envs.mujoco.inverted_pendulum import InvertedPendulumEnv +from gym.envs.mujoco.humanoidstandup import HumanoidStandupEnv from gym.envs.mujoco.inverted_double_pendulum import InvertedDoublePendulumEnv +from gym.envs.mujoco.inverted_pendulum import InvertedPendulumEnv +from gym.envs.mujoco.mujoco_env import MujocoEnv +from gym.envs.mujoco.pusher import PusherEnv from gym.envs.mujoco.reacher import ReacherEnv from gym.envs.mujoco.swimmer import SwimmerEnv -from gym.envs.mujoco.humanoidstandup import HumanoidStandupEnv -from gym.envs.mujoco.pusher import PusherEnv +from gym.envs.mujoco.walker2d import Walker2dEnv diff --git a/gym/envs/mujoco/ant.py b/gym/envs/mujoco/ant.py index 2304b9140..e61b787db 100644 --- a/gym/envs/mujoco/ant.py +++ b/gym/envs/mujoco/ant.py @@ -1,4 +1,5 @@ import numpy as np + from gym import utils from gym.envs.mujoco import mujoco_env diff --git a/gym/envs/mujoco/ant_v3.py b/gym/envs/mujoco/ant_v3.py index e037e92ae..aeffa5075 100644 --- a/gym/envs/mujoco/ant_v3.py +++ b/gym/envs/mujoco/ant_v3.py @@ -1,8 +1,8 @@ import numpy as np + from gym import utils from gym.envs.mujoco import mujoco_env - DEFAULT_CAMERA_CONFIG = { "distance": 4.0, } diff --git a/gym/envs/mujoco/half_cheetah.py b/gym/envs/mujoco/half_cheetah.py index 61674d939..53a206fb6 100644 --- a/gym/envs/mujoco/half_cheetah.py +++ b/gym/envs/mujoco/half_cheetah.py @@ -1,4 +1,5 @@ import numpy as np + from gym import utils from gym.envs.mujoco import mujoco_env diff --git a/gym/envs/mujoco/half_cheetah_v3.py b/gym/envs/mujoco/half_cheetah_v3.py index 468f578ff..64104f867 100644 --- a/gym/envs/mujoco/half_cheetah_v3.py +++ b/gym/envs/mujoco/half_cheetah_v3.py @@ -1,9 +1,9 @@ __credits__ = ["Rushiv Arora"] import numpy as np + from gym import utils from gym.envs.mujoco import mujoco_env - DEFAULT_CAMERA_CONFIG = { "distance": 4.0, } diff --git a/gym/envs/mujoco/hopper.py b/gym/envs/mujoco/hopper.py index 46bf0a7f5..ad459bda2 100644 --- a/gym/envs/mujoco/hopper.py +++ b/gym/envs/mujoco/hopper.py @@ -1,4 +1,5 @@ import numpy as np + from gym import utils from gym.envs.mujoco import mujoco_env diff --git a/gym/envs/mujoco/hopper_v3.py b/gym/envs/mujoco/hopper_v3.py index 5ad008ca8..807b6976f 100644 --- a/gym/envs/mujoco/hopper_v3.py +++ b/gym/envs/mujoco/hopper_v3.py @@ -1,9 +1,9 @@ __credits__ = ["Rushiv Arora"] import numpy as np -from gym.envs.mujoco import mujoco_env -from gym import utils +from gym import utils +from gym.envs.mujoco import mujoco_env DEFAULT_CAMERA_CONFIG = { "trackbodyid": 2, diff --git a/gym/envs/mujoco/humanoid.py b/gym/envs/mujoco/humanoid.py index ae8f1eaee..d02541993 100644 --- a/gym/envs/mujoco/humanoid.py +++ b/gym/envs/mujoco/humanoid.py @@ -1,6 +1,7 @@ import numpy as np -from gym.envs.mujoco import mujoco_env + from gym import utils +from gym.envs.mujoco import mujoco_env def mass_center(model, sim): diff --git a/gym/envs/mujoco/humanoid_v3.py b/gym/envs/mujoco/humanoid_v3.py index 63eeeddda..a17887b0f 100644 --- a/gym/envs/mujoco/humanoid_v3.py +++ b/gym/envs/mujoco/humanoid_v3.py @@ -1,7 +1,7 @@ import numpy as np -from gym.envs.mujoco import mujoco_env -from gym import utils +from gym import utils +from gym.envs.mujoco import mujoco_env DEFAULT_CAMERA_CONFIG = { "trackbodyid": 1, diff --git a/gym/envs/mujoco/humanoidstandup.py b/gym/envs/mujoco/humanoidstandup.py index ffe9fca08..d1a6b4742 100644 --- a/gym/envs/mujoco/humanoidstandup.py +++ b/gym/envs/mujoco/humanoidstandup.py @@ -1,7 +1,8 @@ -from gym.envs.mujoco import mujoco_env -from gym import utils import numpy as np +from gym import utils +from gym.envs.mujoco import mujoco_env + class HumanoidStandupEnv(mujoco_env.MujocoEnv, utils.EzPickle): """ diff --git a/gym/envs/mujoco/inverted_double_pendulum.py b/gym/envs/mujoco/inverted_double_pendulum.py index 8e499cb36..f523d170e 100644 --- a/gym/envs/mujoco/inverted_double_pendulum.py +++ b/gym/envs/mujoco/inverted_double_pendulum.py @@ -1,4 +1,5 @@ import numpy as np + from gym import utils from gym.envs.mujoco import mujoco_env @@ -117,9 +118,9 @@ class InvertedDoublePendulumEnv(mujoco_env.MujocoEnv, utils.EzPickle): self.do_simulation(action, self.frame_skip) ob = self._get_obs() x, _, y = self.sim.data.site_xpos[0] - dist_penalty = 0.01 * x ** 2 + (y - 2) ** 2 + dist_penalty = 0.01 * x**2 + (y - 2) ** 2 v1, v2 = self.sim.data.qvel[1:3] - vel_penalty = 1e-3 * v1 ** 2 + 5e-3 * v2 ** 2 + vel_penalty = 1e-3 * v1**2 + 5e-3 * v2**2 alive_bonus = 10 r = alive_bonus - dist_penalty - vel_penalty done = bool(y <= 1) diff --git a/gym/envs/mujoco/inverted_pendulum.py b/gym/envs/mujoco/inverted_pendulum.py index 40bf31b1e..46472cf43 100644 --- a/gym/envs/mujoco/inverted_pendulum.py +++ b/gym/envs/mujoco/inverted_pendulum.py @@ -1,4 +1,5 @@ import numpy as np + from gym import utils from gym.envs.mujoco import mujoco_env diff --git a/gym/envs/mujoco/mujoco_env.py b/gym/envs/mujoco/mujoco_env.py index d19ad1f27..1d04f2f90 100644 --- a/gym/envs/mujoco/mujoco_env.py +++ b/gym/envs/mujoco/mujoco_env.py @@ -1,12 +1,13 @@ -from collections import OrderedDict import os +from collections import OrderedDict +from os import path from typing import Optional +import numpy as np + +import gym from gym import error, spaces from gym.utils import seeding -import numpy as np -from os import path -import gym try: import mujoco_py diff --git a/gym/envs/mujoco/pusher.py b/gym/envs/mujoco/pusher.py index 7698ae8b7..3be870e01 100644 --- a/gym/envs/mujoco/pusher.py +++ b/gym/envs/mujoco/pusher.py @@ -1,9 +1,9 @@ +import mujoco_py import numpy as np + from gym import utils from gym.envs.mujoco import mujoco_env -import mujoco_py - class PusherEnv(mujoco_env.MujocoEnv, utils.EzPickle): """ diff --git a/gym/envs/mujoco/reacher.py b/gym/envs/mujoco/reacher.py index d0638bf87..0df3a974b 100644 --- a/gym/envs/mujoco/reacher.py +++ b/gym/envs/mujoco/reacher.py @@ -1,4 +1,5 @@ import numpy as np + from gym import utils from gym.envs.mujoco import mujoco_env diff --git a/gym/envs/mujoco/swimmer.py b/gym/envs/mujoco/swimmer.py index f903184da..429852f79 100644 --- a/gym/envs/mujoco/swimmer.py +++ b/gym/envs/mujoco/swimmer.py @@ -1,4 +1,5 @@ import numpy as np + from gym import utils from gym.envs.mujoco import mujoco_env diff --git a/gym/envs/mujoco/swimmer_v3.py b/gym/envs/mujoco/swimmer_v3.py index 43869f0f2..db07f238f 100644 --- a/gym/envs/mujoco/swimmer_v3.py +++ b/gym/envs/mujoco/swimmer_v3.py @@ -1,9 +1,9 @@ __credits__ = ["Rushiv Arora"] import numpy as np -from gym.envs.mujoco import mujoco_env -from gym import utils +from gym import utils +from gym.envs.mujoco import mujoco_env DEFAULT_CAMERA_CONFIG = {} diff --git a/gym/envs/mujoco/walker2d.py b/gym/envs/mujoco/walker2d.py index 5f49b4bf2..915ff45f7 100644 --- a/gym/envs/mujoco/walker2d.py +++ b/gym/envs/mujoco/walker2d.py @@ -1,4 +1,5 @@ import numpy as np + from gym import utils from gym.envs.mujoco import mujoco_env diff --git a/gym/envs/mujoco/walker2d_v3.py b/gym/envs/mujoco/walker2d_v3.py index 51ff53a1b..2c091810f 100644 --- a/gym/envs/mujoco/walker2d_v3.py +++ b/gym/envs/mujoco/walker2d_v3.py @@ -1,7 +1,7 @@ import numpy as np -from gym.envs.mujoco import mujoco_env -from gym import utils +from gym import utils +from gym.envs.mujoco import mujoco_env DEFAULT_CAMERA_CONFIG = { "trackbodyid": 2, diff --git a/gym/envs/registration.py b/gym/envs/registration.py index ce9e4b043..45e94a351 100644 --- a/gym/envs/registration.py +++ b/gym/envs/registration.py @@ -1,24 +1,24 @@ from __future__ import annotations -import re -import sys +import contextlib import copy import difflib import importlib import importlib.util -import contextlib +import re +import sys from typing import ( - Callable, - Type, - Optional, - Union, - Tuple, - Generator, - Sequence, - cast, - SupportsFloat, - overload, Any, + Callable, + Generator, + Optional, + Sequence, + SupportsFloat, + Tuple, + Type, + Union, + cast, + overload, ) if sys.version_info < (3, 10): @@ -26,16 +26,15 @@ if sys.version_info < (3, 10): else: import importlib.metadata as metadata -from dataclasses import dataclass, field, InitVar from collections import defaultdict from collections.abc import MutableMapping +from dataclasses import InitVar, dataclass, field import numpy as np -from gym import error, logger, Env +from gym import Env, error, logger from gym.envs.__relocated__ import internal_env_relocation_map - if sys.version_info >= (3, 8): from typing import Literal else: @@ -670,9 +669,9 @@ def make(id: Literal[ # ---------------------------------------- @overload -def make(id: str, **kwargs) -> "Env": ... +def make(id: str, **kwargs) -> Env: ... # fmt: on -def make(id: str, **kwargs) -> "Env": +def make(id: str, **kwargs) -> Env: return registry.make(id, **kwargs) diff --git a/gym/envs/toy_text/__init__.py b/gym/envs/toy_text/__init__.py index 8adeb2abe..36e120795 100644 --- a/gym/envs/toy_text/__init__.py +++ b/gym/envs/toy_text/__init__.py @@ -1,4 +1,4 @@ from gym.envs.toy_text.blackjack import BlackjackEnv -from gym.envs.toy_text.frozen_lake import FrozenLakeEnv from gym.envs.toy_text.cliffwalking import CliffWalkingEnv +from gym.envs.toy_text.frozen_lake import FrozenLakeEnv from gym.envs.toy_text.taxi import TaxiEnv diff --git a/gym/envs/toy_text/blackjack.py b/gym/envs/toy_text/blackjack.py index 5c76c606a..50181e94c 100644 --- a/gym/envs/toy_text/blackjack.py +++ b/gym/envs/toy_text/blackjack.py @@ -1,5 +1,5 @@ -from typing import Optional import os +from typing import Optional import numpy as np import pygame diff --git a/gym/envs/toy_text/cliffwalking.py b/gym/envs/toy_text/cliffwalking.py index b28cf0510..27188b3b0 100644 --- a/gym/envs/toy_text/cliffwalking.py +++ b/gym/envs/toy_text/cliffwalking.py @@ -4,6 +4,7 @@ from io import StringIO from typing import Optional import numpy as np + from gym import Env, spaces from gym.envs.toy_text.utils import categorical_sample diff --git a/gym/envs/toy_text/frozen_lake.py b/gym/envs/toy_text/frozen_lake.py index 78b3e10ee..58e638435 100644 --- a/gym/envs/toy_text/frozen_lake.py +++ b/gym/envs/toy_text/frozen_lake.py @@ -2,9 +2,10 @@ from contextlib import closing from io import StringIO from os import path from typing import Optional + +import numpy as np import pygame from pygame.constants import SRCALPHA -import numpy as np from gym import Env, spaces, utils from gym.envs.toy_text.utils import categorical_sample diff --git a/gym/envs/toy_text/taxi.py b/gym/envs/toy_text/taxi.py index e033b57ca..ff92177d6 100644 --- a/gym/envs/toy_text/taxi.py +++ b/gym/envs/toy_text/taxi.py @@ -4,6 +4,7 @@ from io import StringIO from typing import Optional import numpy as np + from gym import Env, spaces, utils from gym.envs.toy_text.utils import categorical_sample diff --git a/gym/spaces/__init__.py b/gym/spaces/__init__.py index 45f403d0e..47d64faf8 100644 --- a/gym/spaces/__init__.py +++ b/gym/spaces/__init__.py @@ -1,15 +1,11 @@ -from gym.spaces.space import Space from gym.spaces.box import Box -from gym.spaces.discrete import Discrete -from gym.spaces.multi_discrete import MultiDiscrete -from gym.spaces.multi_binary import MultiBinary -from gym.spaces.tuple import Tuple from gym.spaces.dict import Dict - -from gym.spaces.utils import flatdim -from gym.spaces.utils import flatten_space -from gym.spaces.utils import flatten -from gym.spaces.utils import unflatten +from gym.spaces.discrete import Discrete +from gym.spaces.multi_binary import MultiBinary +from gym.spaces.multi_discrete import MultiDiscrete +from gym.spaces.space import Space +from gym.spaces.tuple import Tuple +from gym.spaces.utils import flatdim, flatten, flatten_space, unflatten __all__ = [ "Space", diff --git a/gym/spaces/box.py b/gym/spaces/box.py index 9066a5a29..fe92e8813 100644 --- a/gym/spaces/box.py +++ b/gym/spaces/box.py @@ -1,12 +1,13 @@ from __future__ import annotations -from typing import Tuple, SupportsFloat, Union, Type, Optional, Sequence +from typing import Optional, Sequence, SupportsFloat, Tuple, Type, Union import numpy as np -from .space import Space from gym import logger +from .space import Space + def _short_repr(arr: np.ndarray) -> str: """Create a shortened string representation of a numpy array. diff --git a/gym/spaces/dict.py b/gym/spaces/dict.py index 54f71a063..9eaa2840d 100644 --- a/gym/spaces/dict.py +++ b/gym/spaces/dict.py @@ -3,7 +3,9 @@ from __future__ import annotations from collections import OrderedDict from collections.abc import Mapping, Sequence from typing import Dict as TypingDict + import numpy as np + from .space import Space diff --git a/gym/spaces/discrete.py b/gym/spaces/discrete.py index e53731841..193e3bd98 100644 --- a/gym/spaces/discrete.py +++ b/gym/spaces/discrete.py @@ -1,6 +1,7 @@ from typing import Optional import numpy as np + from .space import Space diff --git a/gym/spaces/multi_binary.py b/gym/spaces/multi_binary.py index 55ae4f61d..af457bba2 100644 --- a/gym/spaces/multi_binary.py +++ b/gym/spaces/multi_binary.py @@ -1,7 +1,9 @@ from __future__ import annotations -from typing import Optional, Union, Sequence +from typing import Optional, Sequence, Union + import numpy as np + from .space import Space diff --git a/gym/spaces/multi_discrete.py b/gym/spaces/multi_discrete.py index 4c1676d7c..e160513df 100644 --- a/gym/spaces/multi_discrete.py +++ b/gym/spaces/multi_discrete.py @@ -1,10 +1,13 @@ from __future__ import annotations from collections.abc import Sequence + import numpy as np + from gym import logger -from .space import Space + from .discrete import Discrete +from .space import Space class MultiDiscrete(Space[np.ndarray]): diff --git a/gym/spaces/space.py b/gym/spaces/space.py index 86abf3a5a..0693f346a 100644 --- a/gym/spaces/space.py +++ b/gym/spaces/space.py @@ -1,20 +1,11 @@ from __future__ import annotations -from typing import ( - TypeVar, - Generic, - Optional, - Sequence, - Iterable, - Mapping, - Type, -) +from typing import Generic, Iterable, Mapping, Optional, Sequence, Type, TypeVar import numpy as np from gym.utils import seeding - T_cov = TypeVar("T_cov", covariant=True) diff --git a/gym/spaces/tuple.py b/gym/spaces/tuple.py index cf38fc97c..750f7f855 100644 --- a/gym/spaces/tuple.py +++ b/gym/spaces/tuple.py @@ -1,6 +1,8 @@ from collections.abc import Sequence from typing import Iterable, List, Optional, Union + import numpy as np + from .space import Space diff --git a/gym/spaces/utils.py b/gym/spaces/utils.py index 38a28094c..9f2252f01 100644 --- a/gym/spaces/utils.py +++ b/gym/spaces/utils.py @@ -1,18 +1,13 @@ from __future__ import annotations -from collections import OrderedDict -from functools import singledispatch, reduce -from typing import TypeVar, Union -import numpy as np import operator as op +from collections import OrderedDict +from functools import reduce, singledispatch +from typing import TypeVar, Union -from gym.spaces import Box -from gym.spaces import Discrete -from gym.spaces import MultiDiscrete -from gym.spaces import MultiBinary -from gym.spaces import Tuple -from gym.spaces import Dict -from gym.spaces import Space +import numpy as np + +from gym.spaces import Box, Dict, Discrete, MultiBinary, MultiDiscrete, Space, Tuple @singledispatch diff --git a/gym/utils/env_checker.py b/gym/utils/env_checker.py index 7d60db0be..f34bd9b6b 100644 --- a/gym/utils/env_checker.py +++ b/gym/utils/env_checker.py @@ -10,13 +10,13 @@ Original Author: J K Terry These projects are covered by the MIT License. """ -from typing import Union, Optional import inspect +from typing import Optional, Union + +import numpy as np import gym -import numpy as np -from gym import logger -from gym import spaces +from gym import logger, spaces def _is_numpy_array_space(space: spaces.Space) -> bool: diff --git a/gym/utils/play.py b/gym/utils/play.py index 30c51a141..746678205 100644 --- a/gym/utils/play.py +++ b/gym/utils/play.py @@ -1,7 +1,9 @@ -import gym -import pygame -import matplotlib import argparse + +import matplotlib +import pygame + +import gym from gym import logger try: @@ -12,6 +14,7 @@ except ImportError as e: plt = None from collections import deque + from pygame.locals import VIDEORESIZE diff --git a/gym/utils/seeding.py b/gym/utils/seeding.py index 7c3f629d9..4d29b3d66 100644 --- a/gym/utils/seeding.py +++ b/gym/utils/seeding.py @@ -1,7 +1,7 @@ import hashlib -from typing import Optional, List, Tuple, Union, Any import os import struct +from typing import Any, List, Optional, Tuple, Union import numpy as np from numpy.random import Generator @@ -198,6 +198,6 @@ def _int_list_from_bigint(bigint: int) -> List[int]: ints: List[int] = [] while bigint > 0: - bigint, mod = divmod(bigint, 2 ** 32) + bigint, mod = divmod(bigint, 2**32) ints.append(mod) return ints diff --git a/gym/vector/async_vector_env.py b/gym/vector/async_vector_env.py index 651c079f8..e9c1c99f4 100644 --- a/gym/vector/async_vector_env.py +++ b/gym/vector/async_vector_env.py @@ -1,31 +1,31 @@ -from typing import Optional, Union, List +import multiprocessing as mp +import sys +import time +from copy import deepcopy +from enum import Enum +from typing import List, Optional, Union import numpy as np -import multiprocessing as mp -import time -import sys -from enum import Enum -from copy import deepcopy from gym import logger -from gym.logger import warn -from gym.vector.vector_env import VectorEnv from gym.error import ( AlreadyPendingCallError, - NoAsyncCallError, ClosedEnvironmentError, CustomSpaceError, + NoAsyncCallError, ) +from gym.logger import warn from gym.vector.utils import ( - create_shared_memory, - create_empty_array, - write_to_shared_memory, - read_from_shared_memory, - concatenate, - iterate, CloudpickleWrapper, clear_mpi_env_vars, + concatenate, + create_empty_array, + create_shared_memory, + iterate, + read_from_shared_memory, + write_to_shared_memory, ) +from gym.vector.vector_env import VectorEnv __all__ = ["AsyncVectorEnv"] diff --git a/gym/vector/sync_vector_env.py b/gym/vector/sync_vector_env.py index 5853d97c2..97913499b 100644 --- a/gym/vector/sync_vector_env.py +++ b/gym/vector/sync_vector_env.py @@ -1,12 +1,12 @@ -from typing import List, Union, Optional +from copy import deepcopy +from typing import List, Optional, Union import numpy as np -from copy import deepcopy from gym import logger from gym.logger import warn +from gym.vector.utils import concatenate, create_empty_array, iterate from gym.vector.vector_env import VectorEnv -from gym.vector.utils import concatenate, iterate, create_empty_array __all__ = ["SyncVectorEnv"] diff --git a/gym/vector/utils/numpy_utils.py b/gym/vector/utils/numpy_utils.py index 36d411577..6ea257618 100644 --- a/gym/vector/utils/numpy_utils.py +++ b/gym/vector/utils/numpy_utils.py @@ -1,10 +1,10 @@ +from collections import OrderedDict +from functools import singledispatch + import numpy as np -from gym.spaces import Space, Box, Discrete, MultiDiscrete, MultiBinary, Tuple, Dict +from gym.spaces import Box, Dict, Discrete, MultiBinary, MultiDiscrete, Space, Tuple from gym.vector.utils.spaces import _BaseGymSpaces -from collections import OrderedDict - -from functools import singledispatch __all__ = ["concatenate", "create_empty_array"] diff --git a/gym/vector/utils/shared_memory.py b/gym/vector/utils/shared_memory.py index f680bb147..497caeb4c 100644 --- a/gym/vector/utils/shared_memory.py +++ b/gym/vector/utils/shared_memory.py @@ -1,15 +1,15 @@ -import numpy as np import multiprocessing as mp -from ctypes import c_bool from collections import OrderedDict +from ctypes import c_bool +from functools import singledispatch + +import numpy as np from gym import logger -from gym.spaces import Space, Box, Discrete, MultiDiscrete, MultiBinary, Tuple, Dict from gym.error import CustomSpaceError +from gym.spaces import Box, Dict, Discrete, MultiBinary, MultiDiscrete, Space, Tuple from gym.vector.utils.spaces import _BaseGymSpaces -from functools import singledispatch - __all__ = ["create_shared_memory", "read_from_shared_memory", "write_to_shared_memory"] diff --git a/gym/vector/utils/spaces.py b/gym/vector/utils/spaces.py index 4ff0bed5b..c8e782a21 100644 --- a/gym/vector/utils/spaces.py +++ b/gym/vector/utils/spaces.py @@ -1,9 +1,10 @@ -import numpy as np from collections import OrderedDict from functools import singledispatch -from gym.spaces import Space, Box, Discrete, MultiDiscrete, MultiBinary, Tuple, Dict +import numpy as np + from gym.error import CustomSpaceError +from gym.spaces import Box, Dict, Discrete, MultiBinary, MultiDiscrete, Space, Tuple _BaseGymSpaces = (Box, Discrete, MultiDiscrete, MultiBinary) __all__ = ["_BaseGymSpaces", "batch_space", "iterate"] @@ -131,8 +132,7 @@ def iterate(space, items): StopIteration """ raise ValueError( - "Space of type `{0}` is not a valid `gym.Space` " - "instance.".format(type(space)) + "Space of type `{}` is not a valid `gym.Space` " "instance.".format(type(space)) ) diff --git a/gym/vector/vector_env.py b/gym/vector/vector_env.py index 961d9d667..ac2f2fde1 100644 --- a/gym/vector/vector_env.py +++ b/gym/vector/vector_env.py @@ -1,7 +1,7 @@ -from typing import Optional, Union, List +from typing import List, Optional, Union import gym -from gym.logger import warn, deprecation +from gym.logger import deprecation, warn from gym.spaces import Tuple from gym.vector.utils.spaces import batch_space diff --git a/gym/wrappers/__init__.py b/gym/wrappers/__init__.py index 1af8fd708..caadb4074 100644 --- a/gym/wrappers/__init__.py +++ b/gym/wrappers/__init__.py @@ -1,19 +1,18 @@ from gym import error -from gym.wrappers.time_limit import TimeLimit -from gym.wrappers.filter_observation import FilterObservation from gym.wrappers.atari_preprocessing import AtariPreprocessing -from gym.wrappers.time_aware_observation import TimeAwareObservation -from gym.wrappers.rescale_action import RescaleAction +from gym.wrappers.autoreset import AutoResetWrapper +from gym.wrappers.clip_action import ClipAction +from gym.wrappers.filter_observation import FilterObservation from gym.wrappers.flatten_observation import FlattenObservation +from gym.wrappers.frame_stack import FrameStack, LazyFrames from gym.wrappers.gray_scale_observation import GrayScaleObservation -from gym.wrappers.frame_stack import LazyFrames -from gym.wrappers.frame_stack import FrameStack +from gym.wrappers.normalize import NormalizeObservation, NormalizeReward +from gym.wrappers.order_enforcing import OrderEnforcing +from gym.wrappers.record_episode_statistics import RecordEpisodeStatistics +from gym.wrappers.record_video import RecordVideo, capped_cubic_video_schedule +from gym.wrappers.rescale_action import RescaleAction +from gym.wrappers.resize_observation import ResizeObservation +from gym.wrappers.time_aware_observation import TimeAwareObservation +from gym.wrappers.time_limit import TimeLimit from gym.wrappers.transform_observation import TransformObservation from gym.wrappers.transform_reward import TransformReward -from gym.wrappers.resize_observation import ResizeObservation -from gym.wrappers.clip_action import ClipAction -from gym.wrappers.record_episode_statistics import RecordEpisodeStatistics -from gym.wrappers.normalize import NormalizeObservation, NormalizeReward -from gym.wrappers.record_video import RecordVideo, capped_cubic_video_schedule -from gym.wrappers.order_enforcing import OrderEnforcing -from gym.wrappers.autoreset import AutoResetWrapper diff --git a/gym/wrappers/atari_preprocessing.py b/gym/wrappers/atari_preprocessing.py index 15f1c43fb..33a70c3ae 100644 --- a/gym/wrappers/atari_preprocessing.py +++ b/gym/wrappers/atari_preprocessing.py @@ -1,4 +1,5 @@ import numpy as np + import gym from gym.spaces import Box diff --git a/gym/wrappers/clip_action.py b/gym/wrappers/clip_action.py index 15631d625..6ae1862ca 100644 --- a/gym/wrappers/clip_action.py +++ b/gym/wrappers/clip_action.py @@ -1,4 +1,5 @@ import numpy as np + from gym import ActionWrapper from gym.spaces import Box diff --git a/gym/wrappers/filter_observation.py b/gym/wrappers/filter_observation.py index 57bc1e2b7..e765ee017 100644 --- a/gym/wrappers/filter_observation.py +++ b/gym/wrappers/filter_observation.py @@ -1,6 +1,6 @@ import copy -from gym import spaces -from gym import ObservationWrapper + +from gym import ObservationWrapper, spaces class FilterObservation(ObservationWrapper): diff --git a/gym/wrappers/frame_stack.py b/gym/wrappers/frame_stack.py index 7337b521e..0af589bdf 100644 --- a/gym/wrappers/frame_stack.py +++ b/gym/wrappers/frame_stack.py @@ -2,8 +2,9 @@ from collections import deque from typing import Optional import numpy as np -from gym.spaces import Box + from gym import ObservationWrapper +from gym.spaces import Box class LazyFrames: diff --git a/gym/wrappers/gray_scale_observation.py b/gym/wrappers/gray_scale_observation.py index 10d087495..0f4476097 100644 --- a/gym/wrappers/gray_scale_observation.py +++ b/gym/wrappers/gray_scale_observation.py @@ -1,6 +1,7 @@ import numpy as np -from gym.spaces import Box + from gym import ObservationWrapper +from gym.spaces import Box class GrayScaleObservation(ObservationWrapper): diff --git a/gym/wrappers/monitoring/video_recorder.py b/gym/wrappers/monitoring/video_recorder.py index 38a307690..01b92a7a0 100644 --- a/gym/wrappers/monitoring/video_recorder.py +++ b/gym/wrappers/monitoring/video_recorder.py @@ -1,3 +1,5 @@ +import distutils.spawn +import distutils.version import json import os import os.path @@ -6,8 +8,6 @@ import subprocess import tempfile from io import StringIO -import distutils.spawn -import distutils.version import numpy as np from gym import error, logger diff --git a/gym/wrappers/normalize.py b/gym/wrappers/normalize.py index f12065817..d12a87a2a 100644 --- a/gym/wrappers/normalize.py +++ b/gym/wrappers/normalize.py @@ -1,6 +1,7 @@ from typing import Optional import numpy as np + import gym diff --git a/gym/wrappers/pixel_observation.py b/gym/wrappers/pixel_observation.py index a289d3885..de7a327ab 100644 --- a/gym/wrappers/pixel_observation.py +++ b/gym/wrappers/pixel_observation.py @@ -1,11 +1,10 @@ import collections -from collections.abc import MutableMapping import copy +from collections.abc import MutableMapping + import numpy as np -from gym import spaces -from gym import ObservationWrapper - +from gym import ObservationWrapper, spaces STATE_KEY = "state" diff --git a/gym/wrappers/record_episode_statistics.py b/gym/wrappers/record_episode_statistics.py index ecfd94a57..ab5f51192 100644 --- a/gym/wrappers/record_episode_statistics.py +++ b/gym/wrappers/record_episode_statistics.py @@ -3,6 +3,7 @@ from collections import deque from typing import Optional import numpy as np + import gym diff --git a/gym/wrappers/record_video.py b/gym/wrappers/record_video.py index 351367cf0..dd2efa295 100644 --- a/gym/wrappers/record_video.py +++ b/gym/wrappers/record_video.py @@ -1,7 +1,7 @@ import os -import gym from typing import Callable, Optional +import gym from gym import logger from gym.wrappers.monitoring import video_recorder diff --git a/gym/wrappers/rescale_action.py b/gym/wrappers/rescale_action.py index 368eca359..3d2ad41fd 100644 --- a/gym/wrappers/rescale_action.py +++ b/gym/wrappers/rescale_action.py @@ -1,4 +1,5 @@ import numpy as np + import gym from gym import spaces diff --git a/gym/wrappers/resize_observation.py b/gym/wrappers/resize_observation.py index df66f2a9e..234fe1fd3 100644 --- a/gym/wrappers/resize_observation.py +++ b/gym/wrappers/resize_observation.py @@ -1,6 +1,7 @@ import numpy as np -from gym.spaces import Box + from gym import ObservationWrapper +from gym.spaces import Box class ResizeObservation(ObservationWrapper): diff --git a/gym/wrappers/time_aware_observation.py b/gym/wrappers/time_aware_observation.py index 5a16c3c72..a61d14744 100644 --- a/gym/wrappers/time_aware_observation.py +++ b/gym/wrappers/time_aware_observation.py @@ -1,8 +1,9 @@ from typing import Optional import numpy as np -from gym.spaces import Box + from gym import ObservationWrapper +from gym.spaces import Box class TimeAwareObservation(ObservationWrapper): diff --git a/setup.py b/setup.py index 3db52919b..a4317a7bb 100644 --- a/setup.py +++ b/setup.py @@ -1,6 +1,6 @@ +import itertools import os.path import sys -import itertools from setuptools import find_packages, setup @@ -20,7 +20,7 @@ extras = { } # Meta dependency groups. -nomujoco_blacklist = set(["mujoco", "accept-rom-license", "atari"]) +nomujoco_blacklist = {"mujoco", "accept-rom-license", "atari"} nomujoco_groups = set(extras.keys()) - nomujoco_blacklist extras["nomujoco"] = list( @@ -28,7 +28,7 @@ extras["nomujoco"] = list( ) -all_blacklist = set(["accept-rom-license"]) +all_blacklist = {"accept-rom-license"} all_groups = set(extras.keys()) - all_blacklist extras["all"] = list( diff --git a/tests/envs/spec_list.py b/tests/envs/spec_list.py index 9ad82c009..11c816f6b 100644 --- a/tests/envs/spec_list.py +++ b/tests/envs/spec_list.py @@ -1,6 +1,6 @@ -from gym import envs, logger import os +from gym import envs, logger SKIP_MUJOCO_WARNING_MESSAGE = ( "Cannot run mujoco test (either license key not found or mujoco not" diff --git a/tests/envs/test_action_dim_check.py b/tests/envs/test_action_dim_check.py index 0b43ff84f..448847cd6 100644 --- a/tests/envs/test_action_dim_check.py +++ b/tests/envs/test_action_dim_check.py @@ -3,8 +3,7 @@ import pickle import pytest from gym import envs -from tests.envs.spec_list import skip_mujoco, SKIP_MUJOCO_WARNING_MESSAGE - +from tests.envs.spec_list import SKIP_MUJOCO_WARNING_MESSAGE, skip_mujoco ENVIRONMENT_IDS = ("HalfCheetah-v2",) diff --git a/tests/envs/test_atari_legacy_env_specs.py b/tests/envs/test_atari_legacy_env_specs.py index ae41082a7..37e99245c 100644 --- a/tests/envs/test_atari_legacy_env_specs.py +++ b/tests/envs/test_atari_legacy_env_specs.py @@ -2,10 +2,10 @@ import pytest pytest.importorskip("gym.envs.atari") -from gym.envs.registration import registry - from itertools import product +from gym.envs.registration import registry + def test_ale_legacy_env_specs(): versions = ["-v0", "-v4"] diff --git a/tests/envs/test_envs.py b/tests/envs/test_envs.py index df26e607b..6d59057a6 100644 --- a/tests/envs/test_envs.py +++ b/tests/envs/test_envs.py @@ -1,10 +1,10 @@ -import pytest import numpy as np +import pytest from gym import envs -from tests.envs.spec_list import spec_list from gym.spaces import Box from gym.utils.env_checker import check_env +from tests.envs.spec_list import spec_list # This runs a smoketest on each official registered env. We may want diff --git a/tests/envs/test_frozenlake_dfs.py b/tests/envs/test_frozenlake_dfs.py index 5731a6d4f..3cfeddf2b 100644 --- a/tests/envs/test_frozenlake_dfs.py +++ b/tests/envs/test_frozenlake_dfs.py @@ -1,5 +1,5 @@ -import pytest import numpy as np +import pytest from gym.envs.toy_text.frozen_lake import generate_random_map diff --git a/tests/envs/test_lunar_lander.py b/tests/envs/test_lunar_lander.py index 7f430e13c..5aeab73f5 100644 --- a/tests/envs/test_lunar_lander.py +++ b/tests/envs/test_lunar_lander.py @@ -2,10 +2,8 @@ import pytest try: import Box2D - from gym.envs.box2d.lunar_lander import ( - LunarLander, - demo_heuristic_lander, - ) + + from gym.envs.box2d.lunar_lander import LunarLander, demo_heuristic_lander except ImportError: Box2D = None diff --git a/tests/envs/test_mujoco_v2_to_v3_conversion.py b/tests/envs/test_mujoco_v2_to_v3_conversion.py index be90b2f54..201d49766 100644 --- a/tests/envs/test_mujoco_v2_to_v3_conversion.py +++ b/tests/envs/test_mujoco_v2_to_v3_conversion.py @@ -1,7 +1,9 @@ import unittest + import numpy as np + from gym import envs -from tests.envs.spec_list import skip_mujoco, SKIP_MUJOCO_WARNING_MESSAGE +from tests.envs.spec_list import SKIP_MUJOCO_WARNING_MESSAGE, skip_mujoco def verify_environments_match( diff --git a/tests/envs/test_registration.py b/tests/envs/test_registration.py index a4a7933f8..125b7cef5 100644 --- a/tests/envs/test_registration.py +++ b/tests/envs/test_registration.py @@ -1,8 +1,7 @@ -# -*- coding: utf-8 -*- import pytest import gym -from gym import error, envs +from gym import envs, error from gym.envs import registration from gym.envs.classic_control import cartpole from gym.envs.registration import EnvSpec, EnvSpecTree diff --git a/tests/spaces/test_spaces.py b/tests/spaces/test_spaces.py index ace4ff4e0..975fffa2a 100644 --- a/tests/spaces/test_spaces.py +++ b/tests/spaces/test_spaces.py @@ -1,12 +1,12 @@ -import json # note: ujson fails this test due to float equality import copy +import json # note: ujson fails this test due to float equality import pickle import tempfile import numpy as np import pytest -from gym.spaces import Tuple, Box, Discrete, MultiDiscrete, MultiBinary, Dict +from gym.spaces import Box, Dict, Discrete, MultiBinary, MultiDiscrete, Tuple @pytest.mark.parametrize( diff --git a/tests/spaces/test_utils.py b/tests/spaces/test_utils.py index 4797127cc..9261e212c 100644 --- a/tests/spaces/test_utils.py +++ b/tests/spaces/test_utils.py @@ -5,7 +5,6 @@ import pytest from gym.spaces import Box, Dict, Discrete, MultiBinary, MultiDiscrete, Tuple, utils - spaces = [ Discrete(3), Box(low=0.0, high=np.inf, shape=(2, 2)), diff --git a/tests/test_core.py b/tests/test_core.py index be7c8d715..2d6b0dcd3 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -1,10 +1,10 @@ from typing import Optional -import pytest import numpy as np +import pytest from gym import core, spaces -from gym.wrappers import TimeLimit, OrderEnforcing +from gym.wrappers import OrderEnforcing, TimeLimit class ArgumentEnv(core.Env): diff --git a/tests/utils/test_env_checker.py b/tests/utils/test_env_checker.py index 89e215ace..b50ec4c39 100644 --- a/tests/utils/test_env_checker.py +++ b/tests/utils/test_env_checker.py @@ -1,10 +1,10 @@ from typing import Optional -import gym import numpy as np import pytest -from gym.spaces import Box, Dict, Discrete +import gym +from gym.spaces import Box, Dict, Discrete from gym.utils.env_checker import check_env diff --git a/tests/vector/test_async_vector_env.py b/tests/vector/test_async_vector_env.py index 0473d234c..411047990 100644 --- a/tests/vector/test_async_vector_env.py +++ b/tests/vector/test_async_vector_env.py @@ -1,18 +1,18 @@ -import pytest -import numpy as np - from multiprocessing import TimeoutError -from gym.spaces import Box, Tuple, Discrete, MultiDiscrete -from gym.error import AlreadyPendingCallError, NoAsyncCallError, ClosedEnvironmentError + +import numpy as np +import pytest + +from gym.error import AlreadyPendingCallError, ClosedEnvironmentError, NoAsyncCallError +from gym.spaces import Box, Discrete, MultiDiscrete, Tuple +from gym.vector.async_vector_env import AsyncVectorEnv from tests.vector.utils import ( CustomSpace, + make_custom_space_env, make_env, make_slow_env, - make_custom_space_env, ) -from gym.vector.async_vector_env import AsyncVectorEnv - @pytest.mark.parametrize("shared_memory", [True, False]) def test_create_async_vector_env(shared_memory): diff --git a/tests/vector/test_numpy_utils.py b/tests/vector/test_numpy_utils.py index 2ccdebe08..4698c658e 100644 --- a/tests/vector/test_numpy_utils.py +++ b/tests/vector/test_numpy_utils.py @@ -1,14 +1,13 @@ -import pytest -import numpy as np - from collections import OrderedDict -from gym.spaces import Tuple, Dict +import numpy as np +import pytest + +from gym.spaces import Dict, Tuple +from gym.vector.utils.numpy_utils import concatenate, create_empty_array from gym.vector.utils.spaces import _BaseGymSpaces from tests.vector.utils import spaces -from gym.vector.utils.numpy_utils import concatenate, create_empty_array - @pytest.mark.parametrize( "space", spaces, ids=[space.__class__.__name__ for space in spaces] diff --git a/tests/vector/test_shared_memory.py b/tests/vector/test_shared_memory.py index f10fa534a..120d109dd 100644 --- a/tests/vector/test_shared_memory.py +++ b/tests/vector/test_shared_memory.py @@ -1,22 +1,20 @@ -import pytest -import numpy as np - import multiprocessing as mp -from multiprocessing.sharedctypes import SynchronizedArray -from multiprocessing import Array, Process from collections import OrderedDict +from multiprocessing import Array, Process +from multiprocessing.sharedctypes import SynchronizedArray + +import numpy as np +import pytest -from gym.spaces import Tuple, Dict from gym.error import CustomSpaceError -from gym.vector.utils.spaces import _BaseGymSpaces -from tests.vector.utils import spaces, custom_spaces - +from gym.spaces import Dict, Tuple from gym.vector.utils.shared_memory import ( create_shared_memory, read_from_shared_memory, write_to_shared_memory, ) - +from gym.vector.utils.spaces import _BaseGymSpaces +from tests.vector.utils import custom_spaces, spaces expected_types = [ Array("d", 1), diff --git a/tests/vector/test_spaces.py b/tests/vector/test_spaces.py index 0999ff6bf..d01d5a45a 100644 --- a/tests/vector/test_spaces.py +++ b/tests/vector/test_spaces.py @@ -1,10 +1,9 @@ -import pytest import numpy as np +import pytest -from gym.spaces import Box, MultiDiscrete, Tuple, Dict -from tests.vector.utils import spaces, custom_spaces, CustomSpace - +from gym.spaces import Box, Dict, MultiDiscrete, Tuple from gym.vector.utils.spaces import batch_space, iterate +from tests.vector.utils import CustomSpace, custom_spaces, spaces expected_batch_spaces_4 = [ Box(low=-1.0, high=1.0, shape=(4,), dtype=np.float64), diff --git a/tests/vector/test_sync_vector_env.py b/tests/vector/test_sync_vector_env.py index cd60be570..623803238 100644 --- a/tests/vector/test_sync_vector_env.py +++ b/tests/vector/test_sync_vector_env.py @@ -1,10 +1,9 @@ -import pytest import numpy as np +import pytest -from gym.spaces import Box, Tuple, Discrete, MultiDiscrete -from tests.vector.utils import CustomSpace, make_env, make_custom_space_env - +from gym.spaces import Box, Discrete, MultiDiscrete, Tuple from gym.vector.sync_vector_env import SyncVectorEnv +from tests.vector.utils import CustomSpace, make_custom_space_env, make_env def test_create_sync_vector_env(): diff --git a/tests/vector/test_vector_env.py b/tests/vector/test_vector_env.py index b748b270a..82870d79c 100644 --- a/tests/vector/test_vector_env.py +++ b/tests/vector/test_vector_env.py @@ -1,12 +1,11 @@ -import pytest import numpy as np +import pytest from gym.spaces import Tuple -from tests.vector.utils import CustomSpace, make_env - from gym.vector.async_vector_env import AsyncVectorEnv from gym.vector.sync_vector_env import SyncVectorEnv from gym.vector.vector_env import VectorEnv +from tests.vector.utils import CustomSpace, make_env @pytest.mark.parametrize("shared_memory", [True, False]) diff --git a/tests/vector/test_vector_env_wrapper.py b/tests/vector/test_vector_env_wrapper.py index 4572f0405..4c8d165d1 100644 --- a/tests/vector/test_vector_env_wrapper.py +++ b/tests/vector/test_vector_env_wrapper.py @@ -1,6 +1,5 @@ import gym -from gym.vector import make -from gym.vector import VectorEnvWrapper +from gym.vector import VectorEnvWrapper, make class DummyWrapper(VectorEnvWrapper): diff --git a/tests/vector/utils.py b/tests/vector/utils.py index cfdf493b1..0eadb6726 100644 --- a/tests/vector/utils.py +++ b/tests/vector/utils.py @@ -1,10 +1,10 @@ +import time from typing import Optional import numpy as np -import gym -import time -from gym.spaces import Box, Discrete, MultiDiscrete, MultiBinary, Tuple, Dict +import gym +from gym.spaces import Box, Dict, Discrete, MultiBinary, MultiDiscrete, Tuple spaces = [ Box(low=np.array(-1.0), high=np.array(1.0), dtype=np.float64), diff --git a/tests/wrappers/flatten_test.py b/tests/wrappers/flatten_test.py index 21ba8e99e..620d21aca 100644 --- a/tests/wrappers/flatten_test.py +++ b/tests/wrappers/flatten_test.py @@ -7,7 +7,7 @@ import numpy as np import pytest import gym -from gym.spaces import Box, Dict, unflatten, flatten +from gym.spaces import Box, Dict, flatten, unflatten from gym.wrappers import FlattenObservation diff --git a/tests/wrappers/nested_dict_test.py b/tests/wrappers/nested_dict_test.py index 2282472d2..bde470541 100644 --- a/tests/wrappers/nested_dict_test.py +++ b/tests/wrappers/nested_dict_test.py @@ -1,11 +1,11 @@ """Tests for the filter observation wrapper.""" from typing import Optional -import pytest import numpy as np +import pytest import gym -from gym.spaces import Dict, Box, Discrete, Tuple +from gym.spaces import Box, Dict, Discrete, Tuple from gym.wrappers import FilterObservation, FlattenObservation diff --git a/tests/wrappers/test_atari_preprocessing.py b/tests/wrappers/test_atari_preprocessing.py index 34951fad4..e36d37688 100644 --- a/tests/wrappers/test_atari_preprocessing.py +++ b/tests/wrappers/test_atari_preprocessing.py @@ -1,7 +1,8 @@ import numpy as np +import pytest + import gym from gym.wrappers import AtariPreprocessing -import pytest pytest.importorskip("gym.envs.atari") diff --git a/tests/wrappers/test_autoreset.py b/tests/wrappers/test_autoreset.py index c6ac3bbed..f003a7280 100644 --- a/tests/wrappers/test_autoreset.py +++ b/tests/wrappers/test_autoreset.py @@ -1,7 +1,7 @@ -import pytest from typing import Optional import numpy as np +import pytest import gym from gym.wrappers import AutoResetWrapper diff --git a/tests/wrappers/test_filter_observation.py b/tests/wrappers/test_filter_observation.py index de39a7e53..e7d5ef2b0 100644 --- a/tests/wrappers/test_filter_observation.py +++ b/tests/wrappers/test_filter_observation.py @@ -1,7 +1,7 @@ from typing import Optional -import pytest import numpy as np +import pytest import gym from gym import spaces diff --git a/tests/wrappers/test_flatten_observation.py b/tests/wrappers/test_flatten_observation.py index 06daaa352..8018317bf 100644 --- a/tests/wrappers/test_flatten_observation.py +++ b/tests/wrappers/test_flatten_observation.py @@ -1,10 +1,9 @@ +import numpy as np import pytest -import numpy as np - import gym -from gym.wrappers import FlattenObservation from gym import spaces +from gym.wrappers import FlattenObservation @pytest.mark.parametrize("env_id", ["Blackjack-v1"]) diff --git a/tests/wrappers/test_frame_stack.py b/tests/wrappers/test_frame_stack.py index 13f836d08..b9af3002c 100644 --- a/tests/wrappers/test_frame_stack.py +++ b/tests/wrappers/test_frame_stack.py @@ -3,6 +3,7 @@ import pytest pytest.importorskip("gym.envs.atari") import numpy as np + import gym from gym.wrappers import FrameStack @@ -39,7 +40,7 @@ def test_frame_stack(env_id, num_stack, lz4_compress): dup_obs = dup.reset(seed=0) assert np.allclose(obs[-1], dup_obs) - for _ in range(num_stack ** 2): + for _ in range(num_stack**2): action = env.action_space.sample() dup_obs, _, _, _ = dup.step(action) obs, _, _, _ = env.step(action) diff --git a/tests/wrappers/test_gray_scale_observation.py b/tests/wrappers/test_gray_scale_observation.py index 3f83ea4a0..27a00f696 100644 --- a/tests/wrappers/test_gray_scale_observation.py +++ b/tests/wrappers/test_gray_scale_observation.py @@ -1,10 +1,8 @@ +import numpy as np import pytest -import numpy as np - import gym -from gym.wrappers import GrayScaleObservation -from gym.wrappers import AtariPreprocessing +from gym.wrappers import AtariPreprocessing, GrayScaleObservation pytest.importorskip("gym.envs.atari") pytest.importorskip("cv2") diff --git a/tests/wrappers/test_normalize.py b/tests/wrappers/test_normalize.py index 40af999f2..13bf32011 100644 --- a/tests/wrappers/test_normalize.py +++ b/tests/wrappers/test_normalize.py @@ -1,10 +1,10 @@ from typing import Optional -import gym import numpy as np -from numpy.testing import assert_almost_equal import pytest +from numpy.testing import assert_almost_equal +import gym from gym.wrappers.normalize import NormalizeObservation, NormalizeReward diff --git a/tests/wrappers/test_order_enforcing.py b/tests/wrappers/test_order_enforcing.py index 5ac23234a..9b9290aec 100644 --- a/tests/wrappers/test_order_enforcing.py +++ b/tests/wrappers/test_order_enforcing.py @@ -1,6 +1,5 @@ -import pytest - import numpy as np +import pytest import gym from gym.wrappers import OrderEnforcing diff --git a/tests/wrappers/test_pixel_observation.py b/tests/wrappers/test_pixel_observation.py index 935ad69d2..95f094579 100644 --- a/tests/wrappers/test_pixel_observation.py +++ b/tests/wrappers/test_pixel_observation.py @@ -1,12 +1,12 @@ """Tests for the pixel observation wrapper.""" from typing import Optional -import pytest import numpy as np +import pytest import gym from gym import spaces -from gym.wrappers.pixel_observation import PixelObservationWrapper, STATE_KEY +from gym.wrappers.pixel_observation import STATE_KEY, PixelObservationWrapper class FakeEnvironment(gym.Env): diff --git a/tests/wrappers/test_record_episode_statistics.py b/tests/wrappers/test_record_episode_statistics.py index 5960ed036..d9633409e 100644 --- a/tests/wrappers/test_record_episode_statistics.py +++ b/tests/wrappers/test_record_episode_statistics.py @@ -1,6 +1,5 @@ -import pytest - import numpy as np +import pytest import gym from gym.wrappers import RecordEpisodeStatistics diff --git a/tests/wrappers/test_record_video.py b/tests/wrappers/test_record_video.py index 277542179..0757c1bec 100644 --- a/tests/wrappers/test_record_video.py +++ b/tests/wrappers/test_record_video.py @@ -1,8 +1,8 @@ -import pytest import os import shutil import numpy as np +import pytest import gym from gym.wrappers import ( diff --git a/tests/wrappers/test_rescale_action.py b/tests/wrappers/test_rescale_action.py index d1a94f5a3..6db5ad5fa 100644 --- a/tests/wrappers/test_rescale_action.py +++ b/tests/wrappers/test_rescale_action.py @@ -1,6 +1,5 @@ -import pytest - import numpy as np +import pytest import gym from gym.wrappers import RescaleAction diff --git a/tests/wrappers/test_time_limit.py b/tests/wrappers/test_time_limit.py index b2033fd02..32e6e5d2a 100644 --- a/tests/wrappers/test_time_limit.py +++ b/tests/wrappers/test_time_limit.py @@ -1,6 +1,5 @@ -import pytest - import numpy as np +import pytest import gym from gym.wrappers import TimeLimit diff --git a/tests/wrappers/test_transform_observation.py b/tests/wrappers/test_transform_observation.py index f22e5526b..fc1076ae4 100644 --- a/tests/wrappers/test_transform_observation.py +++ b/tests/wrappers/test_transform_observation.py @@ -1,6 +1,5 @@ -import pytest - import numpy as np +import pytest import gym from gym.wrappers import TransformObservation diff --git a/tests/wrappers/test_transform_reward.py b/tests/wrappers/test_transform_reward.py index bc2ffbca4..c7badb7a2 100644 --- a/tests/wrappers/test_transform_reward.py +++ b/tests/wrappers/test_transform_reward.py @@ -1,6 +1,5 @@ -import pytest - import numpy as np +import pytest import gym from gym.wrappers import TransformReward