Skip to content

Commit

Permalink
Add support for python 3.6 (#2836)
Browse files Browse the repository at this point in the history
* Add support for python 3.6

* Add support for python 3.6

* Added check for python 3.6 to not install mujoco as no version exists

* Fixed the install groups for python 3.6

* Re-added python 3.6 support for gym

* black

* Added support for dataclasses through dataclasses module in setup that backports the module

* Fixed install requirements

* Re-added dummy env spec with dataclasses

* Changed type for compatability for python 3.6

* Added a python 3.6 warning

* Fixed python 3.6 typing issue

* Removed __future__ import annotation for python 3.6 support

* Fixed python 3.6 typing
  • Loading branch information
pseudo-rnd-thoughts authored May 25, 2022
1 parent 273e3f2 commit 0263deb
Show file tree
Hide file tree
Showing 27 changed files with 148 additions and 154 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ['3.7', '3.8', '3.9', '3.10']
python-version: ['3.6', '3.7', '3.8', '3.9', '3.10']
steps:
- uses: actions/checkout@v2
- run: |
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ repos:
hooks:
- id: pyupgrade
# TODO: remove `--keep-runtime-typing` option
args: ["--py37-plus", "--keep-runtime-typing"]
args: ["--py36-plus", "--keep-runtime-typing"]
- repo: local
hooks:
- id: pyright
Expand Down
28 changes: 16 additions & 12 deletions gym/core.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
"""Core API for Environment, Wrapper, ActionWrapper, RewardWrapper and ObservationWrapper."""
from __future__ import annotations

from typing import Generic, Optional, SupportsFloat, TypeVar, Union
import sys
from typing import Generic, Optional, SupportsFloat, Tuple, TypeVar, Union

from gym import spaces
from gym.logger import deprecation
from gym.logger import deprecation, warn
from gym.utils import seeding
from gym.utils.seeding import RandomNumberGenerator

if sys.version_info == (3, 6):
warn(
"Gym minimally supports python 3.6 as the python foundation not longer supports the version, please update your version to 3.7+"
)

ObsType = TypeVar("ObsType")
ActType = TypeVar("ActType")

Expand Down Expand Up @@ -62,7 +66,7 @@ def np_random(self) -> RandomNumberGenerator:
def np_random(self, value: RandomNumberGenerator):
self._np_random = value

def step(self, action: ActType) -> tuple[ObsType, float, bool, dict]:
def step(self, action: ActType) -> Tuple[ObsType, float, bool, dict]:
"""Run one timestep of the environment's dynamics.
When end of episode is reached, you are responsible for calling :meth:`reset` to reset this environment's state.
Expand Down Expand Up @@ -92,7 +96,7 @@ def reset(
seed: Optional[int] = None,
return_info: bool = False,
options: Optional[dict] = None,
) -> Union[ObsType, tuple[ObsType, dict]]:
) -> Union[ObsType, Tuple[ObsType, dict]]:
"""Resets the environment to an initial state and returns the initial observation.
This method can reset the environment's random number generator(s) if ``seed`` is an integer or
Expand Down Expand Up @@ -201,7 +205,7 @@ def seed(self, seed=None):
return [seed]

@property
def unwrapped(self) -> Env:
def unwrapped(self) -> "Env":
"""Returns the base non-wrapped environment.
Returns:
Expand Down Expand Up @@ -248,7 +252,7 @@ def __init__(self, env: Env):

self._action_space: Optional[spaces.Space] = None
self._observation_space: Optional[spaces.Space] = None
self._reward_range: Optional[tuple[SupportsFloat, SupportsFloat]] = None
self._reward_range: Optional[Tuple[SupportsFloat, SupportsFloat]] = None
self._metadata: Optional[dict] = None

def __getattr__(self, name):
Expand Down Expand Up @@ -290,14 +294,14 @@ def observation_space(self, space: spaces.Space):
self._observation_space = space

@property
def reward_range(self) -> tuple[SupportsFloat, SupportsFloat]:
def reward_range(self) -> Tuple[SupportsFloat, SupportsFloat]:
"""Return the reward range of the environment."""
if self._reward_range is None:
return self.env.reward_range
return self._reward_range

@reward_range.setter
def reward_range(self, value: tuple[SupportsFloat, SupportsFloat]):
def reward_range(self, value: Tuple[SupportsFloat, SupportsFloat]):
self._reward_range = value

@property
Expand All @@ -311,11 +315,11 @@ def metadata(self) -> dict:
def metadata(self, value):
self._metadata = value

def step(self, action: ActType) -> tuple[ObsType, float, bool, dict]:
def step(self, action: ActType) -> Tuple[ObsType, float, bool, dict]:
"""Steps through the environment with action."""
return self.env.step(action)

def reset(self, **kwargs) -> Union[ObsType, tuple[ObsType, dict]]:
def reset(self, **kwargs) -> Union[ObsType, Tuple[ObsType, dict]]:
"""Resets the environment with kwargs."""
return self.env.reset(**kwargs)

Expand Down
46 changes: 21 additions & 25 deletions gym/envs/registration.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from __future__ import annotations

import contextlib
import copy
import difflib
Expand All @@ -10,12 +8,14 @@
import warnings
from dataclasses import dataclass, field
from typing import (
Any,
Callable,
Dict,
Iterable,
List,
Optional,
Sequence,
SupportsFloat,
Tuple,
Union,
overload,
)
Expand All @@ -34,15 +34,11 @@
if sys.version_info >= (3, 8):
from typing import Literal
else:

class Literal(str):
def __class_getitem__(cls, item):
return Any

from typing_extensions import Literal

from gym import Env, error, logger

ENV_ID_RE: re.Pattern = re.compile(
ENV_ID_RE = re.compile(
r"^(?:(?P<namespace>[\w:-]+)\/)?(?:(?P<name>[\w:.-]+?))(?:-v(?P<version>\d+))?$"
)

Expand All @@ -54,7 +50,7 @@ def load(name: str) -> type:
return fn


def parse_env_id(id: str) -> tuple[Optional[str], str, Optional[int]]:
def parse_env_id(id: str) -> Tuple[Optional[str], str, Optional[int]]:
"""Parse environment ID string format.
This format is true today, but it's *not* an official spec.
Expand Down Expand Up @@ -241,7 +237,7 @@ def _check_version_exists(ns: Optional[str], name: str, version: Optional[int]):


def find_highest_version(ns: Optional[str], name: str) -> Optional[int]:
version: list[int] = [
version: List[int] = [
spec_.version
for spec_ in registry.values()
if spec_.namespace == ns and spec_.name == name and spec_.version is not None
Expand Down Expand Up @@ -302,39 +298,39 @@ def load_env_plugins(entry_point: str = "gym.envs") -> None:


@overload
def make(id: Literal["CartPole-v1"], **kwargs) -> Env[np.ndarray, np.ndarray | int]: ...
def make(id: Literal["CartPole-v0", "CartPole-v1"], **kwargs) -> Env[np.ndarray, Union[np.ndarray, int]]: ...
@overload
def make(id: Literal["MountainCar-v0"], **kwargs) -> Env[np.ndarray, np.ndarray | int]: ...
def make(id: Literal["MountainCar-v0"], **kwargs) -> Env[np.ndarray, Union[np.ndarray, int]]: ...
@overload
def make(id: Literal["MountainCarContinuous-v0"], **kwargs) -> Env[np.ndarray, np.ndarray | Sequence[SupportsFloat]]: ...
def make(id: Literal["MountainCarContinuous-v0"], **kwargs) -> Env[np.ndarray, Union[np.ndarray, Sequence[SupportsFloat]]]: ...
@overload
def make(id: Literal["Pendulum-v1"], **kwargs) -> Env[np.ndarray, np.ndarray | Sequence[SupportsFloat]]: ...
def make(id: Literal["Pendulum-v1"], **kwargs) -> Env[np.ndarray, Union[np.ndarray, Sequence[SupportsFloat]]]: ...
@overload
def make(id: Literal["Acrobot-v1"], **kwargs) -> Env[np.ndarray, np.ndarray | int]: ...
def make(id: Literal["Acrobot-v1"], **kwargs) -> Env[np.ndarray, Union[np.ndarray, int]]: ...

# Box2d
# ----------------------------------------


@overload
def make(id: Literal["LunarLander-v2", "LunarLanderContinuous-v2"], **kwargs) -> Env[np.ndarray, np.ndarray | int]: ...
def make(id: Literal["LunarLander-v2", "LunarLanderContinuous-v2"], **kwargs) -> Env[np.ndarray, Union[np.ndarray, int]]: ...
@overload
def make(id: Literal["BipedalWalker-v3", "BipedalWalkerHardcore-v3"], **kwargs) -> Env[np.ndarray, np.ndarray | Sequence[SupportsFloat]]: ...
def make(id: Literal["BipedalWalker-v3", "BipedalWalkerHardcore-v3"], **kwargs) -> Env[np.ndarray, Union[np.ndarray, Sequence[SupportsFloat]]]: ...
@overload
def make(id: Literal["CarRacing-v1", "CarRacingDomainRandomize-v1"], **kwargs) -> Env[np.ndarray, np.ndarray | Sequence[SupportsFloat]]: ...
def make(id: Literal["CarRacing-v1", "CarRacingDomainRandomize-v1"], **kwargs) -> Env[np.ndarray, Union[np.ndarray, Sequence[SupportsFloat]]]: ...

# Toy Text
# ----------------------------------------


@overload
def make(id: Literal["Blackjack-v1"], **kwargs) -> Env[np.ndarray, np.ndarray | int]: ...
def make(id: Literal["Blackjack-v1"], **kwargs) -> Env[np.ndarray, Union[np.ndarray, int]]: ...
@overload
def make(id: Literal["FrozenLake-v1", "FrozenLake8x8-v1"], **kwargs) -> Env[np.ndarray, np.ndarray | int]: ...
def make(id: Literal["FrozenLake-v1", "FrozenLake8x8-v1"], **kwargs) -> Env[np.ndarray, Union[np.ndarray, int]]: ...
@overload
def make(id: Literal["CliffWalking-v0"], **kwargs) -> Env[np.ndarray, np.ndarray | int]: ...
def make(id: Literal["CliffWalking-v0"], **kwargs) -> Env[np.ndarray, Union[np.ndarray, int]]: ...
@overload
def make(id: Literal["Taxi-v3"], **kwargs) -> Env[np.ndarray, np.ndarray | int]: ...
def make(id: Literal["Taxi-v3"], **kwargs) -> Env[np.ndarray, Union[np.ndarray, int]]: ...

# Mujoco
# ----------------------------------------
Expand Down Expand Up @@ -415,7 +411,7 @@ def env_specs(self):


# Global registry of environments. Meant to be accessed through `register` and `make`
registry: dict[str, EnvSpec] = EnvRegistry()
registry: Dict[str, EnvSpec] = EnvRegistry()
current_namespace: Optional[str] = None


Expand Down Expand Up @@ -522,7 +518,7 @@ def register(id: str, **kwargs):


def make(
id: str | EnvSpec,
id: Union[str, EnvSpec],
max_episode_steps: Optional[int] = None,
autoreset: bool = False,
disable_env_checker: bool = False,
Expand Down
6 changes: 2 additions & 4 deletions gym/envs/toy_text/frozen_lake.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
from __future__ import annotations

from contextlib import closing
from io import StringIO
from os import path
from typing import Optional
from typing import List, Optional

import numpy as np

Expand Down Expand Up @@ -31,7 +29,7 @@
}


def generate_random_map(size: int = 8, p: float = 0.8) -> list[str]:
def generate_random_map(size: int = 8, p: float = 0.8) -> List[str]:
"""Generates a random valid map (one that has a path from start to goal)
Args:
Expand Down
16 changes: 7 additions & 9 deletions gym/spaces/box.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
"""Implementation of a space that represents closed boxes in euclidean space."""
from __future__ import annotations

from typing import Optional, Sequence, SupportsFloat, Union
from typing import List, Optional, Sequence, SupportsFloat, Tuple, Type, Union

import numpy as np

Expand Down Expand Up @@ -52,8 +50,8 @@ def __init__(
low: Union[SupportsFloat, np.ndarray],
high: Union[SupportsFloat, np.ndarray],
shape: Optional[Sequence[int]] = None,
dtype: type = np.float32,
seed: Optional[int | seeding.RandomNumberGenerator] = None,
dtype: Type = np.float32,
seed: Optional[Union[int, seeding.RandomNumberGenerator]] = None,
):
r"""Constructor of :class:`Box`.
Expand Down Expand Up @@ -105,7 +103,7 @@ def __init__(
assert isinstance(high, np.ndarray)
assert high.shape == shape, "high.shape doesn't match provided shape"

self._shape: tuple[int, ...] = shape
self._shape: Tuple[int, ...] = shape

low_precision = get_precision(low.dtype)
high_precision = get_precision(high.dtype)
Expand All @@ -121,7 +119,7 @@ def __init__(
super().__init__(self.shape, self.dtype, seed)

@property
def shape(self) -> tuple[int, ...]:
def shape(self) -> Tuple[int, ...]:
"""Has stricter type than gym.Space - never None."""
return self._shape

Expand Down Expand Up @@ -210,7 +208,7 @@ def to_jsonable(self, sample_n):
"""Convert a batch of samples from this space to a JSONable data type."""
return np.array(sample_n).tolist()

def from_jsonable(self, sample_n: Sequence[SupportsFloat]) -> list[np.ndarray]:
def from_jsonable(self, sample_n: Sequence[SupportsFloat]) -> List[np.ndarray]:
"""Convert a JSONable data type to a batch of samples from this space."""
return [np.asarray(sample) for sample in sample_n]

Expand Down Expand Up @@ -278,7 +276,7 @@ def get_precision(dtype) -> SupportsFloat:
def _broadcast(
value: Union[SupportsFloat, np.ndarray],
dtype,
shape: tuple[int, ...],
shape: Tuple[int, ...],
inf_sign: str,
) -> np.ndarray:
"""Handle infinite bounds and broadcast at the same time if needed."""
Expand Down
14 changes: 6 additions & 8 deletions gym/spaces/dict.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
"""Implementation of a space that represents the cartesian product of other spaces as a dictionary."""
from __future__ import annotations

from collections import OrderedDict
from collections.abc import Mapping, Sequence
from typing import Dict as TypingDict
from typing import Optional
from typing import Optional, Union

import numpy as np

Expand Down Expand Up @@ -53,8 +51,8 @@ class Dict(Space[TypingDict[str, Space]], Mapping):

def __init__(
self,
spaces: Optional[dict[str, Space]] = None,
seed: Optional[dict | int | seeding.RandomNumberGenerator] = None,
spaces: Optional[TypingDict[str, Space]] = None,
seed: Optional[Union[dict, int, seeding.RandomNumberGenerator]] = None,
**spaces_kwargs: Space,
):
"""Constructor of :class:`Dict` space.
Expand Down Expand Up @@ -101,7 +99,7 @@ def __init__(
None, None, seed # type: ignore
) # None for shape and dtype, since it'll require special handling

def seed(self, seed: Optional[dict | int] = None) -> list:
def seed(self, seed: Optional[Union[dict, int]] = None) -> list:
"""Seed the PRNG of this space and all subspaces."""
seeds = []
if isinstance(seed, dict):
Expand Down Expand Up @@ -188,9 +186,9 @@ def to_jsonable(self, sample_n: list) -> dict:
for key, space in self.spaces.items()
}

def from_jsonable(self, sample_n: dict[str, list]) -> list:
def from_jsonable(self, sample_n: TypingDict[str, list]) -> list:
"""Convert a JSONable data type to a batch of samples from this space."""
dict_of_list: dict[str, list] = {}
dict_of_list: TypingDict[str, list] = {}
for key, space in self.spaces.items():
dict_of_list[key] = space.from_jsonable(sample_n[key])
ret = []
Expand Down
6 changes: 2 additions & 4 deletions gym/spaces/discrete.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
"""Implementation of a space consisting of finitely many elements."""
from __future__ import annotations

from typing import Optional
from typing import Optional, Union

import numpy as np

Expand All @@ -23,7 +21,7 @@ class Discrete(Space[int]):
def __init__(
self,
n: int,
seed: Optional[int | seeding.RandomNumberGenerator] = None,
seed: Optional[Union[int, seeding.RandomNumberGenerator]] = None,
start: int = 0,
):
r"""Constructor of :class:`Discrete` space.
Expand Down
8 changes: 3 additions & 5 deletions gym/spaces/multi_binary.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
"""Implementation of a space that consists of binary np.ndarrays of a fixed shape."""
from __future__ import annotations

from typing import Optional, Sequence, Union
from typing import Optional, Sequence, Tuple, Union

import numpy as np

Expand Down Expand Up @@ -29,7 +27,7 @@ class MultiBinary(Space[np.ndarray]):
def __init__(
self,
n: Union[np.ndarray, Sequence[int], int],
seed: Optional[int | seeding.RandomNumberGenerator] = None,
seed: Optional[Union[int, seeding.RandomNumberGenerator]] = None,
):
"""Constructor of :class:`MultiBinary` space.
Expand All @@ -49,7 +47,7 @@ def __init__(
super().__init__(input_n, np.int8, seed)

@property
def shape(self) -> tuple[int, ...]:
def shape(self) -> Tuple[int, ...]:
"""Has stricter type than gym.Space - never None."""
return self._shape # type: ignore

Expand Down
Loading

0 comments on commit 0263deb

Please sign in to comment.