From 3d2cfb13b9d4c17bfa15b8e0490b6e0fa1798d49 Mon Sep 17 00:00:00 2001 From: Daniel Hollas Date: Wed, 7 Feb 2024 15:54:34 +0000 Subject: [PATCH] Use ruff instead of flake8, isort, black and pyupgrade (#202) * Use ruff linter and formatter, add more linter rules * Remove other unnecessary hooks --- .github/workflows/ci.yml | 4 ++-- .pre-commit-config.yaml | 47 +++++++------------------------------ aiidalab_launch/__main__.py | 6 ++--- aiidalab_launch/util.py | 13 ++++------ pyproject.toml | 25 ++++++++++++++++++++ setup.cfg | 8 ------- tests/test_cli.py | 4 ++-- 7 files changed, 45 insertions(+), 62 deletions(-) create mode 100644 pyproject.toml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 86d3fd2..59b84c0 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -19,8 +19,8 @@ jobs: matrix: python-version: ['3.8', '3.9', '3.10', '3.11', '3.12'] default-image: - - '' # use the application default - - aiidalab/aiidalab-docker-stack:latest # This is the old stack + - '' # use the application default + - aiidalab/aiidalab-docker-stack:latest # This is the old stack steps: diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index ac0bc1f..7cb9615 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -7,8 +7,8 @@ repos: - repo: https://github.com/pre-commit/pre-commit-hooks rev: v4.5.0 hooks: - - id: check-json - id: check-yaml + - id: check-toml - id: end-of-file-fixer - id: trailing-whitespace @@ -17,25 +17,13 @@ repos: hooks: - id: yamlfmt - - repo: https://github.com/mgedmin/check-manifest - rev: '0.49' - hooks: - - id: check-manifest - - - repo: https://github.com/psf/black - rev: 23.12.1 + - repo: https://github.com/astral-sh/ruff-pre-commit + rev: v0.2.1 hooks: - - id: black + - id: ruff-format exclude: ^docs/ - - - repo: https://github.com/PyCQA/flake8 - rev: 6.1.0 - hooks: - - id: flake8 - args: [--count, --show-source, --statistics] - additional_dependencies: - - flake8-bugbear - - flake8-unused-arguments==0.0.9 + - id: ruff + args: [--fix, --exit-non-zero-on-fix, --show-fixes] - repo: https://github.com/pre-commit/mirrors-mypy rev: v1.8.0 @@ -47,24 +35,7 @@ repos: - types-tabulate - types-toml - - repo: https://github.com/asottile/setup-cfg-fmt - rev: v2.5.0 - hooks: - - id: setup-cfg-fmt - - - repo: https://github.com/pycqa/isort - rev: 5.13.2 - hooks: - - id: isort - args: [--profile, black, --filter-files] - - - repo: https://github.com/sirosen/check-jsonschema - rev: 0.27.3 - hooks: - - id: check-github-workflows - - - repo: https://github.com/asottile/pyupgrade - rev: v3.15.0 + - repo: https://github.com/mgedmin/check-manifest + rev: '0.49' hooks: - - id: pyupgrade - args: [--py38-plus] + - id: check-manifest diff --git a/aiidalab_launch/__main__.py b/aiidalab_launch/__main__.py index a41fe6d..bc8e231 100644 --- a/aiidalab_launch/__main__.py +++ b/aiidalab_launch/__main__.py @@ -68,7 +68,7 @@ pass_app_state = click.make_pass_decorator(ApplicationState, ensure=True) -def exception_handler(exception_type, exception, traceback): # noqa: U100 +def exception_handler(exception_type, exception, _traceback): click.echo(f"Unexpected {exception_type.__name__}: {exception}", err=True) click.echo( "Use verbose mode `aiidalab-launch --verbose` to see full stack trace", err=True @@ -76,7 +76,7 @@ def exception_handler(exception_type, exception, traceback): # noqa: U100 def with_profile(cmd): - def callback(ctx, param, value): # noqa: U100 + def callback(ctx, _param, value): app_state = ctx.ensure_object(ApplicationState) name = value or app_state.config.default_profile LOGGER.info(f"Using profile: {name}") @@ -320,7 +320,7 @@ async def _async_start( if not force: conflict = False with spinner("Check for potential conflicts...", delay=0.1): - async for p in _find_mount_point_conflict( + async for _p in _find_mount_point_conflict( app_state.docker_client, profile, app_state.config.profiles, diff --git a/aiidalab_launch/util.py b/aiidalab_launch/util.py index 2ca6423..b8d5a45 100644 --- a/aiidalab_launch/util.py +++ b/aiidalab_launch/util.py @@ -172,15 +172,10 @@ def confirm_with_value(value: str, text: str, abort: bool = False) -> bool: def get_docker_mount( container: docker.models.containers.Container, destination: PurePosixPath ) -> docker.types.Mount: - try: - mount = [ - mount - for mount in container.attrs["Mounts"] - if mount["Destination"] == str(destination) - ][0] - except IndexError: - raise ValueError(f"No mount point for {destination}.") - return mount + for mount in container.attrs["Mounts"]: + if mount["Destination"] == str(destination): + return mount + raise ValueError(f"No mount point for {destination}.") def is_volume_readonly( diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..c340ca8 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,25 @@ +# TODO: Move entire setup.cfg to pyproject.toml + +[tool.ruff] +line-length = 88 +show-fixes = true +target-version = "py38" + +[tool.ruff.lint] +# TODO: Fixup all instances of B904 and enable this rule +ignore = ["E501", "B904"] +select = [ + "ARG", # flake8-unused-arguments + "B", # flake8-bugbear + "E", # pycodestyle + "F", # pyflake + "I", # isort + "PLE", # pylint error rules + "PLW", # pylint warning rules + "PLC", # pylint convention rules + "RUF", # ruff-specific rules + "UP" # pyupgrade +] + +[tool.ruff.lint.per-file-ignores] +"tests/*" = ["ARG001"] diff --git a/setup.cfg b/setup.cfg index 46693a6..de59097 100644 --- a/setup.cfg +++ b/setup.cfg @@ -45,14 +45,6 @@ tests = pytest-cov~=4.1.0 responses~=0.23.1 -[flake8] -ignore = - E501 - W503 - E203 -per-file-ignores = - tests/*: U100, U101 - [mypy] warn_unused_configs = True disallow_untyped_calls = True diff --git a/tests/test_cli.py b/tests/test_cli.py index 3bd7697..2ef6f32 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -131,7 +131,7 @@ def test_add_remove_profile(): result: Result = runner.invoke(cli.cli, ["profile", "list"]) assert "new-profile" not in result.output - # Remove new-profile (again – should fail) + # Remove new-profile (again - should fail) result: Result = runner.invoke( cli.cli, ["profile", "remove", "new-profile"], input="y\n" ) @@ -251,7 +251,7 @@ def assert_status_down(): assert get_volume(instance.profile.home_mount) assert get_volume(instance.profile.conda_volume_name()) - # Start instance again – should be noop. + # Start instance again - should be noop. caplog.set_level(logging.DEBUG) result: Result = runner.invoke( cli.cli, ["start", "--no-browser", "--no-pull", "--wait=300"]