Skip to content

Commit 3dddb91

Browse files
authored
Check pyproject requirements before running unit tests (#169)
* Check python version requirements in pyproject.toml * Fix * Fix * Fix * Fix * Refactor * Roll back * Fix * Fix
1 parent ddddcce commit 3dddb91

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

.github/workflows/python_unit_tests.yaml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,39 @@ jobs:
2828
with:
2929
python-version: ${{ matrix.python-version }}
3030

31+
- name: Install Poetry for version checking
32+
run: |
33+
pip install poetry
34+
35+
- name: Check Python version against project requirements
36+
shell: python
37+
run: |
38+
import sys
39+
from contextlib import suppress
40+
from poetry.core.constraints.version import parse_constraint
41+
42+
try:
43+
import tomllib
44+
except ImportError:
45+
import tomli as tomllib
46+
47+
config = tomllib.load(open('pyproject.toml', 'rb'))
48+
constraints = []
49+
50+
with suppress(KeyError):
51+
constraints.append(parse_constraint(config['tool']['poetry']['dependencies']['python']))
52+
with suppress(KeyError):
53+
constraints.append(parse_constraint(config['project']['requires-python']))
54+
55+
python_version = parse_constraint('${{ matrix.python-version }}')
56+
if not all(con.allows(python_version) for con in constraints):
57+
print(f'Python version {str(python_version)} is not allowed by project settings', file=sys.stderr)
58+
sys.exit(1)
59+
60+
- name: Uninstall global poetry again
61+
run: |
62+
pip uninstall -y poetry
63+
3164
- name: Install Python dependencies
3265
run: |
3366
pipx install --python ${{ matrix.python-version }} poetry

0 commit comments

Comments
 (0)