From fda4025871f4c520319e4e6117fc2290b228be1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Tr=C3=B6ndle?= Date: Thu, 14 Mar 2024 11:46:07 +0100 Subject: [PATCH 1/2] Add ruff linter and formatter as pre-commit-hooks Fixes #285 --- .flake8 | 5 ----- .pre-commit-config.yaml | 18 ++++++++++++++++++ .ruff.toml | 36 ++++++++++++++++++++++++++++++++++++ CHANGELOG.md | 1 + CONTRIBUTING.md | 5 ++++- 5 files changed, 59 insertions(+), 6 deletions(-) delete mode 100644 .flake8 create mode 100644 .pre-commit-config.yaml create mode 100644 .ruff.toml diff --git a/.flake8 b/.flake8 deleted file mode 100644 index b37ae0f2..00000000 --- a/.flake8 +++ /dev/null @@ -1,5 +0,0 @@ -[flake8] -max-line-length = 119 -ignore = E261, W503 -exclude = Snakefile, *.smk -builtins = snakemake # for using the snakemake injection in scripts diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 00000000..a7c174c6 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,18 @@ +# See https://pre-commit.com for more information +repos: +- repo: https://github.com/pre-commit/pre-commit-hooks + rev: v3.2.0 + # See https://pre-commit.com/hooks + hooks: + - id: trailing-whitespace + - id: end-of-file-fixer + - id: check-yaml + - id: check-added-large-files +- repo: https://github.com/astral-sh/ruff-pre-commit + rev: v0.3.2 + hooks: + # Run the linter. + - id: ruff + args: [--fix] + # Run the formatter. + - id: ruff-format diff --git a/.ruff.toml b/.ruff.toml new file mode 100644 index 00000000..822dffca --- /dev/null +++ b/.ruff.toml @@ -0,0 +1,36 @@ +line-length = 88 +preview = true # required to activate many pycodestyle errors and warnings as of 2024-03-13 +builtins = ["snakemake"] + +[format] +quote-style = "double" +indent-style = "space" +docstring-code-format = false +line-ending = "auto" + +[lint] +select = [ + # pycodestyle errors + "E", + # pycodestyle warnings + "W", + # Pyflakes + "F", + # pyupgrade + "UP", + # flake8-bugbear + "B", + # flake8-simplify + "SIM", + # isort + "I", +] +ignore = [ + # here and below, rules are redundant with formatter, see + # https://docs.astral.sh/ruff/formatter/#conflicting-lint-rules + "E501", + "W191", + "E111", + "E114", + "E117", +] diff --git a/CHANGELOG.md b/CHANGELOG.md index f507f28b..b801ad12 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ ### Added (workflow) +* **ADD** Ruff as our default linter and formatter (#285). * **ADD** IPython debugger to all conda environments to ease debugging (#254). * **ADD** a default Snakemake profile to run on local machines in addition to the existing profile for Euler (#211). * **ADD** a Snakemake profile to run using conda instead of mamba (#211). diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index b7037fc1..ccecdb51 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -11,7 +11,10 @@ If you have found a bug in Euro-Calliope or you want to propose a new feature, p We welcome changes that you provide as [a pull request](https://github.com/calliope-project/euro-calliope/pulls). -If you consider doing that, you need to know our branching model -- that is, which branches exist and what meaning they have. +If you consider doing that, you need to know two things: our code guidelines and our branching model. +Our code guidelines are implemented through automatic linting (code analysis) and formatting. For both, we employ the tool [Ruff](https://docs.astral.sh/ruff/). It is open-source and available as a [plug-in to many IDEs](https://docs.astral.sh/ruff/integrations/). All you need is to install the plug-in for your IDE and then you should see linting problems and you will be able to format the code automatically based on our guidelines. If you want more, you can use a [Git pre-commit-hook](https://pre-commit.com) but that step is optional. + +Next, let's talk about our branching model -- that is, which branches exist and what meaning they have. Do not worry, it is easy. Our branching model is a simplified version of _git-flow_. We are giving you a summary here that is likely sufficient for you. If you want more information, read the [blog entry](https://nvie.com/posts/a-successful-git-branching-model/) introducing git-flow. From f9e9bc740226623ed6eef6a626cfbef6a5c5c37f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Tr=C3=B6ndle?= Date: Fri, 15 Mar 2024 09:26:24 +0100 Subject: [PATCH 2/2] Remove YAML check as it will not work on our template YAMLs --- .pre-commit-config.yaml | 1 - 1 file changed, 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index a7c174c6..01a342f2 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -6,7 +6,6 @@ repos: hooks: - id: trailing-whitespace - id: end-of-file-fixer - - id: check-yaml - id: check-added-large-files - repo: https://github.com/astral-sh/ruff-pre-commit rev: v0.3.2