From 39f2b4f37d61a44b3794f5460892990eb202b8b8 Mon Sep 17 00:00:00 2001 From: jamshale <31809382+jamshale@users.noreply.github.com> Date: Thu, 4 Jul 2024 14:45:10 -0700 Subject: [PATCH] Switch from black to ruff (#3080) * Remove black in favor of ruff Signed-off-by: jamshale * Update poetry.lock file Signed-off-by: jamshale * Remove only changed files from format check for now Signed-off-by: jamshale * Fix poetry.lock file after rebase Signed-off-by: jamshale * Use sude and fix format individual file Signed-off-by: jamshale --------- Signed-off-by: jamshale --- .devcontainer/devcontainer.json | 7 +- .devcontainer/post-install.sh | 3 - .../workflows/{blackformat.yml => format.yml} | 8 +- .pre-commit-config.yaml | 13 ++- .vscode-sample/launch.json | 47 ++++------ CONTRIBUTING.md | 2 +- .../ContainerImagesAndGithubActions.md | 2 +- docs/features/DevReadMe.md | 2 - docs/features/devcontainer.md | 10 +-- poetry.lock | 86 +------------------ pyproject.toml | 3 - 11 files changed, 35 insertions(+), 148 deletions(-) rename .github/workflows/{blackformat.yml => format.yml} (52%) diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index c65f9db426..c377a666fe 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -14,7 +14,6 @@ "vscode": { "extensions": [ "ms-python.python", - "ms-python.black-formatter", "charliermarsh.ruff" ], "settings": { @@ -33,11 +32,9 @@ "source.fixAll": true, "source.organizeImports": true }, - "editor.defaultFormatter": "ms-python.black-formatter", - "ruff.organizeImports": false + "editor.defaultFormatter": "charliermarsh.ruff", + "ruff.organizeImports": true }, - "black-formatter.importStrategy": "useBundled", - "black-formatter.showNotifications": "always", "ruff.codeAction.fixViolation": { "enable": true }, diff --git a/.devcontainer/post-install.sh b/.devcontainer/post-install.sh index ed52aeb428..c106cb9032 100644 --- a/.devcontainer/post-install.sh +++ b/.devcontainer/post-install.sh @@ -8,9 +8,6 @@ WORKSPACE_DIR=$(pwd) python -m pip install --upgrade pip pip3 install -r demo/requirements.txt -r demo/requirements.behave.txt -# install black for formatting -pip3 install black - # install a version of aries-cloudagent so the pytests can pick up a version pip3 install aries-cloudagent diff --git a/.github/workflows/blackformat.yml b/.github/workflows/format.yml similarity index 52% rename from .github/workflows/blackformat.yml rename to .github/workflows/format.yml index 93d54834ae..17a6ba3942 100644 --- a/.github/workflows/blackformat.yml +++ b/.github/workflows/format.yml @@ -1,4 +1,4 @@ -name: Black Code Formatter Check +name: Ruff Code Formatter and Linting Check "on": pull_request: @@ -14,7 +14,5 @@ jobs: - uses: actions/setup-python@v5 with: python-version: "3.9" - - name: Black Code Formatter Check - # The version of black should be adjusted at the same time dev - # dependencies are updated. - uses: psf/black@24.4.2 + - name: Ruff Format and Lint Check + uses: chartboost/ruff-action@v1 diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 0901419505..cded6ee9f6 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -6,16 +6,15 @@ repos: stages: [commit-msg] args: ["--config", ".commitlint.config.js"] additional_dependencies: ['@commitlint/config-conventional'] - - repo: https://github.com/psf/black - # Ensure this is synced with pyproject.toml - rev: 24.4.2 - hooks: - - id: black - stages: [commit] - repo: https://github.com/astral-sh/ruff-pre-commit # Ensure this is synced with pyproject.toml - rev: v0.4.4 + rev: v0.5.0 hooks: + # Run the linter - id: ruff stages: [commit] args: [--fix, --exit-non-zero-on-fix] + # Run the formatter + - id: ruff-format + stages: [commit] + args: [--fix, --exit-non-zero-on-fix, --formatter] diff --git a/.vscode-sample/launch.json b/.vscode-sample/launch.json index a315b66c6f..fa882fdc21 100644 --- a/.vscode-sample/launch.json +++ b/.vscode-sample/launch.json @@ -60,7 +60,7 @@ ] }, { - "name": "ruff - aries_cloudagent", + "name": "ruff (lint) - aries_cloudagent", "type": "python", "request": "launch", "module": "ruff", @@ -71,7 +71,7 @@ "args": ["check", "."] }, { - "name": "ruff fix - aries_cloudagent", + "name": "ruff fix (lint) - aries_cloudagent", "type": "python", "request": "launch", "module": "ruff", @@ -82,7 +82,7 @@ "args": ["check", ".", "--fix"] }, { - "name": "ruff - current file", + "name": "ruff (lint) - current file", "type": "python", "request": "launch", "module": "ruff", @@ -93,52 +93,37 @@ "args": ["check", ".", "${file}"] }, { - "name": "black (check) - aries_cloudagent", + "name": "ruff fix (lint) - current file", "type": "python", "request": "launch", - "module": "black", + "module": "ruff", "console": "integratedTerminal", + "sudo": true, "justMyCode": true, "cwd": "${workspaceFolder}/aries_cloudagent", - "args": [ - ".", - "--check" - ] + "args": ["check", ".", "${file}", "--fix"] }, { - "name": "black (check) - current file", + "name": "ruff (format) - aries_cloudagent", "type": "python", "request": "launch", - "module": "black", - "console": "integratedTerminal", - "justMyCode": true, - "args": [ - "${file}", - "--check" - ] - }, - { - "name": "black (format) - aries_cloudagent", - "type": "python", - "request": "launch", - "module": "black", + "module": "ruff", "console": "integratedTerminal", + "sudo": true, "justMyCode": true, "cwd": "${workspaceFolder}/aries_cloudagent", - "args": [ - "." - ] + "args": ["format", "."] }, { - "name": "black (format) - current file", + "name": "ruff (format) - current file", "type": "python", "request": "launch", - "module": "black", + "module": "ruff", "console": "integratedTerminal", + "sudo": true, "justMyCode": true, - "args": [ - "${file}" - ] + "cwd": "${workspaceFolder}/aries_cloudagent", + "args": ["format", "${file}"] } ] } \ No newline at end of file diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 369ab200d2..43cf58c98c 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -19,7 +19,7 @@ Contributions are made pursuant to the Developer's Certificate of Origin, availa A configuration for [pre-commit](https://pre-commit.com/) is included in this repository. This is an optional tool to help contributors commit code that follows the formatting requirements enforced by the CI pipeline. Additionally, it can be used to help contributors write descriptive commit messages that can be parsed by changelog generators. -On each commit, pre-commit hooks will run that verify the committed code complies with ruff and is formatted with black. To install the ruff and black checks: +On each commit, pre-commit hooks will run that verify the committed code complies and formats with ruff. To install the ruff checks: ```bash pre-commit install diff --git a/docs/deploying/ContainerImagesAndGithubActions.md b/docs/deploying/ContainerImagesAndGithubActions.md index 3dbdf275fc..6e891fcddb 100644 --- a/docs/deploying/ContainerImagesAndGithubActions.md +++ b/docs/deploying/ContainerImagesAndGithubActions.md @@ -92,7 +92,7 @@ variants and between the BC Gov ACA-Py images. Github Container Registry. - Integration Tests (`.github/workflows/integrationtests.yml`) - Run on pull requests (to the hyperledger fork only); runs BDD integration tests. -- Black Format (`.github/workflows/blackformat.yml`) - Run on pull requests; +- Format (`.github/workflows/format.yml`) - Run on pull requests; checks formatting of files modified by the PR. - CodeQL (`.github/workflows/codeql.yml`) - Run on pull requests; performs CodeQL analysis. diff --git a/docs/features/DevReadMe.md b/docs/features/DevReadMe.md index 0490d4e4aa..0c4bcd84af 100644 --- a/docs/features/DevReadMe.md +++ b/docs/features/DevReadMe.md @@ -241,8 +241,6 @@ The `manage` script is described in detail [here](https://github.com/hyperledger We use [Ruff](https://github.com/astral-sh/ruff) to enforce a coding style guide. -We use [Black](https://black.readthedocs.io/en/stable/) to automatically format code. - Please write tests for the work that you submit. Tests should reside in a directory named `tests` alongside the code under test. Generally, there is one test file for each file module under test. Test files _must_ have a name starting with `test_` to be automatically picked up the test runner. diff --git a/docs/features/devcontainer.md b/docs/features/devcontainer.md index 316b1fd60e..d6bb547e33 100644 --- a/docs/features/devcontainer.md +++ b/docs/features/devcontainer.md @@ -44,11 +44,11 @@ To open ACA-Py in a devcontainer, we open the *root* of this repository. We can #### devcontainer.json -When the [.devcontainer/devcontainer.json](https://github.com/hyperledger/aries-cloudagent-python/blob/main/.devcontainer/devcontainer.json) is opened, you will see it building... it is building a Python 3.9 image (bash shell) and loading it with all the ACA-Py requirements (and black). We also load a few Visual Studio settings (for running Pytests and formatting with Flake and Black). +When the [.devcontainer/devcontainer.json](https://github.com/hyperledger/aries-cloudagent-python/blob/main/.devcontainer/devcontainer.json) is opened, you will see it building... it is building a Python 3.9 image (bash shell) and loading it with all the ACA-Py requirements. We also load a few Visual Studio settings (for running Pytests and formatting with Ruff). ### Poetry -The Python libraries / dependencies are installed using [`poetry`](https://python-poetry.org). For the devcontainer, we *DO NOT* use virtual environments. This means you will not see or need venv prompts in the terminals and you will not need to run tasks through poetry (ie. `poetry run black .`). If you need to add new dependencies, you will need to add the dependency via poetry *AND* you should rebuild your devcontainer. +The Python libraries / dependencies are installed using [`poetry`](https://python-poetry.org). For the devcontainer, we *DO NOT* use virtual environments. This means you will not see or need venv prompts in the terminals and you will not need to run tasks through poetry (ie. `poetry run ruff check .`). If you need to add new dependencies, you will need to add the dependency via poetry *AND* you should rebuild your devcontainer. In VS Code, open a Terminal, you should be able to run the following commands: @@ -56,7 +56,6 @@ In VS Code, open a Terminal, you should be able to run the following commands: python -m aries_cloudagent -v cd aries_cloudagent ruff check . -black . --check poetry --version ``` @@ -72,10 +71,9 @@ Found 1 error. #### extensions -We have added Black formatter and Ruff extensions. Although we have added launch settings for both `ruff` and `black`, you can also use the extension commands from the command palette. +We have added Ruff extensions. Although we have added launch settings for both `ruff`, you can also use the extension commands from the command palette. -- `Ruff: Format Document` -- `Ruff: Fix all auto-fixable problems` +- `ruff (format) - aries_cloudagent` More importantly, these extensions are now added to document save, so files will be formatted and checked. We advise that after each time you rebuild the container that you also perform: `Developer: Reload Window` to ensure the extensions are loaded correctly. diff --git a/poetry.lock b/poetry.lock index f89efd34f8..4c29bdeabf 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1,4 +1,4 @@ -# This file is automatically @generated by Poetry 1.7.1 and should not be changed by hand. +# This file is automatically @generated by Poetry 1.8.1 and should not be changed by hand. [[package]] name = "aiohttp" @@ -271,52 +271,6 @@ files = [ [package.extras] tests = ["PyHamcrest (>=2.0.2)", "mypy", "pytest (>=4.6)", "pytest-benchmark", "pytest-cov", "pytest-flake8"] -[[package]] -name = "black" -version = "24.4.2" -description = "The uncompromising code formatter." -optional = false -python-versions = ">=3.8" -files = [ - {file = "black-24.4.2-cp310-cp310-macosx_10_9_x86_64.whl", hash = "sha256:dd1b5a14e417189db4c7b64a6540f31730713d173f0b63e55fabd52d61d8fdce"}, - {file = "black-24.4.2-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:8e537d281831ad0e71007dcdcbe50a71470b978c453fa41ce77186bbe0ed6021"}, - {file = "black-24.4.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:eaea3008c281f1038edb473c1aa8ed8143a5535ff18f978a318f10302b254063"}, - {file = "black-24.4.2-cp310-cp310-win_amd64.whl", hash = "sha256:7768a0dbf16a39aa5e9a3ded568bb545c8c2727396d063bbaf847df05b08cd96"}, - {file = "black-24.4.2-cp311-cp311-macosx_10_9_x86_64.whl", hash = "sha256:257d724c2c9b1660f353b36c802ccece186a30accc7742c176d29c146df6e474"}, - {file = "black-24.4.2-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:bdde6f877a18f24844e381d45e9947a49e97933573ac9d4345399be37621e26c"}, - {file = "black-24.4.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:e151054aa00bad1f4e1f04919542885f89f5f7d086b8a59e5000e6c616896ffb"}, - {file = "black-24.4.2-cp311-cp311-win_amd64.whl", hash = "sha256:7e122b1c4fb252fd85df3ca93578732b4749d9be076593076ef4d07a0233c3e1"}, - {file = "black-24.4.2-cp312-cp312-macosx_10_9_x86_64.whl", hash = "sha256:accf49e151c8ed2c0cdc528691838afd217c50412534e876a19270fea1e28e2d"}, - {file = "black-24.4.2-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:88c57dc656038f1ab9f92b3eb5335ee9b021412feaa46330d5eba4e51fe49b04"}, - {file = "black-24.4.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:be8bef99eb46d5021bf053114442914baeb3649a89dc5f3a555c88737e5e98fc"}, - {file = "black-24.4.2-cp312-cp312-win_amd64.whl", hash = "sha256:415e686e87dbbe6f4cd5ef0fbf764af7b89f9057b97c908742b6008cc554b9c0"}, - {file = "black-24.4.2-cp38-cp38-macosx_10_9_x86_64.whl", hash = "sha256:bf10f7310db693bb62692609b397e8d67257c55f949abde4c67f9cc574492cc7"}, - {file = "black-24.4.2-cp38-cp38-macosx_11_0_arm64.whl", hash = "sha256:98e123f1d5cfd42f886624d84464f7756f60ff6eab89ae845210631714f6db94"}, - {file = "black-24.4.2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:48a85f2cb5e6799a9ef05347b476cce6c182d6c71ee36925a6c194d074336ef8"}, - {file = "black-24.4.2-cp38-cp38-win_amd64.whl", hash = "sha256:b1530ae42e9d6d5b670a34db49a94115a64596bc77710b1d05e9801e62ca0a7c"}, - {file = "black-24.4.2-cp39-cp39-macosx_10_9_x86_64.whl", hash = "sha256:37aae07b029fa0174d39daf02748b379399b909652a806e5708199bd93899da1"}, - {file = "black-24.4.2-cp39-cp39-macosx_11_0_arm64.whl", hash = "sha256:da33a1a5e49c4122ccdfd56cd021ff1ebc4a1ec4e2d01594fef9b6f267a9e741"}, - {file = "black-24.4.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:ef703f83fc32e131e9bcc0a5094cfe85599e7109f896fe8bc96cc402f3eb4b6e"}, - {file = "black-24.4.2-cp39-cp39-win_amd64.whl", hash = "sha256:b9176b9832e84308818a99a561e90aa479e73c523b3f77afd07913380ae2eab7"}, - {file = "black-24.4.2-py3-none-any.whl", hash = "sha256:d36ed1124bb81b32f8614555b34cc4259c3fbc7eec17870e8ff8ded335b58d8c"}, - {file = "black-24.4.2.tar.gz", hash = "sha256:c872b53057f000085da66a19c55d68f6f8ddcac2642392ad3a355878406fbd4d"}, -] - -[package.dependencies] -click = ">=8.0.0" -mypy-extensions = ">=0.4.3" -packaging = ">=22.0" -pathspec = ">=0.9.0" -platformdirs = ">=2" -tomli = {version = ">=1.1.0", markers = "python_version < \"3.11\""} -typing-extensions = {version = ">=4.0.1", markers = "python_version < \"3.11\""} - -[package.extras] -colorama = ["colorama (>=0.4.3)"] -d = ["aiohttp (>=3.7.4)", "aiohttp (>=3.7.4,!=3.9.0)"] -jupyter = ["ipython (>=7.8.0)", "tokenize-rt (>=3.2.0)"] -uvloop = ["uvloop (>=0.15.2)"] - [[package]] name = "cached-property" version = "1.5.2" @@ -524,20 +478,6 @@ files = [ {file = "charset_normalizer-3.3.2-py3-none-any.whl", hash = "sha256:3e4d1f6587322d2788836a99c69062fbb091331ec940e02d12d179c1d53e25fc"}, ] -[[package]] -name = "click" -version = "8.1.7" -description = "Composable command line interface toolkit" -optional = false -python-versions = ">=3.7" -files = [ - {file = "click-8.1.7-py3-none-any.whl", hash = "sha256:ae74fb96c20a0277a1d615f1e4d73c8414f5a98db8b799a7931d1582f3390c28"}, - {file = "click-8.1.7.tar.gz", hash = "sha256:ca9853ad459e787e2192211578cc907e7594e294c7ccc834310722b41b9ca6de"}, -] - -[package.dependencies] -colorama = {version = "*", markers = "platform_system == \"Windows\""} - [[package]] name = "colorama" version = "0.4.6" @@ -1633,17 +1573,6 @@ files = [ {file = "multidict-6.0.5.tar.gz", hash = "sha256:f7e301075edaf50500f0b341543c41194d8df3ae5caf4702f2095f3ca73dd8da"}, ] -[[package]] -name = "mypy-extensions" -version = "1.0.0" -description = "Type system extensions for programs checked with the mypy type checker." -optional = false -python-versions = ">=3.5" -files = [ - {file = "mypy_extensions-1.0.0-py3-none-any.whl", hash = "sha256:4392f6c0eb8a5668a69e23d168ffa70f0be9ccfd32b5cc2d26a34ae5b844552d"}, - {file = "mypy_extensions-1.0.0.tar.gz", hash = "sha256:75dbf8955dc00442a438fc4d0666508a9a97b6bd41aa2f0ffe9d2f2725af0782"}, -] - [[package]] name = "nest-asyncio" version = "1.6.0" @@ -1677,17 +1606,6 @@ files = [ {file = "packaging-23.2.tar.gz", hash = "sha256:048fb0e9405036518eaaf48a55953c750c11e1a1b68e0dd1a9d62ed0c092cfc5"}, ] -[[package]] -name = "pathspec" -version = "0.12.1" -description = "Utility library for gitignore style pattern matching of file paths." -optional = false -python-versions = ">=3.8" -files = [ - {file = "pathspec-0.12.1-py3-none-any.whl", hash = "sha256:a0d503e138a4c123b27490a4f7beda6a01c6f288df0e4a8b79c7eb0dc7b4cc08"}, - {file = "pathspec-0.12.1.tar.gz", hash = "sha256:a482d51503a1ab33b1c67a6c3813a26953dbdc71c31dacaef9a838c4e29f5712"}, -] - [[package]] name = "pillow" version = "10.3.0" @@ -2956,4 +2874,4 @@ didcommv2 = ["didcomm-messaging"] [metadata] lock-version = "2.0" python-versions = "^3.9" -content-hash = "f4834d9db47a7802b267eed59f5387ae035677244f8cb2313d4a44c9c7746087" +content-hash = "b9b5c9bd6d1d376e8757cd1f8bada6436221c239743ea92f2fd41ea325de2d20" diff --git a/pyproject.toml b/pyproject.toml index b8d09b2335..7c7e612fb1 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -66,9 +66,6 @@ didcomm-messaging = {version = "^0.1.1a0", optional = true } pre-commit = "~3.7.0" # Sync with version in .pre-commit-config.yaml ruff = "0.5.0" -# Sync with version in .github/workflows/blackformat.yml -# Sync with version in .pre-commit-config.yaml -black = "24.4.2" sphinx="^5.3.0" sphinx-rtd-theme=">=0.4.3"