internlm/SWE-Fixer-Train-110K huggingface dataset:
Repository: openai/gym

Issue:
Bug in PixelObservationWrapper
In the pixel_observation.py, we have a bit of code that looks like this: 

```
    def _add_pixel_observation(self, observation):
        if self._pixels_only:
            observation = collections.OrderedDict()
        elif self._observation_is_dict:
            observation = type(observation)(observation)
        else:
            observation = collections.OrderedDict()
            observation[STATE_KEY] = observation
```

If you note, the argument `observation` is being clobbered in the else case, so now the observation dictionary at the STATE_KEY refers to itself instead of the underlying env's observation. 

I'm happy to fix this and submit a pull request but I wanted to raise the community's attention to this first.  


Discussion:

Resolving patch:
diff --git a/gym/wrappers/pixel_observation.py b/gym/wrappers/pixel_observation.py
--- a/gym/wrappers/pixel_observation.py
+++ b/gym/wrappers/pixel_observation.py
@@ -110,14 +110,14 @@ def observation(self, observation):
         pixel_observation = self._add_pixel_observation(observation)
         return pixel_observation
 
-    def _add_pixel_observation(self, observation):
+    def _add_pixel_observation(self, wrapped_observation):
         if self._pixels_only:
             observation = collections.OrderedDict()
         elif self._observation_is_dict:
-            observation = type(observation)(observation)
+            observation = type(wrapped_observation)(wrapped_observation)
         else:
             observation = collections.OrderedDict()
