From 46cb3942dac20c4196e8d46cfc04ab338a8355b7 Mon Sep 17 00:00:00 2001 From: Kenneth Daily Date: Fri, 6 Dec 2024 10:52:33 -0800 Subject: [PATCH] Add pre-commit hook First pass at adding a pre-commit hook to enforce formatting and linting standards. This adds the pre-commit command to run on a limited set of directories. Subsequent changes will remove excluded folders and run formatting on them as well. The command will use the standard pre-commit hooks for yaml, whitespace, and end of line linting. It also uses `ruff` configured identically to the develop branch, except for disabling unused import checks ("F401"), as there are known imports that are unused in the module but imported in other modules. This is common in `compat` but in other places as well. It also uses `isort`, also configured identically to the develop branch. The intention is to start enforcing formatting and linting standards on edited code. Running the pre-commit hook manually will make numerous changes that should be broken into multiple phases. Additional PRs will work to get the codebase up-to-date passing all standards. --- .pre-commit-config.yaml | 33 ++++++++++++++++++++ CONTRIBUTING.rst | 24 ++++++++++++++- pyproject.toml | 67 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 123 insertions(+), 1 deletion(-) create mode 100644 .pre-commit-config.yaml diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 000000000000..c562438ec94f --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,33 @@ +exclude: "\ + ^(\ + .changes|\ + .github|\ + awscli/examples|\ + awscli/topics|\ + awscli/botocore|\ + awscli/s3transfer|\ + awscli/doc|\ + exe/assets|\ + tests/functional/cloudformation/deploy_templates/booleans/input.yml|\ + tests/functional/cloudformation/deploy_templates/nested-tag/input.yml|\ + tests/|\ + CHANGELOG.rst|\ + configure\ + )" +repos: + - repo: 'https://github.com/pre-commit/pre-commit-hooks' + rev: v4.5.0 + hooks: + - id: check-yaml + - id: end-of-file-fixer + - id: trailing-whitespace + - repo: 'https://github.com/PyCQA/isort' + rev: 5.12.0 + hooks: + - id: isort + - repo: https://github.com/astral-sh/ruff-pre-commit + rev: v0.4.8 + hooks: + - id: ruff + args: [ --fix ] + - id: ruff-format diff --git a/CONTRIBUTING.rst b/CONTRIBUTING.rst index 0a89c7b112f9..0d60b0eda675 100644 --- a/CONTRIBUTING.rst +++ b/CONTRIBUTING.rst @@ -67,7 +67,7 @@ Also, ensure your commit messages match this format:: Describe your changes in the imperative mood, e.g. "Add foo to bar", "Update foo component for bar", "Fix race condition for foo". - + The body of the commit message can include: * an explanation of the problem and what this change @@ -120,6 +120,28 @@ can run these commands:: When you push to your remote, the output will contain a URL you can use to open a pull request. +Codestyle +--------- +This project uses `ruff `__ to enforce +codstyle requirements. We've codified this process using a tool called +`pre-commit `__. pre-commit allows us to specify a +config file with all tools required for code linting, and surfaces either a +git commit hook, or single command, for enforcing these. + +To validate your pull request prior to publishing, you can use the following +`installation guide `__ to setup pre-commit. + +If you don't want to use the git commit hook, you can run the below command +to automatically perform the codestyle validation: + +.. code-block:: bash + + $ pre-commit run + +This will automatically perform simple updates (such as white space clean up) +and provide a list of any failing checks. After these are addressed, +you can commit the changes prior to publishing the pull request. + Reporting Issues ---------------- diff --git a/pyproject.toml b/pyproject.toml index 671e41b2af59..bff2f7b73bdf 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -134,3 +134,70 @@ markers = [ [tool.black] line-length = 80 + +[tool.isort] +profile = "black" +line_length = 79 +honor_noqa = true +src_paths = ["awscli", "tests"] + +[tool.ruff] +exclude = [ + ".bzr", + ".direnv", + ".eggs", + ".git", + ".git-rewrite", + ".hg", + ".ipynb_checkpoints", + ".mypy_cache", + ".nox", + ".pants.d", + ".pyenv", + ".pytest_cache", + ".pytype", + ".ruff_cache", + ".svn", + ".tox", + ".venv", + ".vscode", + "__pypackages__", + "_build", + "buck-out", + "build", + "dist", + "node_modules", + "site-packages", + "venv", +] + +# Format same as Black. +line-length = 79 +indent-width = 4 + +target-version = "py38" + +[tool.ruff.lint] +# Enable Pyflakes (`F`) and a subset of the pycodestyle (`E`) codes by default. +# Unlike Flake8, Ruff doesn't enable pycodestyle warnings (`W`) or +# McCabe complexity (`C901`) by default. +select = ["E4", "E7", "E9", "F", "UP"] +ignore = ["F401"] + +# Allow fix for all enabled rules (when `--fix`) is provided. +fixable = ["ALL"] +unfixable = [] + +# Allow unused variables when underscore-prefixed. +dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$" + +[tool.ruff.format] +# Like Black, use double quotes for strings, spaces for indents +# and trailing commas. +quote-style = "preserve" +indent-style = "space" +skip-magic-trailing-comma = false +line-ending = "auto" + +docstring-code-format = false +docstring-code-line-length = "dynamic"