From 2cb2cd480c5ff63592b7060d1498981814568ac3 Mon Sep 17 00:00:00 2001 From: per1234 Date: Fri, 13 Aug 2021 20:55:11 -0700 Subject: [PATCH] Sync Python checks with "template" assets We have assembled a collection of reusable project assets: https://github.com/arduino/tooling-project-assets These will be used in the repositories of all Arduino tooling projects. Some improvements and standardizations have been made in the upstream "template" assets for checking Python files. Those are introduced to this repository here. Notable: - Bump Python version used by the workflow from 3.8 to 3.9 - Add check for black formatting compliance - Bump black formatting tool dependency to ^21.7b0 - Add pep8-naming flake8 plugin to check for Python naming convention compliance - Add flake8 problem matcher to prominently surface linting violations in diffs, workflow run summary and logs --- .flake8 | 11 ++-- .github/workflows/check-python-task.yml | 86 +++++++++++++++++++++++++ .github/workflows/lint-python.yml | 40 ------------ Taskfile.yml | 13 ++-- poetry.lock | 56 ++++++++++++++-- pyproject.toml | 3 +- 6 files changed, 154 insertions(+), 55 deletions(-) create mode 100644 .github/workflows/check-python-task.yml delete mode 100644 .github/workflows/lint-python.yml diff --git a/.flake8 b/.flake8 index accc53c1..efde3a0c 100644 --- a/.flake8 +++ b/.flake8 @@ -1,9 +1,12 @@ +# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-python/.flake8 +# See: https://flake8.pycqa.org/en/latest/user/configuration.html +# The code style defined in this file is the official standardized style to be used in all Arduino tooling projects and +# should not be modified. + [flake8] doctests = True -ignore = - # W503 and W504 are mutually exclusive, so one or the other must be ignored. - # PEP 8 recommends line break before, so we keep W504. - W503 +# W503 and W504 are mutually exclusive. PEP 8 recommends line break before. +ignore = W503 max-complexity = 10 max-line-length = 120 select = E,W,F,C,N diff --git a/.github/workflows/check-python-task.yml b/.github/workflows/check-python-task.yml new file mode 100644 index 00000000..375130d9 --- /dev/null +++ b/.github/workflows/check-python-task.yml @@ -0,0 +1,86 @@ +# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/check-python-task.md +name: Check Python + +env: + # See: https://github.com/actions/setup-python/tree/v2#available-versions-of-python + PYTHON_VERSION: "3.9" + +# See: https://docs.github.com/en/actions/reference/events-that-trigger-workflows +on: + push: + paths: + - ".github/workflows/check-python-task.ya?ml" + - "**/.flake8" + - "**/poetry.lock" + - "**/pyproject.toml" + - "**/setup.cfg" + - "Taskfile.ya?ml" + - "**/tox.ini" + - "**.py" + pull_request: + paths: + - ".github/workflows/check-python-task.ya?ml" + - "**/.flake8" + - "**/poetry.lock" + - "**/pyproject.toml" + - "**/setup.cfg" + - "Taskfile.ya?ml" + - "**/tox.ini" + - "**.py" + workflow_dispatch: + repository_dispatch: + +jobs: + lint: + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v2 + + - name: Install Python + uses: actions/setup-python@v2 + with: + python-version: ${{ env.PYTHON_VERSION }} + + - name: Install Poetry + run: pip install poetry + + - name: Install Task + uses: arduino/setup-task@v1 + with: + repo-token: ${{ secrets.GITHUB_TOKEN }} + version: 3.x + + - name: Run flake8 + uses: liskin/gh-problem-matcher-wrap@v1 + with: + linters: flake8 + run: task python:lint + + formatting: + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v2 + + - name: Install Python + uses: actions/setup-python@v2 + with: + python-version: ${{ env.PYTHON_VERSION }} + + - name: Install Poetry + run: pip install poetry + + - name: Install Task + uses: arduino/setup-task@v1 + with: + repo-token: ${{ secrets.GITHUB_TOKEN }} + version: 3.x + + - name: Format Python code + run: task python:format + + - name: Check formatting + run: git diff --color --exit-code diff --git a/.github/workflows/lint-python.yml b/.github/workflows/lint-python.yml deleted file mode 100644 index 9258d0fe..00000000 --- a/.github/workflows/lint-python.yml +++ /dev/null @@ -1,40 +0,0 @@ -name: Lint Python code - -on: - push: - paths: - - "**.py" - - ".flake8" - - "pyproject.toml" - - "Taskfile.yml" - pull_request: - paths: - - "**.py" - - ".flake8" - - "pyproject.toml" - - "Taskfile.yml" - -jobs: - lint-python: - runs-on: ubuntu-latest - - steps: - - name: Checkout - uses: actions/checkout@v2 - - - name: Install Taskfile - uses: arduino/setup-task@v1 - with: - repo-token: ${{ secrets.GITHUB_TOKEN }} - version: 3.x - - - name: Install Python - uses: actions/setup-python@v2 - with: - python-version: "3.8" - - - name: Install Poetry - run: pip install poetry - - - name: Lint Python files - run: task python:check diff --git a/Taskfile.yml b/Taskfile.yml index 51f6e288..a036ae23 100755 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -4,6 +4,7 @@ includes: dist: ./DistTasks.yml tasks: + # Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/poetry-task/Taskfile.yml poetry:install-deps: desc: Install dependencies managed by Poetry cmds: @@ -190,16 +191,20 @@ tasks: cmds: - task: python:lint + # Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-python-task/Taskfile.yml python:lint: desc: Lint Python code - cmds: + deps: - task: poetry:install-deps - - poetry run flake8 + cmds: + - poetry run flake8 --show-source + # Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-python-task/Taskfile.yml python:format: - desc: Automatically formats Python files - cmds: + desc: Format Python files + deps: - task: poetry:install-deps + cmds: - poetry run black . vars: diff --git a/poetry.lock b/poetry.lock index ce147ed8..ff50d6d5 100644 --- a/poetry.lock +++ b/poetry.lock @@ -30,7 +30,7 @@ tests_no_zope = ["coverage[toml] (>=5.0.2)", "hypothesis", "pympler", "pytest (> [[package]] name = "black" -version = "21.5b1" +version = "21.7b0" description = "The uncompromising code formatter." category = "dev" optional = false @@ -42,12 +42,13 @@ click = ">=7.1.2" mypy-extensions = ">=0.4.3" pathspec = ">=0.8.1,<1" regex = ">=2020.1.8" -toml = ">=0.10.1" +tomli = ">=0.2.6,<2.0.0" [package.extras] colorama = ["colorama (>=0.4.3)"] -d = ["aiohttp (>=3.6.0)", "aiohttp-cors"] +d = ["aiohttp (>=3.6.0)", "aiohttp-cors (>=0.4.0)"] python2 = ["typed-ast (>=1.4.2)"] +uvloop = ["uvloop (>=0.15.2)"] [[package]] name = "click" @@ -78,6 +79,17 @@ mccabe = ">=0.6.0,<0.7.0" pycodestyle = ">=2.7.0,<2.8.0" pyflakes = ">=2.3.0,<2.4.0" +[[package]] +name = "flake8-polyfill" +version = "1.0.2" +description = "Polyfill package for Flake8 plugins" +category = "dev" +optional = false +python-versions = "*" + +[package.dependencies] +flake8 = "*" + [[package]] name = "ghp-import" version = "2.0.1" @@ -299,6 +311,18 @@ category = "dev" optional = false python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*" +[[package]] +name = "pep8-naming" +version = "0.12.1" +description = "Check PEP-8 naming conventions, plugin for flake8" +category = "dev" +optional = false +python-versions = "*" + +[package.dependencies] +flake8 = ">=3.9.1" +flake8-polyfill = ">=1.0.2,<2" + [[package]] name = "pluggy" version = "0.13.1" @@ -453,6 +477,14 @@ category = "main" optional = false python-versions = ">=2.6, !=3.0.*, !=3.1.*, !=3.2.*" +[[package]] +name = "tomli" +version = "1.2.1" +description = "A lil' TOML parser" +category = "dev" +optional = false +python-versions = ">=3.6" + [[package]] name = "verspec" version = "0.1.0" @@ -490,7 +522,7 @@ testing = ["pytest (>=4.6)", "pytest-checkdocs (>=1.2.3)", "pytest-flake8", "pyt [metadata] lock-version = "1.1" python-versions = "^3.8" -content-hash = "c66aa8bf1f6fb9772063141e2133242c3a85ce3ec2880bc3b8a9b6f391f36b7d" +content-hash = "5dcefb5cc6ba4f117c538f7638e6f89804645f0f6f8f7857305345c8293de226" [metadata.files] appdirs = [ @@ -506,8 +538,8 @@ attrs = [ {file = "attrs-21.2.0.tar.gz", hash = "sha256:ef6aaac3ca6cd92904cdd0d83f629a15f18053ec84e6432106f7a4d04ae4f5fb"}, ] black = [ - {file = "black-21.5b1-py3-none-any.whl", hash = "sha256:8a60071a0043876a4ae96e6c69bd3a127dad2c1ca7c8083573eb82f92705d008"}, - {file = "black-21.5b1.tar.gz", hash = "sha256:23695358dbcb3deafe7f0a3ad89feee5999a46be5fec21f4f1d108be0bcdb3b1"}, + {file = "black-21.7b0-py3-none-any.whl", hash = "sha256:1c7aa6ada8ee864db745b22790a32f94b2795c253a75d6d9b5e439ff10d23116"}, + {file = "black-21.7b0.tar.gz", hash = "sha256:c8373c6491de9362e39271630b65b964607bc5c79c83783547d76c839b3aa219"}, ] click = [ {file = "click-7.1.2-py2.py3-none-any.whl", hash = "sha256:dacca89f4bfadd5de3d7489b7c8a566eee0d3676333fbb50030263894c38c0dc"}, @@ -521,6 +553,10 @@ flake8 = [ {file = "flake8-3.9.2-py2.py3-none-any.whl", hash = "sha256:bf8fd333346d844f616e8d47905ef3a3384edae6b4e9beb0c5101e25e3110907"}, {file = "flake8-3.9.2.tar.gz", hash = "sha256:07528381786f2a6237b061f6e96610a4167b226cb926e2aa2b6b1d78057c576b"}, ] +flake8-polyfill = [ + {file = "flake8-polyfill-1.0.2.tar.gz", hash = "sha256:e44b087597f6da52ec6393a709e7108b2905317d0c0b744cdca6208e670d8eda"}, + {file = "flake8_polyfill-1.0.2-py2.py3-none-any.whl", hash = "sha256:12be6a34ee3ab795b19ca73505e7b55826d5f6ad7230d31b18e106400169b9e9"}, +] ghp-import = [ {file = "ghp-import-2.0.1.tar.gz", hash = "sha256:753de2eace6e0f7d4edfb3cce5e3c3b98cd52aadb80163303d1d036bda7b4483"}, ] @@ -629,6 +665,10 @@ pathspec = [ {file = "pathspec-0.8.1-py2.py3-none-any.whl", hash = "sha256:aa0cb481c4041bf52ffa7b0d8fa6cd3e88a2ca4879c533c9153882ee2556790d"}, {file = "pathspec-0.8.1.tar.gz", hash = "sha256:86379d6b86d75816baba717e64b1a3a3469deb93bb76d613c9ce79edc5cb68fd"}, ] +pep8-naming = [ + {file = "pep8-naming-0.12.1.tar.gz", hash = "sha256:bb2455947757d162aa4cad55dba4ce029005cd1692f2899a21d51d8630ca7841"}, + {file = "pep8_naming-0.12.1-py2.py3-none-any.whl", hash = "sha256:4a8daeaeb33cfcde779309fc0c9c0a68a3bbe2ad8a8308b763c5068f86eb9f37"}, +] pluggy = [ {file = "pluggy-0.13.1-py2.py3-none-any.whl", hash = "sha256:966c145cd83c96502c3c3868f50408687b38434af77734af1e9ca461a4081d2d"}, {file = "pluggy-0.13.1.tar.gz", hash = "sha256:15b2acde666561e1298d71b523007ed7364de07029219b604cf808bfa1c765b0"}, @@ -759,6 +799,10 @@ toml = [ {file = "toml-0.10.2-py2.py3-none-any.whl", hash = "sha256:806143ae5bfb6a3c6e736a764057db0e6a0e05e338b5630894a5f779cabb4f9b"}, {file = "toml-0.10.2.tar.gz", hash = "sha256:b3bda1d108d5dd99f4a20d24d9c348e91c4db7ab1b749200bded2f839ccbe68f"}, ] +tomli = [ + {file = "tomli-1.2.1-py3-none-any.whl", hash = "sha256:8dd0e9524d6f386271a36b41dbf6c57d8e32fd96fd22b6584679dc569d20899f"}, + {file = "tomli-1.2.1.tar.gz", hash = "sha256:a5b75cb6f3968abb47af1b40c1819dc519ea82bcc065776a866e8d74c5ca9442"}, +] verspec = [ {file = "verspec-0.1.0-py3-none-any.whl", hash = "sha256:741877d5633cc9464c45a469ae2a31e801e6dbbaa85b9675d481cda100f11c31"}, {file = "verspec-0.1.0.tar.gz", hash = "sha256:c4504ca697b2056cdb4bfa7121461f5a0e81809255b41c03dda4ba823637c01e"}, diff --git a/pyproject.toml b/pyproject.toml index 8e448a6d..593ea1fc 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -13,13 +13,14 @@ python-dateutil = "^2.8.1" [tool.poetry.dev-dependencies] flake8 = "^3.9.2" -black = "^21.5b1" +black = "^21.7b0" mkdocs = "^1.2.1" mkdocs-material = "^7.1.8" mdx-truly-sane-lists = "^1.2" click = "<7.2" GitPython = "^3.1.1" mike = "^1.0.1" +pep8-naming = "^0.12.1" [tool.black] line-length = 120