-            observation[STATE_KEY] = observation
+            observation[STATE_KEY] = wrapped_observation
 
         pixel_observations = {
             pixel_key: self.env.render(**self._render_kwargs[pixel_key])


Test patch:
diff --git a/gym/wrappers/test_pixel_observation.py b/gym/wrappers/test_pixel_observation.py
--- a/gym/wrappers/test_pixel_observation.py
+++ b/gym/wrappers/test_pixel_observation.py
@@ -116,3 +116,6 @@ def test_single_array_observation(self, pixels_only):
 
         assert depth_observation.shape == (32, 32, 3)
         assert depth_observation.dtype == np.uint8
+
+        if not pixels_only:
+            assert isinstance(observation[STATE_KEY], np.ndarray)
Repository: openai/gym

Issue:
Base Env class should be abstract
Hi, thank you for this lib,
The Env class should be abstract but it is not. Instead its methods have a `raise NotImplementedError` body.
There are advantages to making this abstract: for example PyLance can properly analyze gym code.

I think Env should subclass `ABC` and methods should be annotated as `@abstractmethod`.


Discussion:

Resolving patch:
diff --git a/gym/core.py b/gym/core.py
--- a/gym/core.py
+++ b/gym/core.py
@@ -1,3 +1,5 @@
+from abc import abstractmethod
+
 import gym
 from gym import error
 from gym.utils import closer
@@ -38,6 +40,7 @@ class Env(object):
     action_space = None
     observation_space = None
 
+    @abstractmethod
     def step(self, action):
         """Run one timestep of the environment's dynamics. When end of
         episode is reached, you are responsible for calling `reset()`
@@ -56,6 +59,7 @@ def step(self, action):
         """
         raise NotImplementedError
 
+    @abstractmethod
     def reset(self):
         """Resets the environment to an initial state and returns an initial
         observation.
@@ -71,7 +75,8 @@ def reset(self):
         """
         raise NotImplementedError
 
-    def render(self, mode="human"):
+    @abstractmethod
+    def render(self, mode='human'):
         """Renders the environment.
 
         The set of supported modes varies per environment. (And some
@@ -184,6 +189,7 @@ def reset(self):
                     )
                 )
 
+    @abstractmethod
     def compute_reward(self, achieved_goal, desired_goal, info):
         """Compute the step reward. This externalizes the reward function and makes
         it dependent on a desired goal and the one that was achieved. If you wish to include
@@ -278,6 +284,7 @@ def step(self, action):
         observation, reward, done, info = self.env.step(action)
         return self.observation(observation), reward, done, info
 
+    @abstractmethod
     def observation(self, observation):
         raise NotImplementedError
 
@@ -290,6 +297,7 @@ def step(self, action):
         observation, reward, done, info = self.env.step(action)
         return observation, self.reward(reward), done, info
 
+    @abstractmethod
     def reward(self, reward):
         raise NotImplementedError
 
@@ -301,8 +309,10 @@ def reset(self, **kwargs):
     def step(self, action):
         return self.env.step(self.action(action))
 
+    @abstractmethod
     def action(self, action):
         raise NotImplementedError
 
+    @abstractmethod
     def reverse_action(self, action):
         raise NotImplementedError


Test patch:
Repository: openai/gym

Issue:
close the env when finished
https://github.com/openai/gym/blob/345c65973fc7160d8be374745a60c36869d8accc/gym/envs/box2d/lunar_lander.py#L449

Shall we add `env.close()` before returning here?
I've seen error below if it's not closed.
`ImportError: sys.meta_path is None, Python is likely shutting down`.


Discussion:
Technically yes, I would entertain a PR to add that.

Resolving patch:
diff --git a/gym/envs/box2d/lunar_lander.py b/gym/envs/box2d/lunar_lander.py
--- a/gym/envs/box2d/lunar_lander.py
+++ b/gym/envs/box2d/lunar_lander.py
@@ -446,6 +446,8 @@ def demo_heuristic_lander(env, seed=None, render=False):
             print("step {} total_reward {:+0.2f}".format(steps, total_reward))
         steps += 1
         if done: break
+    if render:
+        env.close()
     return total_reward
 
 


Test patch:
Repository: openai/gym

Issue:
Monitor wrapper used 2 different directory paths when it starts
When an environment is started with the `Monitor` wrapper in the `_start` method the directory is set with `directory = os.path.abspath(directory)`. In some cases this leads to 2 different directory paths, so `StatsRecorder` files end up in one and `VideoRecorder` files in the other.

Setting `self.directory = os.path.abspath(directory)` at the very beginning and using `self.directory` everywhere solves the problem.


Discussion:

Resolving patch:
diff --git a/gym/wrappers/monitor.py b/gym/wrappers/monitor.py
--- a/gym/wrappers/monitor.py
+++ b/gym/wrappers/monitor.py
@@ -88,9 +88,11 @@ def _start(
         else:
             env_id = self.env.spec.id
 
-        if not os.path.exists(directory):
-            logger.info("Creating monitor directory %s", directory)
-            os.makedirs(directory, exist_ok=True)
+        self.directory = os.path.abspath(directory)
+
+        if not os.path.exists(self.directory):
+            logger.info("Creating monitor directory %s", self.directory)
+            os.makedirs(self.directory, exist_ok=True)
 
         if video_callable is None:
             video_callable = capped_cubic_video_schedule
@@ -106,13 +108,12 @@ def _start(
 
         # Check on whether we need to clear anything
         if force:
-            clear_monitor_files(directory)
+            clear_monitor_files(self.directory)
         elif not resume:
-            training_manifests = detect_training_manifests(directory)
+            training_manifests = detect_training_manifests(self.directory)
             if len(training_manifests) > 0:
                 raise error.Error(
                     """Trying to write to monitor directory {} with existing monitor files: {}.
-
  You should use a unique directory for each training run, or use 'force=True' to automatically clear previous monitor files.""".format(
                         directory, ", ".join(training_manifests[:5])
                     )
@@ -121,21 +122,18 @@ def _start(
         self._monitor_id = monitor_closer.register(self)
 
         self.enabled = True
-        self.directory = os.path.abspath(directory)
         # We use the 'openai-gym' prefix to determine if a file is
         # ours
         self.file_prefix = FILE_PREFIX
         self.file_infix = "{}.{}".format(self._monitor_id, uid if uid else os.getpid())
 
         self.stats_recorder = stats_recorder.StatsRecorder(
-            directory,
+            self.directory,
             "{}.episode_batch.{}".format(self.file_prefix, self.file_infix),
             autoreset=self.env_semantics_autoreset,
             env_id=env_id,
         )
 
-        if not os.path.exists(directory):
-            os.mkdir(directory)
         self.write_upon_reset = write_upon_reset
 
         if mode is not None:


Test patch:
Repository: openai/gym

Issue:
[Proposal] Simplify the Box string representation
### Proposal 

Stop the `Box` string representation from printing out all the low/high values if they are all identical

### Motivation

In any reasonably large environment, `print(env.observation_space)` will spam the output with dozens of zero's or infs or whatever else. This information may be relevant if some of these values are different, but often is not necessary, and is just noise. And good luck printing a Dictionary observation space.

I think it'd be enough to change the __repr__ method on Box so that if `len(set(self.low)) == 1`, we just print the unique value, and the same thing for `self.high`.

### Pitch

Make Boxes printable again

### Alternatives

Grin and bear it

### Additional context

![image](https://user-images.githubusercontent.com/19414946/143067427-b2b999ba-e430-458a-81c3-f633db2a7b8f.png)
Help

This is partially going back on #2182 and #2183 , but I wasn't around to yell alarm at the full consequences of that PR. But since it will only simplify the representation when all limits are equal, it doesn't actually cause that same problem.

### Checklist

- [x] I have checked that there is no similar [issue](https://github.com/openai/gym/issues) in the repo (**required**)



Discussion:
I mean the linked fix was less bad than before it. I'm fine printing max/max only if both are uniform, that's definitely more readable, though a print statement clarifying that only single values are being displayed is needed to accompany a behavior like that.

Resolving patch:
diff --git a/gym/spaces/box.py b/gym/spaces/box.py
--- a/gym/spaces/box.py
+++ b/gym/spaces/box.py
@@ -4,6 +4,17 @@
 from gym import logger
 
 
+def _short_repr(arr):
+    """Create a shortened string representation of a numpy array.
+
+    If arr is a multiple of the all-ones vector, return a string representation of the multiplier.
+    Otherwise, return a string representation of the entire array.
+    """
+    if arr.size != 0 and np.min(arr) == np.max(arr):
+        return str(np.min(arr))
+    return str(arr)
+
+
 class Box(Space):
     """
     A (possibly unbounded) box in R^n. Specifically, a Box represents the
@@ -74,6 +85,9 @@ def _get_precision(dtype):
         self.low = self.low.astype(self.dtype)
         self.high = self.high.astype(self.dtype)
 
+        self.low_repr = _short_repr(self.low)
+        self.high_repr = _short_repr(self.high)
+
         # Boolean arrays which indicate the interval type for each coordinate
         self.bounded_below = -np.inf < self.low
         self.bounded_above = np.inf > self.high
@@ -154,7 +168,7 @@ def from_jsonable(self, sample_n):
         return [np.asarray(sample) for sample in sample_n]
 
     def __repr__(self):
-        return f"Box({self.low}, {self.high}, {self.shape}, {self.dtype})"
+        return f"Box({self.low_repr}, {self.high_repr}, {self.shape}, {self.dtype})"
 
     def __eq__(self, other):
         return (


Test patch:
Repository: openai/gym

Issue:
[Proposal] Add license to the license field in the setup.py file
### Proposal 

Add license to the license field in the setup.py file.

### Motivation

The license field is defined but not completed in the setup.py file. 

Some package dependency scanners will block the usage of this package as the license is not specified.

### Checklist

- [x] I have checked that there is no similar [issue](https://github.com/openai/gym/issues) in the repo (**required**)



Discussion:
We'd just need to specify the MIT license flag in setup.py right? if that's the case, could you please do a tiny PR for this?

Resolving patch:
diff --git a/setup.py b/setup.py
--- a/setup.py
+++ b/setup.py
@@ -42,7 +42,7 @@
     url="https://github.com/openai/gym",
     author="Gym Community",
     author_email="jkterry@umd.edu",
-    license="",
+    license="MIT",
     packages=[package for package in find_packages() if package.startswith("gym")],
     zip_safe=False,
     install_requires=[


Test patch:
Repository: openai/gym

Issue:
[Bug Report] FrozenLake RGB rendering returns None
FrozenLake `render()` method in mode human always returns `None` and lacks an `rgb_array` render mode. For reference, Blackjack has recently been converted to pygame and implements these features correctly.

@younik I believe you converted FrozenLake to pygame? 



Discussion:

Resolving patch:
diff --git a/gym/envs/toy_text/frozen_lake.py b/gym/envs/toy_text/frozen_lake.py
--- a/gym/envs/toy_text/frozen_lake.py
+++ b/gym/envs/toy_text/frozen_lake.py
@@ -230,10 +230,10 @@ def reset(
 
     def render(self, mode="human"):
         desc = self.desc.tolist()
-        if mode == "ansi":
-            return self._render_text(desc)
-        else:
+        if mode == "human":
             return self._render_gui(desc)
+        else:
+            return self._render_text(desc)
 
     def _render_gui(self, desc):
         if self.window_surface is None:


Test patch:
Repository: openai/gym

Issue:
[Bug Report] FrozenLake RGB rendering returns None
FrozenLake `render()` method in mode human always returns `None` and lacks an `rgb_array` render mode. For reference, Blackjack has recently been converted to pygame and implements these features correctly.

@younik I believe you converted FrozenLake to pygame? 



Discussion:

Resolving patch:
diff --git a/gym/envs/toy_text/frozen_lake.py b/gym/envs/toy_text/frozen_lake.py
--- a/gym/envs/toy_text/frozen_lake.py
+++ b/gym/envs/toy_text/frozen_lake.py
@@ -133,7 +133,7 @@ class FrozenLakeEnv(Env):
     * v0: Initial versions release (1.0.0)
     """
 
-    metadata = {"render.modes": ["human", "ansi"]}
+    metadata = {"render.modes": ["human", "ansi", "rgb_array"]}
 
     def __init__(self, desc=None, map_name="4x4", is_slippery=True):
         if desc is None and map_name is None:
@@ -230,16 +230,19 @@ def reset(
 
     def render(self, mode="human"):
         desc = self.desc.tolist()
-        if mode == "human":
-            return self._render_gui(desc)
-        else:
+        if mode == "ansi":
             return self._render_text(desc)
+        else:
+            return self._render_gui(desc, mode)
 
-    def _render_gui(self, desc):
+    def _render_gui(self, desc, mode):
         if self.window_surface is None:
             pygame.init()
             pygame.display.set_caption("Frozen Lake")
-            self.window_surface = pygame.display.set_mode(self.window_size)
+            if mode == "human":
+                self.window_surface = pygame.display.set_mode(self.window_size)
+            else:  # rgb_array
+                self.window_surface = pygame.Surface(self.window_size)
         if self.hole_img is None:
             file_name = path.join(path.dirname(__file__), "img/hole.png")
             self.hole_img = pygame.image.load(file_name)
@@ -324,7 +327,12 @@ def _render_gui(self, desc):
             self.window_surface.blit(elf_img, elf_rect)
 
         self.window_surface.blit(board, board.get_rect())
-        pygame.display.update()
+        if mode == "human":
+            pygame.display.update()
+        else:  # rgb_array
+            return np.transpose(
+                np.array(pygame.surfarray.pixels3d(self.window_surface)), axes=(1, 0, 2)
+            )
 
     @staticmethod
     def _center_small_rect(big_rect, small_dims):


Test patch:
Repository: openai/gym

Issue:
[Bug Report] Empty print version warning 
**Describe the bug**
When I import gym, there's an empty line printed.
It's because of this line: https://github.com/openai/gym/blob/master/gym/__init__.py#L30

Either it's a bug, because `notice` shouldn't be an empty string, or the check should be `if notice:` which is false for both `None` and `""` (empty string).

Currently it's cluttering the logs at best, or masking some other issue.

**Code example**
```python
import gym
```

**System Info**
Describe the characteristic of your environment:
Latest gym installed from pip, Ubuntu 20.04, Python 3.9.7


### Checklist

- [x] I have checked that there is no similar [issue](https://github.com/openai/gym/issues) in the repo (**required**)



Discussion:

Resolving patch:
diff --git a/gym/__init__.py b/gym/__init__.py
--- a/gym/__init__.py
+++ b/gym/__init__.py
@@ -26,7 +26,7 @@
 
     # print version warning if necessary
     notice = notices.notices.get(__version__)
-    if notice is not None:
+    if notice:
         print(notice, file=sys.stderr)
 
 except Exception:  # nosec


Test patch:
Repository: openai/gym

Issue:
py.typed not bundled in release
The latest pypi package [gym==0.23.0](https://pypi.org/project/gym/0.23.0/) does not include `py.typed`, resulting in failed `mypy` checks.

Reproduce by `pip install gym` and noting the missing file or downloading the zip from pypi (zip on GH contains the file).


Discussion:
We use `pyright` for type checking. Can you elaborate on what `py.typed` should contain, and what benefits there would be to including it?

Resolving patch:
diff --git a/setup.py b/setup.py
--- a/setup.py
+++ b/setup.py
@@ -58,6 +58,7 @@
             "envs/classic_control/assets/*.png",
             "envs/toy_text/font/*.ttf",
             "envs/toy_text/img/*.png",
+            "py.typed",
         ]
     },
     tests_require=["pytest", "mock"],


Test patch:
Repository: openai/gym

Issue:
[Bug Report] Taxi env.render(mode='rgb_array') broken
```python
import gym
env = gym.make("Taxi-v3")
env.reset()
env.render(mode='rgb_array')
```
yields

```
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/anaconda3/envs/vid/lib/python3.9/site-packages/gym/core.py", line 286, in render
    return self.env.render(**kwargs)
  File "/opt/anaconda3/envs/vid/lib/python3.9/site-packages/gym/core.py", line 286, in render
    return self.env.render(**kwargs)
  File "/opt/anaconda3/envs/vid/lib/python3.9/site-packages/gym/envs/toy_text/taxi.py", line 234, in render
    return self._render_gui(mode)
  File "/opt/anaconda3/envs/vid/lib/python3.9/site-packages/gym/envs/toy_text/taxi.py", line 269, in _render_gui
    self.destination_img = self.destination_img.convert_alpha()
pygame.error: No video mode has been set
```

@younik 

[Bug Report] Taxi env.render(mode='rgb_array') broken
```python
import gym
env = gym.make("Taxi-v3")
env.reset()
env.render(mode='rgb_array')
```
yields

```
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/anaconda3/envs/vid/lib/python3.9/site-packages/gym/core.py", line 286, in render
    return self.env.render(**kwargs)
  File "/opt/anaconda3/envs/vid/lib/python3.9/site-packages/gym/core.py", line 286, in render
    return self.env.render(**kwargs)
  File "/opt/anaconda3/envs/vid/lib/python3.9/site-packages/gym/envs/toy_text/taxi.py", line 234, in render
    return self._render_gui(mode)
  File "/opt/anaconda3/envs/vid/lib/python3.9/site-packages/gym/envs/toy_text/taxi.py", line 269, in _render_gui
    self.destination_img = self.destination_img.convert_alpha()
pygame.error: No video mode has been set
```

@younik 



Discussion:

Resolving patch:
diff --git a/gym/envs/toy_text/taxi.py b/gym/envs/toy_text/taxi.py
--- a/gym/envs/toy_text/taxi.py
+++ b/gym/envs/toy_text/taxi.py
@@ -241,7 +241,7 @@ def _render_gui(self, mode):
             pygame.display.set_caption("Taxi")
             if mode == "human":
                 self.window = pygame.display.set_mode(WINDOW_SIZE)
-            else:  # rgb_array
+            else:  # "rgb_array"
                 self.window = pygame.Surface(WINDOW_SIZE)
         if self.clock is None:
             self.clock = pygame.time.Clock()
@@ -266,7 +266,6 @@ def _render_gui(self, mode):
             self.destination_img = pygame.transform.scale(
                 pygame.image.load(file_name), self.cell_size
             )
-            self.destination_img = self.destination_img.convert_alpha()
             self.destination_img.set_alpha(170)
         if self.median_horiz is None:
             file_names = [


Test patch:
Repository: openai/gym

Issue:
[Bug] turn indicators on car racing not rendering
**Describe the bug**
I have no idea why, but after #2749, the turn indicators in CarRacing no longer render and are only black now.

Edit: Figured it out.

- [x] I have checked that there is no similar [issue](https://github.com/openai/gym/issues) in the repo (**required**)

![image](https://user-images.githubusercontent.com/38184875/163634485-ac7cdb46-a3f8-4164-bc99-ebc077d1472d.png)




Discussion:

Resolving patch:
diff --git a/gym/envs/box2d/car_racing.py b/gym/envs/box2d/car_racing.py
--- a/gym/envs/box2d/car_racing.py
+++ b/gym/envs/box2d/car_racing.py
@@ -391,7 +391,10 @@ def _create_track(self):
                     y2 + side * (TRACK_WIDTH + BORDER) * math.sin(beta2),
                 )
                 self.road_poly.append(
-                    ([b1_l, b1_r, b2_r, b2_l], (1, 1, 1) if i % 2 == 0 else (1, 0, 0))
+                    (
+                        [b1_l, b1_r, b2_r, b2_l],
+                        (255, 255, 255) if i % 2 == 0 else (255, 0, 0),
+                    )
                 )
         self.track = track
         return True


Test patch:
Repository: openai/gym

Issue:
[Question] How to suppress  Custom namespace `ALE` is being overridden by namespace `ALE` warning?
### Question

Installing gym and then ale-py (easiest is to do that in a colab), `import gym` throws a warning:

```
 Custom namespace `ALE` is being overridden by namespace `ALE`
```
how to get rid of it?

(does not seem normal that basic install of gym throws a warning)
gym 0.24
ale-py 0.7.5


Discussion:

Resolving patch:
diff --git a/gym/envs/registration.py b/gym/envs/registration.py
--- a/gym/envs/registration.py
+++ b/gym/envs/registration.py
@@ -417,17 +417,7 @@ def env_specs(self):
 
 def _check_spec_register(spec: EnvSpec):
     """Checks whether the spec is valid to be registered. Helper function for `register`."""
-    global registry, current_namespace
-    if current_namespace is not None:
-        if spec.namespace is not None:
-            logger.warn(
-                f"Custom namespace `{spec.namespace}` is being overridden "
-                f"by namespace `{current_namespace}`. If you are developing a "
-                "plugin you shouldn't specify a namespace in `register` "
-                "calls. The namespace is specified through the "
-                "entry point package metadata."
-            )
-
+    global registry
     latest_versioned_spec = max(
         (
             spec_
@@ -496,7 +486,10 @@ def register(id: str, **kwargs):
     ns, name, version = parse_env_id(id)
 
     if current_namespace is not None:
-        if kwargs.get("namespace") is not None:
+        if (
+            kwargs.get("namespace") is not None
+            and kwargs.get("namespace") != current_namespace
+        ):
             logger.warn(
                 f"Custom namespace `{kwargs.get('namespace')}` is being overridden "
                 f"by namespace `{current_namespace}`. If you are developing a "


Test patch: