Skip to content

Commit da2817d

Browse files
authored
Use hatch and hatch-vcs (#1192)
* use hatch and hatch-vcs * update github action to use hatch envs * add news * CI run doctest
1 parent 3e81603 commit da2817d

File tree

5 files changed

+179
-38
lines changed

5 files changed

+179
-38
lines changed

.github/workflows/CI.yaml

+37-19
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ jobs:
4545
- uses: actions/setup-python@v5
4646
with:
4747
python-version-file: .python-version-default
48-
- name: Install tox-uv
49-
run: python -Im pip install tox-uv
48+
- name: Install hatch
49+
run: python -Im pip install hatch
5050
- name: type-check
51-
run: python -Im tox run -e mypy
51+
run: hatch run types:run
5252

5353
docs:
5454
name: Build docs and run doctests
@@ -59,14 +59,18 @@ jobs:
5959
persist-credentials: false
6060
- uses: actions/setup-python@v5
6161
with:
62-
# Keep in sync with tox/docs and .readthedocs.yaml.
62+
# Keep in sync with tox/docs, hatch.toml and .readthedocs.yaml.
6363
python-version: "3.12"
6464
cache: pip
6565

66-
- name: Install tox-uv
67-
run: python -Im pip install tox-uv
68-
- name: Build docs
69-
run: python -Im tox run -e docs,changelog
66+
- name: Install hatch
67+
run: python -Im pip install hatch
68+
- name: Build and check docs
69+
run: hatch run docs:run
70+
- name: Run doctests
71+
run: hatch run docs:doctest
72+
- name: Check changelog
73+
run: hatch run changelog:run
7074

7175
- uses: actions/upload-artifact@v4
7276
with:
@@ -118,32 +122,46 @@ jobs:
118122
matrix:
119123
os: [ubuntu-latest, macos-latest, windows-latest]
120124
# Created by the build-and-inspect-python-package action above.
121-
python-version: ${{ fromJson(needs.build-package.outputs.supported-python-versions) }}
125+
py: ${{ fromJson(needs.build-package.outputs.supported-python-versions) }}
122126
steps:
123127
- uses: actions/checkout@v4
124128
with:
125129
persist-credentials: false
126130
- uses: actions/setup-python@v5
127131
with:
128-
python-version: ${{ matrix.python-version }}
132+
python-version: ${{ matrix.py }}
129133
cache: 'pip'
130134
allow-prereleases: true
131-
- name: Install tox-uv
132-
run: python -Im pip install tox-uv
135+
- name: Install hatch
136+
run: python -Im pip install hatch
133137

134-
- name: Run tests
138+
- name: Pick environment to run
139+
env:
140+
PYTHON_VERSION: ${{ matrix.py }}
141+
run: |
142+
import codecs; import os
143+
py = f"tests.py{os.environ.get('PYTHON_VERSION')}"
144+
print(f"Picked {py}")
145+
with codecs.open(os.environ["GITHUB_ENV"], mode="a", encoding="utf-8") as file_handler:
146+
file_handler.write(f"FORCE_COLOR=1\nENV={py}\n")
147+
shell: python
148+
- name: Setup test environment
149+
run: |
150+
hatch -v env create ${ENV}
151+
shell: bash
152+
- name: Run test suite
135153
env:
136154
# Make sure to add `passenv=COVERAGE_FILE` to `[testenv]` in tox.ini
137-
COVERAGE_FILE: ".coverage.${{ matrix.os }}.${{ matrix.python-version }}"
138-
run: >-
139-
uvx --with=tox-uv
140-
tox run
141-
-e py${{ matrix.python-version }}-coverage
155+
# Make sure to add `overrides = { env.COVERAGE_FILE.env-vars = "COVERAGE_FILE" }` to `[tests]` in hatch.toml
156+
COVERAGE_FILE: ".coverage.${{ matrix.os }}.${{ matrix.py }}"
157+
CI_RUN: "yes"
158+
run: hatch -v run ${ENV}:test-cov
159+
shell: bash
142160

143161
- name: Store coverage file
144162
uses: actions/upload-artifact@v4
145163
with:
146-
name: coverage-${{ matrix.os }}-${{ matrix.python-version }}
164+
name: coverage-${{ matrix.os }}-${{ matrix.py }}
147165
path: .coverage*
148166
include-hidden-files: true
149167
if-no-files-found: error

changelog.d/1192.change.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Use hatch builder and hatch-vcs

hatch.toml

+128
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
# https://hatch.pypa.io/latest/config/environment/overview/
2+
# ---------------------------------------------------------
3+
[envs.default]
4+
description = "Development environment"
5+
installer = "uv pip install"
6+
features = [
7+
"tests",
8+
"types",
9+
"docs",
10+
"dev",
11+
]
12+
13+
# ---------------------------------------------------------
14+
[envs.pkg]
15+
description = "package information"
16+
features = [
17+
"tests",
18+
"dev",
19+
]
20+
[envs.pkg.scripts]
21+
show = [
22+
"uv pip list --format=columns",
23+
'python -c "import sys; print(sys.version); print(sys.executable)"',
24+
]
25+
26+
# ---------------------------------------------------------
27+
[envs.lint]
28+
template = "lint"
29+
installer = "uv pip install"
30+
description = "lint and format"
31+
detached = true
32+
dependencies = ["pre-commit"]
33+
34+
[envs.lint.scripts]
35+
run = "pre-commit run --all-files --show-diff-on-failure"
36+
37+
# ---------------------------------------------------------
38+
[envs.types]
39+
template = "types"
40+
installer = "uv pip install"
41+
description = "Run the type checker"
42+
dev-mode = false
43+
features = ["tests", "types"]
44+
45+
[envs.types.scripts]
46+
run = "mypy --install-types --non-interactive --ignore-missing-imports --config-file={root}/pyproject.toml {args:src tests}"
47+
48+
# ---------------------------------------------------------
49+
[envs.docs]
50+
template = "docs"
51+
installer = "uv pip install"
52+
description = "build and check documentation"
53+
features = ["docs"]
54+
# Keep in sync with CI.yaml/docs, tox/docs and .readthedocs.yaml.
55+
python = "3.12"
56+
57+
[envs.docs.scripts]
58+
build = "sphinx-build -W --keep-going --color -b html docs docs/_build"
59+
linkcheck = "sphinx-build -W --keep-going --color -b linkcheck docs docs/_build"
60+
doctest = "sphinx-build -W --keep-going --color -b doctest docs docs/_build"
61+
run = ["build", "linkcheck"]
62+
all = ["build", "linkcheck", "doctest"]
63+
64+
# ---------------------------------------------------------
65+
[envs.changelog]
66+
template = "changelog"
67+
installer = "uv pip install"
68+
description = "build changelog with towncrier"
69+
dev-mode = false
70+
dependencies = ["towncrier"]
71+
72+
[envs.changelog.scripts]
73+
run = "towncrier build --version main --draft"
74+
75+
# ---------------------------------------------------------
76+
[envs.tests]
77+
template = "tests"
78+
#installer = "uv pip install"
79+
description = "Run the tests suite"
80+
dev-mode = false
81+
features = ["tests"]
82+
83+
[[envs.tests.matrix]]
84+
python = ["3.9", "3.10", "3.11", "3.12", "3.13"]
85+
86+
[envs.tests.env-vars]
87+
COVERAGE_PROCESS_START = "pyproject.toml"
88+
COVERAGE_FILE = "report/.coverage.{matrix:python}"
89+
90+
[envs.tests.overrides]
91+
# To allow environment variable overwrite
92+
env.COVERAGE_FILE.env-vars = "COVERAGE_FILE"
93+
env.COVERAGE_PROCESS_START.env-vars = "COVERAGE_PROCESS_START"
94+
95+
[envs.tests.scripts]
96+
run = "pytest {args:-n auto}"
97+
test-cov = "python -m pytest --cov=subliminal --cov-report= --cov-fail-under=0 {args:-n auto}"
98+
test-cov-core = "python -m pytest -m core --cov=subliminal --cov-report= --cov-fail-under=0 {args:-n auto}"
99+
run-cov = [
100+
"test-cov",
101+
"coverage report --skip-covered --show-missing --fail-under=80",
102+
]
103+
run-cov-core = [
104+
"test-cov-core",
105+
"""\
106+
coverage report --skip-covered --show-missing --fail-under=100 \
107+
--omit='src/subliminal/cli.py,src/subliminal/converters/*,src/subliminal/providers/*,src/subliminal/refiners/*' \
108+
""",
109+
]
110+
111+
# ---------------------------------------------------------
112+
[envs.coverage]
113+
template = "coverage"
114+
installer = "uv pip install"
115+
description = "combine coverage files"
116+
detached = true
117+
dependencies = [
118+
"coverage[toml]>=7.3.2",
119+
]
120+
env-vars = { COVERAGE_FILE = "report/.coverage" }
121+
# To allow environment variable overwrite
122+
overrides = { env.COVERAGE_FILE.env-vars = "COVERAGE_FILE" }
123+
124+
[envs.coverage.scripts]
125+
run = [
126+
"- coverage combine report",
127+
"coverage report --sort=-Cover --show-missing --skip-covered --skip-empty",
128+
]

pyproject.toml

+4-16
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# https://packaging.python.org/en/latest/specifications/pyproject-toml/
22
[build-system]
3-
requires = ["setuptools>=64"]
4-
build-backend = "setuptools.build_meta"
3+
requires = ["hatchling", "hatch-vcs"]
4+
build-backend = "hatchling.build"
55

66
# https://peps.python.org/pep-0621/
77
[project]
@@ -117,21 +117,9 @@ opensubtitlescom = "subliminal.converters.opensubtitlescom:OpenSubtitlesComConve
117117
subtitulamos = "subliminal.converters.subtitulamos:SubtitulamosConverter"
118118
tvsubtitles = "subliminal.converters.tvsubtitles:TVsubtitlesConverter"
119119

120-
[tool.setuptools]
121-
py-modules = ["subliminal"]
122-
include-package-data = true
123120

124-
[tool.setuptools.package-data]
125-
subliminal = [
126-
"py.typed",
127-
]
128-
129-
[tool.setuptools.packages.find]
130-
namespaces = false
131-
where = ["src"]
132-
133-
[tool.setuptools.dynamic]
134-
version = {attr = "subliminal.__version__"}
121+
[tool.hatch.version]
122+
source = "vcs"
135123

136124

137125
# https://docs.pytest.org/en/6.2.x/customize.html

src/subliminal/__init__.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,20 @@
22

33
from __future__ import annotations
44

5-
__title__: str = 'subliminal'
6-
__version__: str = '2.2.1'
5+
import logging
6+
from importlib.metadata import PackageNotFoundError, version
7+
8+
# Must be first, otherwise we run into ImportError: partially initialized module
9+
try:
10+
__version__ = version('subliminal')
11+
except PackageNotFoundError:
12+
__version__ = 'undefined'
713
__short_version__: str = '.'.join(__version__.split('.')[:2])
14+
__title__: str = 'subliminal'
815
__author__: str = 'Antoine Bertin'
916
__license__: str = 'MIT'
1017
__copyright__: str = 'Copyright 2016, Antoine Bertin'
1118

12-
import logging
1319

1420
from .cache import region
1521
from .core import (

0 commit comments

Comments
 (0)