From e5cf021f6f39948c17f309c9c07411f4617fc4d3 Mon Sep 17 00:00:00 2001 From: Keith Smiley Date: Mon, 2 Dec 2024 10:04:29 -0800 Subject: [PATCH] Format python with ruff Ruff is a faster and more modern python formatter. This swaps out using `mojo format` for python with ruff. Signed-off-by: Keith Smiley --- .pre-commit-config.yaml | 8 +++++++- examples/check_mod.py | 2 +- examples/lit.cfg.py | 8 +++++--- examples/pymatmul.py | 2 +- examples/simple_interop.py | 2 +- stdlib/benchmarks/lit.cfg.py | 2 ++ stdlib/test/lit.cfg.py | 6 +++--- 7 files changed, 20 insertions(+), 10 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 9ca3f26be67..1de73c9adf7 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -5,7 +5,7 @@ repos: name: mojo-format entry: mojo format language: system - files: '\.(mojo|🔥|py)$' + files: '\.(🔥|py)$' stages: [commit] - id: check-docstrings name: check-docstrings @@ -24,3 +24,9 @@ repos: hooks: - id: markdownlint args: ['--config', 'stdlib/scripts/.markdownlint.yaml', '--fix'] + - repo: https://github.com/astral-sh/ruff-pre-commit + rev: v0.8.0 + hooks: + - id: ruff + args: [--fix] + - id: ruff-format diff --git a/examples/check_mod.py b/examples/check_mod.py index 26e99dc419b..21a676d7086 100644 --- a/examples/check_mod.py +++ b/examples/check_mod.py @@ -37,5 +37,5 @@ def install_if_missing(name: str): raise ImportError("python not on path" + FIX) subprocess.check_call([python, "-m", "pip", "install", name]) return - except: + except subprocess.CalledProcessError: raise ImportError(f"{name} not found" + FIX) diff --git a/examples/lit.cfg.py b/examples/lit.cfg.py index 1c3516870d3..4795ce09e27 100644 --- a/examples/lit.cfg.py +++ b/examples/lit.cfg.py @@ -11,6 +11,8 @@ # limitations under the License. # ===----------------------------------------------------------------------=== # +# ruff: noqa + import os from pathlib import Path @@ -55,9 +57,9 @@ / "mojo", ) -os.environ[ - "MODULAR_MOJO_NIGHTLY_IMPORT_PATH" -] = f"{build_root},{pre_built_packages_path}" +os.environ["MODULAR_MOJO_NIGHTLY_IMPORT_PATH"] = ( + f"{build_root},{pre_built_packages_path}" +) # Pass through several environment variables # to the underlying subprocesses that run the tests. diff --git a/examples/pymatmul.py b/examples/pymatmul.py index 5116cc289cb..ab47f0c9420 100644 --- a/examples/pymatmul.py +++ b/examples/pymatmul.py @@ -17,7 +17,7 @@ import check_mod check_mod.install_if_missing("numpy") -import numpy as np +import numpy as np # noqa class PyMatrix: diff --git a/examples/simple_interop.py b/examples/simple_interop.py index e0124546178..c7c29694f06 100644 --- a/examples/simple_interop.py +++ b/examples/simple_interop.py @@ -18,7 +18,7 @@ import check_mod check_mod.install_if_missing("numpy") -import numpy as np +import numpy as np # noqa def test_interop_func(): diff --git a/stdlib/benchmarks/lit.cfg.py b/stdlib/benchmarks/lit.cfg.py index a06446ab341..834c5f62c61 100644 --- a/stdlib/benchmarks/lit.cfg.py +++ b/stdlib/benchmarks/lit.cfg.py @@ -11,6 +11,8 @@ # limitations under the License. # ===----------------------------------------------------------------------=== # +# ruff: noqa + import os from pathlib import Path diff --git a/stdlib/test/lit.cfg.py b/stdlib/test/lit.cfg.py index 55976ecfe6c..f67a1aeb129 100644 --- a/stdlib/test/lit.cfg.py +++ b/stdlib/test/lit.cfg.py @@ -11,6 +11,8 @@ # limitations under the License. # ===----------------------------------------------------------------------=== # +# ruff: noqa + import os import shutil from pathlib import Path @@ -48,9 +50,7 @@ def has_not(): config.test_source_root = os.path.dirname(__file__) # test_exec_root: The root path where tests should be run. - config.test_exec_root = os.path.join( - config.modular_obj_root, "mojo-stdlib", "test" - ) + config.test_exec_root = os.path.join(config.modular_obj_root, "mojo-stdlib", "test") # External, public Mojo testing configuration else: # test_source_root: The root path where tests are located.