Skip to content

Commit 44b07a6

Browse files
authored
enable ruff format checks and reformat unconforming files (Metta-AI#267)
### TL;DR Added `ruff format --check` to CI workflow and pre-commit hook, and fixed formatting issues across the codebase. ### What changed? - Added `ruff format --check` command to the GitHub Actions build workflow - Added `ruff format --check` to the pre-commit hook to ensure code is properly formatted before commits - Fixed formatting issues across multiple files ### How to test? 1. Run `ruff format --check .` to verify all files are properly formatted 2. Make a commit to ensure the pre-commit hook correctly checks formatting 3. Push changes to verify the GitHub Actions workflow passes with the new format check ### Why make this change? This change enforces consistent code formatting across the codebase, making it more readable and maintainable. By adding format checking to both the pre-commit hook and CI workflow, we ensure that all code contributions adhere to the same formatting standards, reducing style-related merge conflicts and code review comments.
1 parent 14bf403 commit 44b07a6

File tree

13 files changed

+86
-74
lines changed

13 files changed

+86
-74
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ jobs:
7575
source venv/bin/activate
7676
pip install ruff==0.11.5
7777
ruff check --respect-gitignore .
78+
ruff format --check .
7879
7980
- name: Run tests in /tests
8081
env:

deps/mettagrid/mettagrid/config/room/maze.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class MazePrim(Room):
1313
def __init__(
1414
self, width, height, start_pos, end_pos, branching=0.0, seed=None, border_width=0, border_object="wall"
1515
):
16-
super().__init__(border_width=border_width, border_object=border_object, labels = ["maze"])
16+
super().__init__(border_width=border_width, border_object=border_object, labels=["maze"])
1717
self._rng = random.Random(seed)
1818
self._width = width if width % 2 == 1 else width - 1
1919
self._height = height if height % 2 == 1 else height - 1

deps/mettagrid/mettagrid/config/room/mean_distance.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def __init__(
1919
border_width: int = 0,
2020
border_object: str = "wall",
2121
):
22-
super().__init__(border_width=border_width, border_object=border_object, labels = ["mean_distance"])
22+
super().__init__(border_width=border_width, border_object=border_object, labels=["mean_distance"])
2323
logger = logging.getLogger(__name__)
2424

2525
self._rng = np.random.default_rng(seed)

deps/mettagrid/mettagrid/config/room/navigation/cylinder_world.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def __init__(
2323
border_object: str = "wall",
2424
team: str | None = None,
2525
):
26-
super().__init__(border_width=border_width, border_object=border_object, labels = ["cylinder_world"])
26+
super().__init__(border_width=border_width, border_object=border_object, labels=["cylinder_world"])
2727
self._rng = np.random.default_rng(seed)
2828
width, height = np.random.randint(40, 100), np.random.randint(40, 100)
2929
self._width, self._height = width, height
@@ -33,6 +33,7 @@ def __init__(
3333
self._occ = np.zeros((height, width), dtype=bool)
3434

3535
self.set_size_labels(width, height)
36+
3637
# ------------------------------------------------------------------ #
3738
# Public build
3839
# ------------------------------------------------------------------ #
@@ -62,7 +63,6 @@ def _build_cylinder_world(self) -> np.ndarray:
6263
else:
6364
fails += 1 # try a different size/orientation
6465

65-
6666
# Finally, spawn any requested agents on leftover empty cells
6767
grid = self._place_agents(grid)
6868
return grid

deps/mettagrid/mettagrid/config/room/navigation/terrain_from_numpy.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,14 @@ class TerrainFromNumpy(Room):
5555
"""
5656

5757
def __init__(
58-
self, dir, border_width: int = 0, border_object: str = "wall", num_agents: int = 10,
59-
generators: bool = False, file: str | None = None, team: str | None = None
58+
self,
59+
dir,
60+
border_width: int = 0,
61+
border_object: str = "wall",
62+
num_agents: int = 10,
63+
generators: bool = False,
64+
file: str | None = None,
65+
team: str | None = None,
6066
):
6167
zipped_dir = dir + ".zip"
6268
lock_path = zipped_dir + ".lock"
@@ -75,7 +81,7 @@ def __init__(
7581
self.generators = generators
7682
self.uri = file
7783
self.team = team
78-
super().__init__(border_width=border_width, border_object=border_object, labels = ["terrain"])
84+
super().__init__(border_width=border_width, border_object=border_object, labels=["terrain"])
7985

8086
def get_valid_positions(self, level):
8187
valid_positions = []
@@ -113,7 +119,7 @@ def _build(self):
113119
else:
114120
level[pos] = "agent." + self.team
115121
area = level.shape[0] * level.shape[1]
116-
num_hearts = area // 180 #random.randint(66, 180)
122+
num_hearts = area // 180 # random.randint(66, 180)
117123
# Find valid empty spaces surrounded by empty
118124
valid_positions = self.get_valid_positions(level)
119125

deps/mettagrid/mettagrid/config/room/navigation/varied_terrain.py

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -29,44 +29,44 @@ class VariedTerrain(Room):
2929
STYLE_PARAMETERS = {
3030
"all-sparse": {
3131
"hearts_count": 15,
32-
"large_obstacles": {"size_range": [10, 25], "count": [0,2]},
33-
"small_obstacles": {"size_range": [3, 6], "count": [0,2]},
34-
"crosses": {"count": [0,2]},
35-
"labyrinths": {"count": [0,2]},
36-
"scattered_walls": {"count": [0,2]},
37-
"blocks": {"count": [0,2]},
38-
"clumpiness": [0,2],
32+
"large_obstacles": {"size_range": [10, 25], "count": [0, 2]},
33+
"small_obstacles": {"size_range": [3, 6], "count": [0, 2]},
34+
"crosses": {"count": [0, 2]},
35+
"labyrinths": {"count": [0, 2]},
36+
"scattered_walls": {"count": [0, 2]},
37+
"blocks": {"count": [0, 2]},
38+
"clumpiness": [0, 2],
3939
},
4040
"balanced": {
4141
"hearts_count": 75,
42-
"large_obstacles": {"size_range": [10, 25], "count": [3,7]},
43-
"small_obstacles": {"size_range": [3, 6], "count": [3,7]},
44-
"crosses": {"count": [3,7]},
45-
"labyrinths": {"count": [3,7]},
46-
"scattered_walls": {"count": [3,7]},
47-
"blocks": {"count": [3,7 ]},
48-
"clumpiness": [1,3],
42+
"large_obstacles": {"size_range": [10, 25], "count": [3, 7]},
43+
"small_obstacles": {"size_range": [3, 6], "count": [3, 7]},
44+
"crosses": {"count": [3, 7]},
45+
"labyrinths": {"count": [3, 7]},
46+
"scattered_walls": {"count": [3, 7]},
47+
"blocks": {"count": [3, 7]},
48+
"clumpiness": [1, 3],
4949
},
5050
"sparse-altars-dense-objects": {
5151
"hearts_count": 25,
52-
"large_obstacles": {"size_range": [10, 25], "count": [8,15]},
53-
"small_obstacles": {"size_range": [3, 6], "count": [8,15]},
54-
"crosses": {"count": [7,15]},
55-
"labyrinths": {"count": [6,15]},
56-
"scattered_walls": {"count": [40,60]},
57-
"blocks": {"count": [5,15]},
58-
"clumpiness": [2,6],
52+
"large_obstacles": {"size_range": [10, 25], "count": [8, 15]},
53+
"small_obstacles": {"size_range": [3, 6], "count": [8, 15]},
54+
"crosses": {"count": [7, 15]},
55+
"labyrinths": {"count": [6, 15]},
56+
"scattered_walls": {"count": [40, 60]},
57+
"blocks": {"count": [5, 15]},
58+
"clumpiness": [2, 6],
5959
},
60-
# New style: maze-like with predominant labyrinth features.
60+
# New style: maze-like with predominant labyrinth features.
6161
"maze": {
6262
"hearts_count": 25, # Altars placed after obstacles; keeps the grid sparse for maze corridors.
63-
"large_obstacles": {"size_range": [10, 25], "count": [0,2]}, # Disable large obstacles.
64-
"small_obstacles": {"size_range": [3, 6], "count": [0,2]}, # Disable small obstacles.
65-
"crosses": {"count": [0,2]}, # No cross obstacles.
66-
"labyrinths": {"count": [10,20]}, # Increase labyrinth count to generate more maze segments.
67-
"scattered_walls": {"count": [0,2]}, # Avoid adding extra walls that could break up maze consistency.
68-
"blocks": {"count": [0,2]}, # No rectangular blocks.
69-
"clumpiness": [0,2], # Clumpiness is not necessary when only labyrinths are used.
63+
"large_obstacles": {"size_range": [10, 25], "count": [0, 2]}, # Disable large obstacles.
64+
"small_obstacles": {"size_range": [3, 6], "count": [0, 2]}, # Disable small obstacles.
65+
"crosses": {"count": [0, 2]}, # No cross obstacles.
66+
"labyrinths": {"count": [10, 20]}, # Increase labyrinth count to generate more maze segments.
67+
"scattered_walls": {"count": [0, 2]}, # Avoid adding extra walls that could break up maze consistency.
68+
"blocks": {"count": [0, 2]}, # No rectangular blocks.
69+
"clumpiness": [0, 2], # Clumpiness is not necessary when only labyrinths are used.
7070
},
7171
}
7272

@@ -80,9 +80,9 @@ def __init__(
8080
border_object: str = "wall",
8181
occupancy_threshold: float = 0.66, # maximum fraction of grid cells to occupy
8282
style: str = "balanced",
83-
team: str| None = None,
83+
team: str | None = None,
8484
):
85-
super().__init__(border_width=border_width, border_object=border_object, labels = [style])
85+
super().__init__(border_width=border_width, border_object=border_object, labels=[style])
8686

8787
width = np.random.randint(40, 100)
8888
height = np.random.randint(40, 100)

deps/mettagrid/mettagrid/config/room/random.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def __init__(
1717
border_width: int = 0,
1818
border_object: str = "wall",
1919
):
20-
super().__init__(border_width=border_width, border_object=border_object, labels = ["random"])
20+
super().__init__(border_width=border_width, border_object=border_object, labels=["random"])
2121
self._rng = np.random.default_rng(seed)
2222
assert isinstance(width, int), f"width must be an int, got '{width}'"
2323
assert isinstance(height, int), f"height must be an int, got {type(height)}"

deps/mettagrid/mettagrid/config/room/room_list.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ def _build(self):
3131
common_labels = set.intersection(*[set(labels) for labels in room_labels])
3232
self.labels = list(common_labels)
3333

34-
3534
# Determine grid dimensions based on number of rooms
3635
n_rooms = len(rooms)
3736

deps/mettagrid/mettagrid/mettagrid_env.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,6 @@ def process_episode_stats(self, infos: Dict[str, Any]):
128128
for n, v in infos["agent"].items():
129129
infos["agent"][n] = v / self._num_agents
130130

131-
132131
@property
133132
def _max_steps(self):
134133
return self._env_cfg.game.max_steps
@@ -203,5 +202,6 @@ def inventory_item_names(self):
203202
def close(self):
204203
pass
205204

205+
206206
# Ensure resolvers are registered when this module is imported
207207
register_resolvers()

devops/git-hooks/pre-commit

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,11 @@ if ! echo "$STAGED_FILES" | xargs ruff check; then
2121
exit 1
2222
fi
2323

24+
# Run ruff format on staged files
25+
if ! echo "$STAGED_FILES" | xargs ruff format --check; then
26+
echo "❌ Ruff format failed. Please fix the issues before committing."
27+
exit 1
28+
fi
29+
2430
# No output on success - GitHub Desktop works better with silent success
25-
exit 0
31+
exit 0

0 commit comments

Comments
 (0)