Skip to content

Commit

Permalink
[BE Week] Add Ruff pre-commit hook (#2156)
Browse files Browse the repository at this point in the history
* Add ruff pre-commit hook

* Add Ruff PERF checks

* Ignore notebooks for now

* Remove autoflake
  • Loading branch information
Skylion007 authored Jul 17, 2023
1 parent 5dd94a9 commit d4daec4
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 14 deletions.
11 changes: 6 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,13 @@ repos:
- id: black
exclude: ^examples/tutorials/(nb_python|colabs)

- repo: https://github.com/myint/autoflake
rev: v1.4
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.0.277
hooks:
- id: autoflake
args: ['--expand-star-imports', '--ignore-init-module-imports', '--in-place']
exclude: docs/
- id: ruff
args: [--fix]
exclude: ^examples/tutorials/(nb_python|colabs)

- repo: https://github.com/pycqa/flake8
rev: 5.0.4
Expand Down
2 changes: 1 addition & 1 deletion examples/demo_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ def benchmark(self, settings, group_id=ABTestGroup.CONTROL):
) as pool:
perfs = pool.map(self._bench_target, range(nprocs))

res = {k: [] for k in perfs[0].keys()}
res = {k: [] for k in perfs[0]}
for p in perfs:
for k, v in p.items():
res[k] += [v]
Expand Down
2 changes: 1 addition & 1 deletion examples/instance_segmentation/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def visual_filter(masks, labels, segment_type):
target["image_id"].item(): images[i]
for i, target in enumerate(targets)
}
for image_id in res.keys():
for image_id in res:
masks = res[image_id]["masks"].numpy() >= 0.5
labels = res[image_id]["labels"].numpy()
visual = visual_filter(masks, labels, segment_type)
Expand Down
43 changes: 43 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,46 @@ skip_glob = ["*/deps/*", "*/build/*", "*/obselete/*"]
known_first_party = ["habitat_sim"]
profile = 'black'
treat_comments_as_code = "# %%"

[tool.ruff]
exclude = [
".git",
"__pycache__",
"build",
"data",
"dist",
"docs",
"src/deps",
"tools/run-clang-tidy.py",
]
ignore = [
"A003",
"B018",
"B9",
"C401",
"C402",
"C408",
"RET504",
"RET505",
"SIM105",
"PERF2",
"PERF4",
]
line-length = 88
select = [
"A",
"B",
"C4",
"F",
"RET",
"SIM",
"W",
"PERF",
]

[tool.ruff.per-file-ignores]
"*/__init__.py" = ["F401"]
"examples/tutorials/nb_python/*.py" = [
"B008",
"F841",
]
4 changes: 2 additions & 2 deletions src_python/habitat_sim/agent/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ def act(self, action_id: Any) -> bool:
self.scene_node, action.name, action.actuation, apply_filter=True
)
else:
for _, v in self._sensors.items():
for v in self._sensors.values():
habitat_sim.errors.assert_obj_valid(v)
self.controls.action(
v.object, action.name, action.actuation, apply_filter=False
Expand Down Expand Up @@ -255,7 +255,7 @@ def set_state(
self.body.object.rotation = quat_to_magnum(state.rotation)

if reset_sensors:
for _, v in self._sensors.items():
for v in self._sensors.values():
v.set_transformation_from_spec()

if not infer_sensor_states:
Expand Down
6 changes: 3 additions & 3 deletions tests/test_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ def test_reconfigure():
agent = habitat_sim.Agent(scene_graph.get_root_node().create_child())

habitat_sim.errors.assert_obj_valid(agent.body)
for _, v in agent._sensors.items():
for v in agent._sensors.values():
habitat_sim.errors.assert_obj_valid(v)

agent.reconfigure(agent.agent_config)
for _, v in agent._sensors.items():
for v in agent._sensors.values():
habitat_sim.errors.assert_obj_valid(v)

agent.reconfigure(agent.agent_config, True)
for _, v in agent._sensors.items():
for v in agent._sensors.values():
habitat_sim.errors.assert_obj_valid(v)


Expand Down
2 changes: 1 addition & 1 deletion tools/create_basis_compressed_glbs.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def _gltf2unlit(gltf_name: str):

# Drop everything except base color and base color texture
pbrMetallicRoughness = material["pbrMetallicRoughness"]
for key in pbrMetallicRoughness.keys():
for key in pbrMetallicRoughness:
if key not in ["baseColorFactor", "baseColorTexture"]:
del pbrMetallicRoughness[key]
for key in [
Expand Down
2 changes: 1 addition & 1 deletion tools/npz2scn.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

# Convert ndarrays to python lists so that we can serialize.
def listify(entry: Dict[str, Any]) -> None:
for key in entry.keys():
for key in entry:
if type(entry[key]) is np.ndarray:
entry[key] = entry[key].tolist()

Expand Down

0 comments on commit d4daec4

Please sign in to comment.