Skip to content

Commit ba5f9f7

Browse files
committed
style: apply pre-commit formatting and lint fixes
1 parent d9aa52d commit ba5f9f7

6 files changed

Lines changed: 59 additions & 24 deletions

File tree

‎highway_env/__init__.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ def _register_highway_envs():
185185

186186
_register_pettingzoo_envs()
187187

188+
188189
def _register_pettingzoo_envs() -> None:
189190
"""Register PettingZoo ParallelEnv IDs (skipped silently if pettingzoo not installed).
190191
IDs continue the ``intersection-multi-agent-vN`` series from v2:
@@ -213,4 +214,5 @@ def _register_pettingzoo_envs() -> None:
213214
),
214215
)
215216

217+
216218
_register_highway_envs()

‎highway_env/envs/__init__.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@
3838

3939
# PettingZoo environments — requires highway-env[multi-agent]
4040
try:
41+
from highway_env.envs.common.pettingzoo_env import HighwayParallelEnv # noqa: F401
4142
from highway_env.envs.intersection_pz_env import ( # noqa: F401
4243
ConnectedLaneIntersectionParallelEnv,
4344
IntersectionParallelEnv,
4445
)
45-
from highway_env.envs.common.pettingzoo_env import HighwayParallelEnv # noqa: F401
4646

4747
__all__ += [
4848
"HighwayParallelEnv",

‎highway_env/envs/common/abstract.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
import gymnasium as gym
99
import numpy as np
10-
import warnings
1110
from gymnasium import Wrapper
1211
from gymnasium.utils import RecordConstructorArgs
1312
from gymnasium.wrappers import RecordVideo
@@ -473,6 +472,7 @@ class MultiAgentWrapper(Wrapper, RecordConstructorArgs):
473472
Use the PettingZoo ``ParallelEnv`` interface instead::
474473
from highway_env.envs.common.pettingzoo_env import HighwayParallelEnv
475474
"""
475+
476476
def __init__(self, env):
477477
warnings.warn(
478478
"MultiAgentWrapper is deprecated and will be removed in a future release. "
@@ -484,6 +484,7 @@ def __init__(self, env):
484484
)
485485
Wrapper.__init__(self, env)
486486
RecordConstructorArgs.__init__(self)
487+
487488
def step(self, action): # unchanged
488489
obs, _, _, truncated, info = super().step(action)
489490
reward = info["agents_rewards"]

‎highway_env/envs/common/pettingzoo_env.py‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import gymnasium as gym
77
from pettingzoo.utils.env import ParallelEnv
88

9+
910
if TYPE_CHECKING:
1011
from highway_env.envs.common.abstract import AbstractEnv
1112

@@ -41,7 +42,10 @@ class HighwayParallelEnv(ParallelEnv):
4142
and ``MultiAgentAction`` configured.
4243
"""
4344

44-
metadata: dict = {"render_modes": ["human", "rgb_array"], "name": "highway_parallel_v0"}
45+
metadata: dict = {
46+
"render_modes": ["human", "rgb_array"],
47+
"name": "highway_parallel_v0",
48+
}
4549

4650
def __init__(self, env: AbstractEnv) -> None:
4751
super().__init__()
@@ -182,4 +186,5 @@ def _idle_action(self, agent: str):
182186
return min(1, space.n - 1)
183187
# Continuous: return zeros
184188
import numpy as np
189+
185190
return np.zeros(space.shape, dtype=space.dtype)

‎highway_env/envs/intersection_pz_env.py‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
from __future__ import annotations
22

3+
from highway_env.envs.common.pettingzoo_env import HighwayParallelEnv
34
from highway_env.envs.intersection_env import (
45
ConnectedLaneMultiAgentIntersectionEnv,
56
MultiAgentIntersectionEnv,
67
)
7-
from highway_env.envs.common.pettingzoo_env import HighwayParallelEnv
88

99

1010
class IntersectionParallelEnv(HighwayParallelEnv):
@@ -29,7 +29,9 @@ class IntersectionParallelEnv(HighwayParallelEnv):
2929
"name": "intersection_parallel_v3",
3030
}
3131

32-
def __init__(self, config: dict | None = None, render_mode: str | None = None) -> None:
32+
def __init__(
33+
self, config: dict | None = None, render_mode: str | None = None
34+
) -> None:
3335
base = MultiAgentIntersectionEnv(config=config, render_mode=render_mode)
3436
super().__init__(base)
3537

@@ -48,7 +50,9 @@ class ConnectedLaneIntersectionParallelEnv(HighwayParallelEnv):
4850
"name": "intersection_parallel_v4",
4951
}
5052

51-
def __init__(self, config: dict | None = None, render_mode: str | None = None) -> None:
53+
def __init__(
54+
self, config: dict | None = None, render_mode: str | None = None
55+
) -> None:
5256
base = ConnectedLaneMultiAgentIntersectionEnv(
5357
config=config, render_mode=render_mode
5458
)

‎tests/envs/test_pettingzoo.py‎

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,31 @@
33
Skipped automatically if pettingzoo is not installed
44
(e.g. in base CI without the [multi-agent] extra).
55
"""
6+
67
from __future__ import annotations
78

