From 5f93a31aff2b7d755cdc02f1a0807f91b4cf92b6 Mon Sep 17 00:00:00 2001 From: Hynek Schlawack Date: Fri, 19 Jul 2024 10:56:10 +0200 Subject: [PATCH] Switch to build-and-inspect-python-package (#1307) * Switch to build-and-inspect-python-package Use the occasion to factor out PyPy jobs which simplifies everthing. * Only install pytest-codspeed when benchmarking * No need to build twice on PRs * No coverage on PyPy * Still require PyPy to pass * Actually remove codspeed from tests * Separate glacial CodSpeed into own workflow Might switch to on-demand/cron if we don't find a way for it to run under 5 minutes. * Get rid of version override * Add missing needs * Test should pass without src * oops * Make PyPy wheel-only, too * Clarify * Explain how the matrix works since everybody steals my stuff --- .github/workflows/ci.yml | 140 ++++++++++++++++++----------- .github/workflows/codspeed.yml | 36 ++++++++ .github/workflows/pypi-package.yml | 2 - .pre-commit-config.yaml | 2 +- pyproject.toml | 7 +- tox.ini | 2 +- 6 files changed, 131 insertions(+), 58 deletions(-) create mode 100644 .github/workflows/codspeed.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 0949b80c..e1f7693f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -10,36 +10,48 @@ on: workflow_dispatch: env: - FORCE_COLOR: "1" # Make tools pretty. + FORCE_COLOR: "1" PIP_DISABLE_PIP_VERSION_CHECK: "1" PIP_NO_PYTHON_VERSION_WARNING: "1" - # Use oldest version used in doctests / examples. - SETUPTOOLS_SCM_PRETEND_VERSION: "19.2.0" permissions: {} jobs: + build-package: + name: Build & verify package + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - uses: hynek/build-and-inspect-python-package@v2 + id: baipp + + outputs: + # Used to define the matrix for tests below. The value is based on + # packaging metadata (trove classifiers). + supported-python-versions: ${{ steps.baipp.outputs.supported_python_classifiers_json_array }} + tests: name: Tests & Mypy on ${{ matrix.python-version }} runs-on: ubuntu-latest + needs: build-package strategy: fail-fast: false matrix: - python-version: - - "3.7" - - "3.8" - - "3.9" - - "3.10" - - "3.11" - - "3.12" - - "3.13" - - "pypy-3.8" - - "pypy-3.9" - - "pypy-3.10" + # Created by the build-and-inspect-python-package action above. + python-version: ${{ fromJson(needs.build-package.outputs.supported-python-versions) }} steps: - - uses: actions/checkout@v4 + - name: Download pre-built packages + uses: actions/download-artifact@v4 + with: + name: Packages + path: dist + - run: tar xf dist/*.tar.gz --strip-components=1 - uses: actions/setup-python@v5 with: python-version: ${{ matrix.python-version }} @@ -51,33 +63,30 @@ jobs: V=${{ matrix.python-version }} DO_MYPY=1 - if [[ "$V" = pypy-* ]]; then + if [[ "$V" == "3.7" || "$V" == "3.8" ]]; then DO_MYPY=0 - - V=pypy3 - else - if [[ "$V" == "3.7" || "$V" == "3.8" ]]; then - DO_MYPY=0 - echo "skipping $V" - else - echo "not skipping $V" - fi - - V=py$(echo $V | tr -d .) fi echo DO_MYPY=$DO_MYPY >>$GITHUB_ENV - echo TOX_PYTHON=$V >>$GITHUB_ENV + echo TOX_PYTHON=py$(echo $V | tr -d .) >>$GITHUB_ENV python -Im pip install tox - run: python -Im pip install tox-uv if: matrix.python-version != '3.7' - - run: python -Im tox run -e ${{ env.TOX_PYTHON }}-tests - run: python -Im tox run -e ${{ env.TOX_PYTHON }}-mypy if: env.DO_MYPY == '1' + - name: Remove src to ensure tests run against wheel + run: rm -rf src + + - run: > + python -Im + tox run + --installpkg dist/*.whl + -e ${{ env.TOX_PYTHON }}-tests + - name: Upload coverage data uses: actions/upload-artifact@v4 with: @@ -85,13 +94,54 @@ jobs: path: .coverage.* if-no-files-found: ignore + tests-pypy: + name: Tests on ${{ matrix.python-version }} + runs-on: ubuntu-latest + needs: build-package + + strategy: + fail-fast: false + matrix: + python-version: + - pypy-3.8 + - pypy-3.9 + - pypy-3.10 + + steps: + - name: Download pre-built packages + uses: actions/download-artifact@v4 + with: + name: Packages + path: dist + - run: | + tar xf dist/*.tar.gz --strip-components=1 + rm -rf src # ensure tests run against wheel + - uses: actions/setup-python@v5 + with: + python-version: ${{ matrix.python-version }} + allow-prereleases: true + cache: pip + + - run: python -Im pip install tox-uv + + - run: > + python -Im + tox run + --installpkg dist/*.whl + -e pypy3-tests + coverage: name: Combine & check coverage. runs-on: ubuntu-latest needs: tests steps: - - uses: actions/checkout@v4 + - name: Download pre-built packages + uses: actions/download-artifact@v4 + with: + name: Packages + path: dist + - run: tar xf dist/*.tar.gz --strip-components=1 - uses: actions/setup-python@v5 with: python-version-file: .python-version-default @@ -123,32 +173,17 @@ jobs: path: htmlcov if: ${{ failure() }} - - codspeed: - name: Run CodSpeed benchmarks - runs-on: ubuntu-latest - if: github.event_name != 'merge_group' - - steps: - - uses: actions/checkout@v4 - - uses: actions/setup-python@v5 - with: - python-version-file: .python-version-default - cache: pip - - run: python -Im pip install tox-uv - - - name: Run CodSpeed benchmarks - uses: CodSpeedHQ/action@v2 - with: - token: ${{ secrets.CODSPEED_TOKEN }} - run: tox run -e codspeed - - docs: name: Build docs & run doctests runs-on: ubuntu-latest + needs: build-package steps: - - uses: actions/checkout@v4 + - name: Download pre-built packages + uses: actions/download-artifact@v4 + with: + name: Packages + path: dist + - run: tar xf dist/*.tar.gz --strip-components=1 - uses: actions/setup-python@v5 with: # Keep in sync with tox/docs and .readthedocs.yaml. @@ -197,6 +232,7 @@ jobs: needs: - coverage + - tests-pypy - docs - install-dev - pyright diff --git a/.github/workflows/codspeed.yml b/.github/workflows/codspeed.yml new file mode 100644 index 00000000..c425a727 --- /dev/null +++ b/.github/workflows/codspeed.yml @@ -0,0 +1,36 @@ +--- +name: CodSpeed Benchmarks + +on: + push: + branches: [main] + tags: ["*"] + pull_request: + workflow_dispatch: + + +env: + FORCE_COLOR: "1" + PIP_DISABLE_PIP_VERSION_CHECK: "1" + PIP_NO_PYTHON_VERSION_WARNING: "1" + +permissions: {} + +jobs: + codspeed: + name: Run CodSpeed benchmarks + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-python@v5 + with: + python-version-file: .python-version-default + cache: pip + - run: python -Im pip install tox-uv + + - name: Run CodSpeed benchmarks + uses: CodSpeedHQ/action@v2 + with: + token: ${{ secrets.CODSPEED_TOKEN }} + run: tox run -e codspeed diff --git a/.github/workflows/pypi-package.yml b/.github/workflows/pypi-package.yml index 8495480c..ea35e48f 100644 --- a/.github/workflows/pypi-package.yml +++ b/.github/workflows/pypi-package.yml @@ -5,8 +5,6 @@ on: push: branches: [main] tags: ["*"] - pull_request: - branches: [main] release: types: - published diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index c498e0f0..dd0f3506 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -9,7 +9,7 @@ repos: - id: black - repo: https://github.com/astral-sh/ruff-pre-commit - rev: v0.5.2 + rev: v0.5.3 hooks: - id: ruff args: [--fix, --exit-non-zero-on-fix] diff --git a/pyproject.toml b/pyproject.toml index 719c7f3d..8511feb0 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -43,8 +43,6 @@ tests = [ "pympler", # 4.3.0 dropped last use of `convert` "pytest>=4.3.0", - # CodSpeed needs CFFI, and CFFI doesn't speak 3.13. - 'pytest-codspeed; python_version < "3.13"', "pytest-xdist[psutil]", "attrs[tests-mypy]", ] @@ -53,6 +51,11 @@ cov = [ # Ensure coverage is new enough for `source_pkgs`. "coverage[toml]>=5.3", ] +benchmark = [ + "pytest-codspeed", + "pytest-xdist[psutil]", + "attrs[tests]", +] docs = [ "cogapp", "furo", diff --git a/tox.ini b/tox.ini index 039dcf51..6508998a 100644 --- a/tox.ini +++ b/tox.ini @@ -49,7 +49,7 @@ commands = [testenv:codspeed] -extras = tests +extras = benchmark pass_env = CODSPEED_TOKEN CODSPEED_ENV