From 22b5eb45e74deef15e8220cb8b23187e1efbe70a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Josef=20Proch=C3=A1zka?= Date: Tue, 18 Feb 2025 08:59:24 +0100 Subject: [PATCH] Poetry enforce specific Python version (#173) * Simplify poetry using correct python version * Make sure that exactly same Python version is used as for poetry * Set same Python version enforcement for lint job as well * Do for all related workflows * Update python_docs_check.yaml * Add comment explaining the change --- .../workflows/python_integration_tests.yaml | 4 +++ .github/workflows/python_lint_check.yaml | 4 +++ .github/workflows/python_type_check.yaml | 4 +++ .github/workflows/python_unit_tests.yaml | 33 +++---------------- 4 files changed, 16 insertions(+), 29 deletions(-) diff --git a/.github/workflows/python_integration_tests.yaml b/.github/workflows/python_integration_tests.yaml index bd51cce..9c08445 100644 --- a/.github/workflows/python_integration_tests.yaml +++ b/.github/workflows/python_integration_tests.yaml @@ -80,6 +80,10 @@ jobs: - name: Install Python dependencies run: | pipx install --python ${{ matrix.python-version }} poetry + # This forces poetry to use specific Python version when installing a project. + # Raises error, if this Python version does not match project specific Python constraints. + poetry config virtualenvs.use-poetry-python true + poetry env use ${{ matrix.python-version }} make install-dev - name: Run integration tests diff --git a/.github/workflows/python_lint_check.yaml b/.github/workflows/python_lint_check.yaml index a41c567..73e5f90 100644 --- a/.github/workflows/python_lint_check.yaml +++ b/.github/workflows/python_lint_check.yaml @@ -23,6 +23,10 @@ jobs: - name: Install Python dependencies run: | pipx install --python ${{ matrix.python-version }} poetry + # This forces poetry to use specific Python version when installing a project. + # Raises error, if this Python version does not match project specific Python constraints. + poetry config virtualenvs.use-poetry-python true + poetry env use ${{ matrix.python-version }} make install-dev - name: Run lint check diff --git a/.github/workflows/python_type_check.yaml b/.github/workflows/python_type_check.yaml index b4ec2db..7813812 100644 --- a/.github/workflows/python_type_check.yaml +++ b/.github/workflows/python_type_check.yaml @@ -23,6 +23,10 @@ jobs: - name: Install Python dependencies run: | pipx install --python ${{ matrix.python-version }} poetry + # This forces poetry to use specific Python version when installing a project. + # Raises error, if this Python version does not match project specific Python constraints. + poetry config virtualenvs.use-poetry-python true + poetry env use ${{ matrix.python-version }} make install-dev - name: Run type check diff --git a/.github/workflows/python_unit_tests.yaml b/.github/workflows/python_unit_tests.yaml index 1b62d77..9c3f065 100644 --- a/.github/workflows/python_unit_tests.yaml +++ b/.github/workflows/python_unit_tests.yaml @@ -32,38 +32,13 @@ jobs: run: | pip install poetry - - name: Check Python version against project requirements - shell: python - run: | - import sys - from contextlib import suppress - from poetry.core.constraints.version import parse_constraint - - try: - import tomllib - except ImportError: - import tomli as tomllib - - config = tomllib.load(open('pyproject.toml', 'rb')) - constraints = [] - - with suppress(KeyError): - constraints.append(parse_constraint(config['tool']['poetry']['dependencies']['python'])) - with suppress(KeyError): - constraints.append(parse_constraint(config['project']['requires-python'])) - - python_version = parse_constraint('${{ matrix.python-version }}') - if not all(con.allows(python_version) for con in constraints): - print(f'Python version {str(python_version)} is not allowed by project settings', file=sys.stderr) - sys.exit(1) - - - name: Uninstall global poetry again - run: | - pip uninstall -y poetry - - name: Install Python dependencies run: | pipx install --python ${{ matrix.python-version }} poetry + # This forces poetry to use specific Python version when installing a project. + # Raises error, if this Python version does not match project specific Python constraints. + poetry config virtualenvs.use-poetry-python true + poetry env use ${{ matrix.python-version }} make install-dev - name: Run unit tests