89
import pytest
910

11+
1012
# Skip entire module if pettingzoo is not available
1113
pytest.importorskip(
1214
"pettingzoo.test",
1315
reason="pettingzoo not installed; run with: pip install highway-env[multi-agent]",
1416
)
15-
from pettingzoo.test import parallel_api_test
17+
from pettingzoo.test import parallel_api_test # noqa: E402
1618

17-
import highway_env # noqa: F401 — triggers env registration
18-
from highway_env.envs.intersection_pz_env import (
19+
import highway_env # noqa: E402,F401 — triggers env registration
20+
from highway_env.envs.intersection_pz_env import ( # noqa: E402
1921
ConnectedLaneIntersectionParallelEnv,
2022
IntersectionParallelEnv,
2123
)
22-
from highway_env.envs.common.pettingzoo_env import HighwayParallelEnv
2324

2425

2526
# -----------------------------------------------------------------------
2627
# Fixtures
2728
# -----------------------------------------------------------------------
2829

30+
2931
@pytest.fixture
3032
def intersection_env():
3133
env = IntersectionParallelEnv(config={"controlled_vehicles": 2, "duration": 10})
@@ -46,6 +48,7 @@ def connected_lane_env():
4648
# PettingZoo API conformance
4749
# -----------------------------------------------------------------------
4850

51+
4952
def test_parallel_api_test_intersection():
5053
"""PettingZoo's built-in API conformance checker must pass."""
5154
env = IntersectionParallelEnv(config={"controlled_vehicles": 2, "duration": 5})
@@ -66,6 +69,7 @@ def test_parallel_api_test_connected_lane():
6669
# Observation / action space correctness
6770
# -----------------------------------------------------------------------
6871

72+
6973
def test_reset_returns_agent_dict(intersection_env):
7074
"""reset() must return dicts keyed by agent name."""
7175
obs, infos = intersection_env.reset(seed=42)
@@ -79,20 +83,26 @@ def test_obs_in_observation_space(intersection_env):
7983
"""Every observation must lie within the declared observation space."""
8084
obs, _ = intersection_env.reset(seed=0)
8185
while intersection_env.agents:
82-
actions = {a: intersection_env.action_space(a).sample() for a in intersection_env.agents}
86+
actions = {
87+
a: intersection_env.action_space(a).sample()
88+
for a in intersection_env.agents
89+
}
8390
obs, rewards, terminations, truncations, infos = intersection_env.step(actions)
8491
for agent, agent_obs in obs.items():
85-
assert intersection_env.observation_space(agent).contains(agent_obs), (
86-
f"{agent}: obs not in observation_space"
87-
)
92+
assert intersection_env.observation_space(agent).contains(
93+
agent_obs
94+
), f"{agent}: obs not in observation_space"
8895

8996

9097
def test_possible_agents_is_stable(intersection_env):
9198
"""possible_agents must not change across the episode."""
9299
intersection_env.reset(seed=1)
93100
initial_possible = list(intersection_env.possible_agents)
94101
while intersection_env.agents:
95-
actions = {a: intersection_env.action_space(a).sample() for a in intersection_env.agents}
102+
actions = {
103+
a: intersection_env.action_space(a).sample()
104+
for a in intersection_env.agents
105+
}
96106
intersection_env.step(actions)
97107
assert intersection_env.possible_agents == initial_possible
98108

@@ -101,19 +111,23 @@ def test_possible_agents_is_stable(intersection_env):
101111
# Per-agent termination semantics
102112
# -----------------------------------------------------------------------
103113

114+
104115
def test_terminated_agents_removed_from_agents(intersection_env):
105116
"""Agents flagged as terminated must NOT appear in self.agents next step."""
106117
intersection_env.reset(seed=2)
107118
while intersection_env.agents:
108119
pre_step_agents = set(intersection_env.agents)
109-
actions = {a: intersection_env.action_space(a).sample() for a in intersection_env.agents}
120+
actions = {
121+
a: intersection_env.action_space(a).sample()
122+
for a in intersection_env.agents
123+
}
110124
_, _, terminations, truncations, _ = intersection_env.step(actions)
111125

112126
for agent in pre_step_agents:
113127
if terminations.get(agent, False) or truncations.get(agent, False):
114-
assert agent not in intersection_env.agents, (
115-
f"{agent} terminated/truncated but still in env.agents"
116-
)
128+
assert (
129+
agent not in intersection_env.agents
130+
), f"{agent} terminated/truncated but still in env.agents"
117131

118132

119133
def test_episode_ends_when_all_agents_done(intersection_env):
@@ -122,7 +136,10 @@ def test_episode_ends_when_all_agents_done(intersection_env):
122136
steps = 0
123137
max_steps = 500
124138
while intersection_env.agents and steps < max_steps:
125-
actions = {a: intersection_env.action_space(a).sample() for a in intersection_env.agents}
139+
actions = {
140+
a: intersection_env.action_space(a).sample()
141+
for a in intersection_env.agents
142+
}
126143
intersection_env.step(actions)
127144
steps += 1
128145
# If we reach here without error, the loop terminated correctly
@@ -133,9 +150,11 @@ def test_episode_ends_when_all_agents_done(intersection_env):
133150
# Seeding / reproducibility
134151
# -----------------------------------------------------------------------
135152

153+
136154
def test_reset_with_same_seed_is_reproducible():
137155
"""Two resets with the same seed must produce identical initial observations."""
138156
import numpy as np
157+
139158
env1 = IntersectionParallelEnv(config={"controlled_vehicles": 2, "duration": 5})
140159
env2 = IntersectionParallelEnv(config={"controlled_vehicles": 2, "duration": 5})
141160

@@ -153,20 +172,24 @@ def test_reset_with_same_seed_is_reproducible():
153172
# Deprecation warning for MultiAgentWrapper
154173
# -----------------------------------------------------------------------
155174

175+
156176
def test_multi_agent_wrapper_deprecation_warning():
157177
"""Using MultiAgentWrapper must raise DeprecationWarning."""
158178
import warnings
179+
159180
import gymnasium as gym
160181

161182
with warnings.catch_warnings(record=True) as caught:
162183
warnings.simplefilter("always")
163184
env = gym.make("intersection-multi-agent-v1")
164185
env.close()
165186

166-
deprecation_warnings = [w for w in caught if issubclass(w.category, DeprecationWarning)]
167-
assert len(deprecation_warnings) > 0, (
168-
"Expected DeprecationWarning from MultiAgentWrapper but none was raised"
169-
)
187+
deprecation_warnings = [
188+
w for w in caught if issubclass(w.category, DeprecationWarning)
189+
]
190+
assert (
191+
len(deprecation_warnings) > 0
192+
), "Expected DeprecationWarning from MultiAgentWrapper but none was raised"
170193
assert any(
171194
"MultiAgentWrapper" in str(w.message) for w in deprecation_warnings
172195
), "Expected a DeprecationWarning mentioning MultiAgentWrapper"

0 commit comments

Comments
 (0)