From c0cbcf92ea77d028b125b4f0b66a2fa2297cfd59 Mon Sep 17 00:00:00 2001 From: Remco de Boer <29308176+redeboer@users.noreply.github.com> Date: Mon, 12 Feb 2024 22:18:44 +0100 Subject: [PATCH 01/11] DX: switch from `black` to ruff --- .gitpod.yml | 1 - .pre-commit-config.yaml | 23 ++++--------- .vscode/extensions.json | 2 +- .vscode/settings.json | 3 +- README.md | 1 - pyproject.toml | 4 --- src/compwa_policy/check_dev_files/__init__.py | 3 +- src/compwa_policy/check_dev_files/jupyter.py | 1 + src/compwa_policy/check_dev_files/ruff.py | 32 ++++++++++++++++--- 9 files changed, 40 insertions(+), 30 deletions(-) diff --git a/.gitpod.yml b/.gitpod.yml index b206b788..26c9d956 100644 --- a/.gitpod.yml +++ b/.gitpod.yml @@ -22,7 +22,6 @@ vscode: - executablebookproject.myst-highlight - github.vscode-github-actions - github.vscode-pull-request-github - - ms-python.black-formatter - ms-python.mypy-type-checker - ms-python.python - ms-python.vscode-pylance diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 23bda8a8..d8531bae 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -15,6 +15,13 @@ repos: - id: check-hooks-apply - id: check-useless-excludes + - repo: https://github.com/astral-sh/ruff-pre-commit + rev: v0.2.1 + hooks: + - id: ruff + args: [--fix] + - id: ruff-format + - repo: https://github.com/pre-commit/pre-commit-hooks rev: v4.5.0 hooks: @@ -57,16 +64,6 @@ repos: language: python files: ^\.pre\-commit\-(config|hooks)\.yaml$ - - repo: https://github.com/psf/black-pre-commit-mirror - rev: 24.1.1 - hooks: - - id: black - - - repo: https://github.com/asottile/blacken-docs - rev: 1.16.0 - hooks: - - id: blacken-docs - - repo: https://github.com/pre-commit/mirrors-prettier rev: v4.0.0-alpha.8 hooks: @@ -115,9 +112,3 @@ repos: rev: v1.1.350 hooks: - id: pyright - - - repo: https://github.com/astral-sh/ruff-pre-commit - rev: v0.2.1 - hooks: - - id: ruff - args: [--fix] diff --git a/.vscode/extensions.json b/.vscode/extensions.json index 0b9a95b2..55cb02b9 100644 --- a/.vscode/extensions.json +++ b/.vscode/extensions.json @@ -8,7 +8,6 @@ "executablebookproject.myst-highlight", "github.vscode-github-actions", "github.vscode-pull-request-github", - "ms-python.black-formatter", "ms-python.mypy-type-checker", "ms-python.python", "ms-python.vscode-pylance", @@ -26,6 +25,7 @@ "bungcip.better-toml", "davidanson.vscode-markdownlint", "garaioag.garaio-vscode-unwanted-recommendations", + "ms-python.black-formatter", "ms-python.flake8", "ms-python.isort", "ms-python.pylint", diff --git a/.vscode/settings.json b/.vscode/settings.json index 3c6e5759..c46f751d 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -19,13 +19,12 @@ "editor.codeActionsOnSave": { "source.organizeImports": "explicit" }, - "editor.defaultFormatter": "ms-python.black-formatter", + "editor.defaultFormatter": "charliermarsh.ruff", "editor.rulers": [88] }, "[yaml]": { "editor.defaultFormatter": "esbenp.prettier-vscode" }, - "black-formatter.importStrategy": "fromEnvironment", "coverage-gutters.coverageFileNames": ["coverage.xml"], "coverage-gutters.coverageReportFileName": "**/htmlcov/index.html", "coverage-gutters.showGutterCoverage": false, diff --git a/README.md b/README.md index a6bfe9af..cd4ecf99 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,6 @@ [![pytest](https://github.com/ComPWA/qrules/workflows/pytest/badge.svg)](https://github.com/ComPWA/qrules/actions?query=branch%3Amain+workflow%3Apytest) [![pre-commit.ci status](https://results.pre-commit.ci/badge/github/ComPWA/policy/main.svg)](https://results.pre-commit.ci/latest/github/ComPWA/policy/main) [![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier) -[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black) [![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/charliermarsh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff) This repository sets the policies for developer environment in repositories if the [ComPWA organization](https://github.com/ComPWA) (See our [Help developing](https://compwa.github.io/develop) page). The policies are automatically enforced through [pre-commit](https://pre-commit.com). diff --git a/pyproject.toml b/pyproject.toml index 7139a1f2..4c6f2f8a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -59,9 +59,6 @@ doc = [ "sphinx-book-theme", "sphinx-copybutton", ] -format = [ - "black", -] lint = [ "compwa-policy[mypy]", "radon", @@ -74,7 +71,6 @@ mypy = [ "types-toml", ] sty = [ - "compwa-policy[format]", "compwa-policy[lint]", "compwa-policy[test]", # for pytest type hints "pre-commit >=1.4.0", diff --git a/src/compwa_policy/check_dev_files/__init__.py b/src/compwa_policy/check_dev_files/__init__.py index 671cb03f..fd61a092 100644 --- a/src/compwa_policy/check_dev_files/__init__.py +++ b/src/compwa_policy/check_dev_files/__init__.py @@ -80,7 +80,8 @@ def main(argv: Sequence[str] | None = None) -> int: executor(toml.main) # has to run before pre-commit executor(prettier.main, args.no_prettierrc) if is_python_repo: - executor(black.main, has_notebooks) + if args.no_ruff: + executor(black.main, has_notebooks) if not args.no_github_actions: executor( release_drafter.main, diff --git a/src/compwa_policy/check_dev_files/jupyter.py b/src/compwa_policy/check_dev_files/jupyter.py index 61b3e3bc..e16a476a 100644 --- a/src/compwa_policy/check_dev_files/jupyter.py +++ b/src/compwa_policy/check_dev_files/jupyter.py @@ -19,6 +19,7 @@ def _update_dev_requirements() -> None: return hierarchy = ["jupyter", "dev"] dependencies = [ + "black", "isort", "jupyterlab", "jupyterlab-code-formatter", diff --git a/src/compwa_policy/check_dev_files/ruff.py b/src/compwa_policy/check_dev_files/ruff.py index 399fe2b7..4491a2c6 100644 --- a/src/compwa_policy/check_dev_files/ruff.py +++ b/src/compwa_policy/check_dev_files/ruff.py @@ -48,6 +48,7 @@ def main(has_notebooks: bool) -> None: "[![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/charliermarsh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)", ) executor(_check_setup_cfg) + executor(_remove_black) executor(_remove_flake8) executor(_remove_isort) executor(_remove_pydocstyle) @@ -95,6 +96,22 @@ def _check_setup_cfg() -> None: raise PrecommitError(msg) +def _remove_black() -> None: + executor = Executor() + executor( + vscode.remove_extension_recommendation, + "ms-python.black-formatter", + unwanted=True, + ) + executor(___uninstall, "black") + executor(remove_badge, r".*https://github\.com/psf.*/black.*") + executor(remove_precommit_hook, "black-jupyter") + executor(remove_precommit_hook, "black") + executor(remove_precommit_hook, "blacken-docs") + executor(vscode.remove_settings, ["black-formatter.importStrategy"]) + executor.finalize() + + def _remove_flake8() -> None: executor = Executor() executor(remove_configs, [".flake8"]) @@ -211,6 +228,10 @@ def ___uninstall_from_pyproject_toml(package: str) -> None: if package in values: values.remove(package) updated = True + if updated: + empty_sections = [k for k, v in optional_dependencies.items() if not v] + for section in empty_sections: + del optional_dependencies[section] if updated: write_pyproject(pyproject) msg = f"Removed {package} from {CONFIG_PATH.pyproject}" @@ -565,14 +586,16 @@ def _update_precommit_hook(has_notebooks: bool) -> None: if not CONFIG_PATH.precommit.exists(): return yaml = YAML(typ="rt") - ruff_hook = Hook(id="ruff", args=yaml.load("[--fix]")) + lint_hook = Hook(id="ruff", args=yaml.load("[--fix]")) + format_hook = Hook(id="ruff-format") if has_notebooks: types = yaml.load("[python, pyi, jupyter]") - ruff_hook["types_or"] = types + lint_hook["types_or"] = types + format_hook["types_or"] = types expected_repo = Repo( repo="https://github.com/astral-sh/ruff-pre-commit", rev="", - hooks=[ruff_hook], + hooks=[lint_hook, format_hook], ) update_single_hook_precommit_repo(expected_repo) @@ -607,7 +630,8 @@ def _update_vscode_settings() -> None: "[python]": { "editor.codeActionsOnSave": { "source.organizeImports": "explicit", - } + }, + "editor.defaultFormatter": "charliermarsh.ruff", }, "ruff.enable": True, "ruff.organizeImports": True, From f36ad8a47cf8849674369e8f5ca512429ab676ab Mon Sep 17 00:00:00 2001 From: Remco de Boer <29308176+redeboer@users.noreply.github.com> Date: Mon, 12 Feb 2024 22:18:44 +0100 Subject: [PATCH 02/11] MAINT: uninstall `radon` --- pyproject.toml | 1 - src/compwa_policy/check_dev_files/ruff.py | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 4c6f2f8a..41ad11e9 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -61,7 +61,6 @@ doc = [ ] lint = [ "compwa-policy[mypy]", - "radon", "ruff", ] mypy = [ diff --git a/src/compwa_policy/check_dev_files/ruff.py b/src/compwa_policy/check_dev_files/ruff.py index 4491a2c6..5aec125c 100644 --- a/src/compwa_policy/check_dev_files/ruff.py +++ b/src/compwa_policy/check_dev_files/ruff.py @@ -48,6 +48,7 @@ def main(has_notebooks: bool) -> None: "[![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/charliermarsh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)", ) executor(_check_setup_cfg) + executor(___uninstall, "radon") executor(_remove_black) executor(_remove_flake8) executor(_remove_isort) From 8471db6cd889ad2254564002bdf442b51477b040 Mon Sep 17 00:00:00 2001 From: Remco de Boer <29308176+redeboer@users.noreply.github.com> Date: Mon, 12 Feb 2024 22:18:44 +0100 Subject: [PATCH 03/11] MAINT: move `ruff` to `lint` optional dependencies --- pyproject.toml | 8 +++----- src/compwa_policy/check_dev_files/ruff.py | 11 ++++------- 2 files changed, 7 insertions(+), 12 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 41ad11e9..894b20fd 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -59,10 +59,6 @@ doc = [ "sphinx-book-theme", "sphinx-copybutton", ] -lint = [ - "compwa-policy[mypy]", - "ruff", -] mypy = [ "mypy", "sphinx-api-relink >=0.0.3", @@ -71,8 +67,10 @@ mypy = [ ] sty = [ "compwa-policy[lint]", - "compwa-policy[test]", # for pytest type hints + "compwa-policy[mypy]", + "compwa-policy[test]", "pre-commit >=1.4.0", + "ruff", ] test = [ "pytest", diff --git a/src/compwa_policy/check_dev_files/ruff.py b/src/compwa_policy/check_dev_files/ruff.py index 5aec125c..26a06a49 100644 --- a/src/compwa_policy/check_dev_files/ruff.py +++ b/src/compwa_policy/check_dev_files/ruff.py @@ -75,12 +75,9 @@ def _check_setup_cfg() -> None: [{extras_require}] ... - lint = - ruff - ... sty = ... - %(lint)s + ruff ... dev = ... @@ -88,11 +85,11 @@ def _check_setup_cfg() -> None: ... """ msg = dedent(msg).strip() - for section in ("dev", "lint", "sty"): + for section in ("dev", "sty"): if cfg.has_option(extras_require, section): continue raise PrecommitError(msg) - lint_section = cfg.get(extras_require, "lint") + lint_section = cfg.get(extras_require, "sty") if not any("ruff" in line for line in lint_section.split("\n")): raise PrecommitError(msg) @@ -618,7 +615,7 @@ def _update_lint_dependencies() -> None: ruff = 'ruff; python_version >="3.7.0"' else: ruff = "ruff" - add_dependency(ruff, optional_key=["lint", "sty", "dev"]) + add_dependency(ruff, optional_key=["sty", "dev"]) def _update_vscode_settings() -> None: From eb221d1af732a9fe80cce4c3336ec267ad783280 Mon Sep 17 00:00:00 2001 From: Remco de Boer <29308176+redeboer@users.noreply.github.com> Date: Mon, 12 Feb 2024 22:18:44 +0100 Subject: [PATCH 04/11] FIX: avoid use of YAML anchors --- src/compwa_policy/check_dev_files/ruff.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/compwa_policy/check_dev_files/ruff.py b/src/compwa_policy/check_dev_files/ruff.py index 26a06a49..b9ff5e46 100644 --- a/src/compwa_policy/check_dev_files/ruff.py +++ b/src/compwa_policy/check_dev_files/ruff.py @@ -587,9 +587,9 @@ def _update_precommit_hook(has_notebooks: bool) -> None: lint_hook = Hook(id="ruff", args=yaml.load("[--fix]")) format_hook = Hook(id="ruff-format") if has_notebooks: - types = yaml.load("[python, pyi, jupyter]") - lint_hook["types_or"] = types - format_hook["types_or"] = types + types_str = "[python, pyi, jupyter]" + lint_hook["types_or"] = yaml.load(types_str) # use twice to avoid YAML anchor + format_hook["types_or"] = yaml.load(types_str) expected_repo = Repo( repo="https://github.com/astral-sh/ruff-pre-commit", rev="", From fdfe0ff09e1e5f1919c95848ca893ba5b5619454 Mon Sep 17 00:00:00 2001 From: Remco de Boer <29308176+redeboer@users.noreply.github.com> Date: Mon, 12 Feb 2024 22:18:44 +0100 Subject: [PATCH 05/11] FIX: ignore `black` under `jupyter` section --- src/compwa_policy/check_dev_files/ruff.py | 24 +++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/src/compwa_policy/check_dev_files/ruff.py b/src/compwa_policy/check_dev_files/ruff.py index b9ff5e46..d99cb9bf 100644 --- a/src/compwa_policy/check_dev_files/ruff.py +++ b/src/compwa_policy/check_dev_files/ruff.py @@ -101,7 +101,7 @@ def _remove_black() -> None: "ms-python.black-formatter", unwanted=True, ) - executor(___uninstall, "black") + executor(___uninstall, "black", ignore=["jupyter"]) executor(remove_badge, r".*https://github\.com/psf.*/black.*") executor(remove_precommit_hook, "black-jupyter") executor(remove_precommit_hook, "black") @@ -187,28 +187,30 @@ def _remove_pylint() -> None: executor.finalize() -def ___uninstall(package: str) -> None: - ___uninstall_from_setup_cfg(package) - ___uninstall_from_pyproject_toml(package) +def ___uninstall(package: str, ignore: Iterable[str] | None = None) -> None: + ignored_sections = set() if ignore is None else set(ignore) + ___uninstall_from_setup_cfg(package, ignored_sections) + ___uninstall_from_pyproject_toml(package, ignored_sections) -def ___uninstall_from_setup_cfg(package: str) -> None: +def ___uninstall_from_setup_cfg(package: str, ignored_sections: set[str]) -> None: if not os.path.exists(CONFIG_PATH.setup_cfg): return cfg = open_setup_cfg() section = "options.extras_require" if not cfg.has_section(section): return - for option in cfg[section]: - if not cfg.has_option(section, option): + extras_require = cfg[section] + for option in extras_require: + if option in ignored_sections: continue - if package not in cfg.get(section, option): + if package not in cfg.get(section, option, raw=True): continue msg = f'Please remove {package} from the "{section}" section of setup.cfg' raise PrecommitError(msg) -def ___uninstall_from_pyproject_toml(package: str) -> None: +def ___uninstall_from_pyproject_toml(package: str, ignored_sections: set[str]) -> None: # noqa: C901 if not os.path.exists(CONFIG_PATH.pyproject): return pyproject = load_pyproject() @@ -222,7 +224,9 @@ def ___uninstall_from_pyproject_toml(package: str) -> None: updated = True optional_dependencies = project.get("optional-dependencies") if optional_dependencies is not None: - for values in optional_dependencies.values(): + for section, values in optional_dependencies.items(): + if section in ignored_sections: + continue if package in values: values.remove(package) updated = True From d0a2e3de4580ff3e131f2c647f79d03fcca3398e Mon Sep 17 00:00:00 2001 From: Remco de Boer <29308176+redeboer@users.noreply.github.com> Date: Mon, 12 Feb 2024 22:18:44 +0100 Subject: [PATCH 06/11] FIX: keep `black` in optional `doc` dependencies --- src/compwa_policy/check_dev_files/ruff.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compwa_policy/check_dev_files/ruff.py b/src/compwa_policy/check_dev_files/ruff.py index d99cb9bf..d1670eb7 100644 --- a/src/compwa_policy/check_dev_files/ruff.py +++ b/src/compwa_policy/check_dev_files/ruff.py @@ -101,7 +101,7 @@ def _remove_black() -> None: "ms-python.black-formatter", unwanted=True, ) - executor(___uninstall, "black", ignore=["jupyter"]) + executor(___uninstall, "black", ignore=["doc", "jupyter"]) executor(remove_badge, r".*https://github\.com/psf.*/black.*") executor(remove_precommit_hook, "black-jupyter") executor(remove_precommit_hook, "black") From 9897409af53af23695d0b0ec644797c2d71a40e1 Mon Sep 17 00:00:00 2001 From: Remco de Boer <29308176+redeboer@users.noreply.github.com> Date: Mon, 12 Feb 2024 22:18:44 +0100 Subject: [PATCH 07/11] FIX: keep `black` in optional `test` dependencies --- src/compwa_policy/check_dev_files/ruff.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compwa_policy/check_dev_files/ruff.py b/src/compwa_policy/check_dev_files/ruff.py index d1670eb7..33940a33 100644 --- a/src/compwa_policy/check_dev_files/ruff.py +++ b/src/compwa_policy/check_dev_files/ruff.py @@ -101,7 +101,7 @@ def _remove_black() -> None: "ms-python.black-formatter", unwanted=True, ) - executor(___uninstall, "black", ignore=["doc", "jupyter"]) + executor(___uninstall, "black", ignore=["doc", "jupyter", "test"]) executor(remove_badge, r".*https://github\.com/psf.*/black.*") executor(remove_precommit_hook, "black-jupyter") executor(remove_precommit_hook, "black") From 420497c353c0345fe10f00078d486e192ccd066e Mon Sep 17 00:00:00 2001 From: Remco de Boer <29308176+redeboer@users.noreply.github.com> Date: Mon, 12 Feb 2024 22:18:44 +0100 Subject: [PATCH 08/11] ENH: remove `black` settings --- pyproject.toml | 25 ----------------------- src/compwa_policy/check_dev_files/ruff.py | 11 +++++----- 2 files changed, 6 insertions(+), 30 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 894b20fd..bae02ed3 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -116,31 +116,6 @@ where = ["src"] [tool.setuptools_scm] write_to = "src/compwa_policy/version.py" -[tool.black] -exclude = ''' -/( - .*\.egg-info - | .*build - | \.eggs - | \.git - | \.pytest_cache - | \.tox - | \.venv - | \.vscode - | dist -)/ -''' -include = '\.pyi?$' -preview = true -target-version = [ - "py310", - "py311", - "py312", - "py37", - "py38", - "py39", -] - [tool.coverage.run] branch = true source = ["src"] diff --git a/src/compwa_policy/check_dev_files/ruff.py b/src/compwa_policy/check_dev_files/ruff.py index 33940a33..5096a9f4 100644 --- a/src/compwa_policy/check_dev_files/ruff.py +++ b/src/compwa_policy/check_dev_files/ruff.py @@ -101,6 +101,7 @@ def _remove_black() -> None: "ms-python.black-formatter", unwanted=True, ) + executor(__remove_tool_table, "black") executor(___uninstall, "black", ignore=["doc", "jupyter", "test"]) executor(remove_badge, r".*https://github\.com/psf.*/black.*") executor(remove_precommit_hook, "black-jupyter") @@ -126,9 +127,9 @@ def _remove_flake8() -> None: def _remove_isort() -> None: executor = Executor() - executor(__remove_isort_settings) executor(__remove_nbqa_option, "black") executor(__remove_nbqa_option, "isort") + executor(__remove_tool_table, "isort") executor(vscode.remove_extension_recommendation, "ms-python.isort", unwanted=True) executor(remove_precommit_hook, "isort") executor(remove_precommit_hook, "nbqa-isort") @@ -151,13 +152,13 @@ def __remove_nbqa_option(option: str) -> None: raise PrecommitError(msg) -def __remove_isort_settings() -> None: +def __remove_tool_table(tool_table: str) -> None: pyproject = load_pyproject() - if pyproject.get("tool", {}).get("isort") is None: + if pyproject.get("tool", {}).get(tool_table) is None: return - pyproject["tool"].remove("isort") # type: ignore[union-attr] + pyproject["tool"].remove(tool_table) # type: ignore[union-attr] write_pyproject(pyproject) - msg = f"Removed [tool.isort] section from {CONFIG_PATH.pyproject}" + msg = f"Removed [tool.{tool_table}] section from {CONFIG_PATH.pyproject}" raise PrecommitError(msg) From 9309faff887da5cd8d1e300842d101244922c268 Mon Sep 17 00:00:00 2001 From: Remco de Boer <29308176+redeboer@users.noreply.github.com> Date: Mon, 12 Feb 2024 22:18:44 +0100 Subject: [PATCH 09/11] ENH: ignore Ruff rule ISC001 for formatter --- pyproject.toml | 1 + src/compwa_policy/check_dev_files/ruff.py | 1 + 2 files changed, 2 insertions(+) diff --git a/pyproject.toml b/pyproject.toml index bae02ed3..b0f2631e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -258,6 +258,7 @@ ignore = [ "D407", "D416", "E501", + "ISC001", "PLR0913", "PLW1514", "PLW2901", diff --git a/src/compwa_policy/check_dev_files/ruff.py b/src/compwa_policy/check_dev_files/ruff.py index 5096a9f4..cc7ae028 100644 --- a/src/compwa_policy/check_dev_files/ruff.py +++ b/src/compwa_policy/check_dev_files/ruff.py @@ -361,6 +361,7 @@ def __update_lint_settings() -> None: "D407", # missing dashed underline after section "D416", # section name does not have to end with a colon "E501", # line-width already handled by black + "ISC001", # conflicts with ruff formatter "PLW1514", # allow missing encoding in open() "SIM108", # allow if-else blocks ] From 96e54c8a7297c22090c41c1d28f8d2f04fec2f66 Mon Sep 17 00:00:00 2001 From: Remco de Boer <29308176+redeboer@users.noreply.github.com> Date: Mon, 12 Feb 2024 22:18:44 +0100 Subject: [PATCH 10/11] MAINT: remove `lint` optional dependencies --- pyproject.toml | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index b0f2631e..5bcd093a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -59,16 +59,9 @@ doc = [ "sphinx-book-theme", "sphinx-copybutton", ] -mypy = [ - "mypy", - "sphinx-api-relink >=0.0.3", - "types-PyYAML", - "types-toml", -] sty = [ - "compwa-policy[lint]", - "compwa-policy[mypy]", - "compwa-policy[test]", + "compwa-policy[types]", + "mypy", "pre-commit >=1.4.0", "ruff", ] @@ -77,6 +70,12 @@ test = [ "pytest-cov", "pytest-xdist", ] +types = [ + "pytest", + "sphinx-api-relink >=0.0.3", + "types-PyYAML", + "types-toml", +] [project.readme] content-type = "text/markdown" From 3e254b8c138676bfa98142ac6a9d03be97c5ff88 Mon Sep 17 00:00:00 2001 From: Remco de Boer <29308176+redeboer@users.noreply.github.com> Date: Mon, 12 Feb 2024 22:18:44 +0100 Subject: [PATCH 11/11] DX: format docstrings with Ruff --- pyproject.toml | 4 ++++ src/compwa_policy/check_dev_files/__init__.py | 6 +++--- src/compwa_policy/check_dev_files/ruff.py | 21 ++++++++++++++++--- src/compwa_policy/utilities/vscode.py | 17 +++++++-------- 4 files changed, 32 insertions(+), 16 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 5bcd093a..326753d5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -212,6 +212,10 @@ src = [ ] target-version = "py37" +[tool.ruff.format] +docstring-code-format = true +line-ending = "lf" + [tool.ruff.lint] extend-select = [ "A", diff --git a/src/compwa_policy/check_dev_files/__init__.py b/src/compwa_policy/check_dev_files/__init__.py index fd61a092..0929c8fa 100644 --- a/src/compwa_policy/check_dev_files/__init__.py +++ b/src/compwa_policy/check_dev_files/__init__.py @@ -288,11 +288,11 @@ def _create_argparse() -> ArgumentParser: def _to_list(arg: str) -> list[str]: """Create a comma-separated list from a string argument. - >>> _to_list('a c , test,b') + >>> _to_list("a c , test,b") ['a', 'b', 'c', 'test'] - >>> _to_list(' ') + >>> _to_list(" ") [] - >>> _to_list('') + >>> _to_list("") [] """ space_separated = arg.replace(",", " ") diff --git a/src/compwa_policy/check_dev_files/ruff.py b/src/compwa_policy/check_dev_files/ruff.py index cc7ae028..c2629b31 100644 --- a/src/compwa_policy/check_dev_files/ruff.py +++ b/src/compwa_policy/check_dev_files/ruff.py @@ -279,7 +279,8 @@ def _move_ruff_lint_config() -> None: def _update_ruff_config(has_notebooks: bool) -> None: executor = Executor() executor(__update_global_settings, has_notebooks) - executor(__update_lint_settings) + executor(__update_ruff_format_settings) + executor(__update_ruff_lint_settings) executor(__update_per_file_ignores, has_notebooks) executor(__update_isort_settings) executor(__update_pydocstyle_settings) @@ -347,7 +348,21 @@ def ___get_src_directories() -> list[str]: return to_toml_array(sorted(directories)) -def __update_lint_settings() -> None: +def __update_ruff_format_settings() -> None: + pyproject = load_pyproject() + settings = get_sub_table(pyproject, "tool.ruff.format", create=True) + minimal_settings = { + "docstring-code-format": True, + "line-ending": "lf", + } + if not complies_with_subset(settings, minimal_settings): + settings.update(minimal_settings) + write_pyproject(pyproject) + msg = f"Updated Ruff formatter configuration in {CONFIG_PATH.pyproject}" + raise PrecommitError(msg) + + +def __update_ruff_lint_settings() -> None: pyproject = load_pyproject() settings = get_sub_table(pyproject, "tool.ruff.lint", create=True) ignored_rules = [ @@ -376,7 +391,7 @@ def __update_lint_settings() -> None: if not complies_with_subset(settings, minimal_settings): settings.update(minimal_settings) write_pyproject(pyproject) - msg = f"Updated Ruff configuration in {CONFIG_PATH.pyproject}" + msg = f"Updated Ruff linting configuration in {CONFIG_PATH.pyproject}" raise PrecommitError(msg) diff --git a/src/compwa_policy/utilities/vscode.py b/src/compwa_policy/utilities/vscode.py index 8d1e9cec..8e07b3dd 100644 --- a/src/compwa_policy/utilities/vscode.py +++ b/src/compwa_policy/utilities/vscode.py @@ -181,19 +181,16 @@ def sort_case_insensitive(dct): # type: ignore[no-untyped-def] >>> import pytest, sys >>> if sys.version_info >= (3, 12): ... pytest.skip() - ... - >>> sort_case_insensitive( - ... { - ... "cSpell.enabled": True, - ... "coverage-gutters": ["test", "coverage.xml"], - ... } - ... ) + >>> sort_case_insensitive({ + ... "cSpell.enabled": True, + ... "coverage-gutters": ["test", "coverage.xml"], + ... }) OrderedDict([('coverage-gutters', ['coverage.xml', 'test']), ('cSpell.enabled', True)]) """ if isinstance(dct, abc.Mapping): - return collections.OrderedDict( - {k: sort_case_insensitive(dct[k]) for k in sorted(dct, key=str.lower)} - ) + return collections.OrderedDict({ + k: sort_case_insensitive(dct[k]) for k in sorted(dct, key=str.lower) + }) if isinstance(dct, str): return dct if isinstance(dct, abc.Iterable):