From da2817da2cee503cdc7f4bcc9595d7ecefae4a23 Mon Sep 17 00:00:00 2001 From: getzze Date: Mon, 25 Nov 2024 22:35:45 +0000 Subject: [PATCH] Use hatch and hatch-vcs (#1192) * use hatch and hatch-vcs * update github action to use hatch envs * add news * CI run doctest --- .github/workflows/CI.yaml | 56 ++++++++++------ changelog.d/1192.change.rst | 1 + hatch.toml | 128 ++++++++++++++++++++++++++++++++++++ pyproject.toml | 20 ++---- src/subliminal/__init__.py | 12 +++- 5 files changed, 179 insertions(+), 38 deletions(-) create mode 100644 changelog.d/1192.change.rst create mode 100644 hatch.toml diff --git a/.github/workflows/CI.yaml b/.github/workflows/CI.yaml index f3a4fd3f..71a27bbb 100644 --- a/.github/workflows/CI.yaml +++ b/.github/workflows/CI.yaml @@ -45,10 +45,10 @@ jobs: - uses: actions/setup-python@v5 with: python-version-file: .python-version-default - - name: Install tox-uv - run: python -Im pip install tox-uv + - name: Install hatch + run: python -Im pip install hatch - name: type-check - run: python -Im tox run -e mypy + run: hatch run types:run docs: name: Build docs and run doctests @@ -59,14 +59,18 @@ jobs: persist-credentials: false - uses: actions/setup-python@v5 with: - # Keep in sync with tox/docs and .readthedocs.yaml. + # Keep in sync with tox/docs, hatch.toml and .readthedocs.yaml. python-version: "3.12" cache: pip - - name: Install tox-uv - run: python -Im pip install tox-uv - - name: Build docs - run: python -Im tox run -e docs,changelog + - name: Install hatch + run: python -Im pip install hatch + - name: Build and check docs + run: hatch run docs:run + - name: Run doctests + run: hatch run docs:doctest + - name: Check changelog + run: hatch run changelog:run - uses: actions/upload-artifact@v4 with: @@ -118,32 +122,46 @@ jobs: matrix: os: [ubuntu-latest, macos-latest, windows-latest] # Created by the build-and-inspect-python-package action above. - python-version: ${{ fromJson(needs.build-package.outputs.supported-python-versions) }} + py: ${{ fromJson(needs.build-package.outputs.supported-python-versions) }} steps: - uses: actions/checkout@v4 with: persist-credentials: false - uses: actions/setup-python@v5 with: - python-version: ${{ matrix.python-version }} + python-version: ${{ matrix.py }} cache: 'pip' allow-prereleases: true - - name: Install tox-uv - run: python -Im pip install tox-uv + - name: Install hatch + run: python -Im pip install hatch - - name: Run tests + - name: Pick environment to run + env: + PYTHON_VERSION: ${{ matrix.py }} + run: | + import codecs; import os + py = f"tests.py{os.environ.get('PYTHON_VERSION')}" + print(f"Picked {py}") + with codecs.open(os.environ["GITHUB_ENV"], mode="a", encoding="utf-8") as file_handler: + file_handler.write(f"FORCE_COLOR=1\nENV={py}\n") + shell: python + - name: Setup test environment + run: | + hatch -v env create ${ENV} + shell: bash + - name: Run test suite env: # Make sure to add `passenv=COVERAGE_FILE` to `[testenv]` in tox.ini - COVERAGE_FILE: ".coverage.${{ matrix.os }}.${{ matrix.python-version }}" - run: >- - uvx --with=tox-uv - tox run - -e py${{ matrix.python-version }}-coverage + # Make sure to add `overrides = { env.COVERAGE_FILE.env-vars = "COVERAGE_FILE" }` to `[tests]` in hatch.toml + COVERAGE_FILE: ".coverage.${{ matrix.os }}.${{ matrix.py }}" + CI_RUN: "yes" + run: hatch -v run ${ENV}:test-cov + shell: bash - name: Store coverage file uses: actions/upload-artifact@v4 with: - name: coverage-${{ matrix.os }}-${{ matrix.python-version }} + name: coverage-${{ matrix.os }}-${{ matrix.py }} path: .coverage* include-hidden-files: true if-no-files-found: error diff --git a/changelog.d/1192.change.rst b/changelog.d/1192.change.rst new file mode 100644 index 00000000..9e334f4b --- /dev/null +++ b/changelog.d/1192.change.rst @@ -0,0 +1 @@ +Use hatch builder and hatch-vcs diff --git a/hatch.toml b/hatch.toml new file mode 100644 index 00000000..0196eee7 --- /dev/null +++ b/hatch.toml @@ -0,0 +1,128 @@ +# https://hatch.pypa.io/latest/config/environment/overview/ +# --------------------------------------------------------- +[envs.default] +description = "Development environment" +installer = "uv pip install" +features = [ + "tests", + "types", + "docs", + "dev", +] + +# --------------------------------------------------------- +[envs.pkg] +description = "package information" +features = [ + "tests", + "dev", +] +[envs.pkg.scripts] +show = [ + "uv pip list --format=columns", + 'python -c "import sys; print(sys.version); print(sys.executable)"', +] + +# --------------------------------------------------------- +[envs.lint] +template = "lint" +installer = "uv pip install" +description = "lint and format" +detached = true +dependencies = ["pre-commit"] + +[envs.lint.scripts] +run = "pre-commit run --all-files --show-diff-on-failure" + +# --------------------------------------------------------- +[envs.types] +template = "types" +installer = "uv pip install" +description = "Run the type checker" +dev-mode = false +features = ["tests", "types"] + +[envs.types.scripts] +run = "mypy --install-types --non-interactive --ignore-missing-imports --config-file={root}/pyproject.toml {args:src tests}" + +# --------------------------------------------------------- +[envs.docs] +template = "docs" +installer = "uv pip install" +description = "build and check documentation" +features = ["docs"] +# Keep in sync with CI.yaml/docs, tox/docs and .readthedocs.yaml. +python = "3.12" + +[envs.docs.scripts] +build = "sphinx-build -W --keep-going --color -b html docs docs/_build" +linkcheck = "sphinx-build -W --keep-going --color -b linkcheck docs docs/_build" +doctest = "sphinx-build -W --keep-going --color -b doctest docs docs/_build" +run = ["build", "linkcheck"] +all = ["build", "linkcheck", "doctest"] + +# --------------------------------------------------------- +[envs.changelog] +template = "changelog" +installer = "uv pip install" +description = "build changelog with towncrier" +dev-mode = false +dependencies = ["towncrier"] + +[envs.changelog.scripts] +run = "towncrier build --version main --draft" + +# --------------------------------------------------------- +[envs.tests] +template = "tests" +#installer = "uv pip install" +description = "Run the tests suite" +dev-mode = false +features = ["tests"] + +[[envs.tests.matrix]] +python = ["3.9", "3.10", "3.11", "3.12", "3.13"] + +[envs.tests.env-vars] +COVERAGE_PROCESS_START = "pyproject.toml" +COVERAGE_FILE = "report/.coverage.{matrix:python}" + +[envs.tests.overrides] +# To allow environment variable overwrite +env.COVERAGE_FILE.env-vars = "COVERAGE_FILE" +env.COVERAGE_PROCESS_START.env-vars = "COVERAGE_PROCESS_START" + +[envs.tests.scripts] +run = "pytest {args:-n auto}" +test-cov = "python -m pytest --cov=subliminal --cov-report= --cov-fail-under=0 {args:-n auto}" +test-cov-core = "python -m pytest -m core --cov=subliminal --cov-report= --cov-fail-under=0 {args:-n auto}" +run-cov = [ + "test-cov", + "coverage report --skip-covered --show-missing --fail-under=80", +] +run-cov-core = [ + "test-cov-core", + """\ + coverage report --skip-covered --show-missing --fail-under=100 \ + --omit='src/subliminal/cli.py,src/subliminal/converters/*,src/subliminal/providers/*,src/subliminal/refiners/*' \ + """, +] + +# --------------------------------------------------------- +[envs.coverage] +template = "coverage" +installer = "uv pip install" +description = "combine coverage files" +detached = true +dependencies = [ + "coverage[toml]>=7.3.2", +] +env-vars = { COVERAGE_FILE = "report/.coverage" } +# To allow environment variable overwrite +overrides = { env.COVERAGE_FILE.env-vars = "COVERAGE_FILE" } + +[envs.coverage.scripts] +run = [ + "- coverage combine report", + "coverage report --sort=-Cover --show-missing --skip-covered --skip-empty", +] diff --git a/pyproject.toml b/pyproject.toml index 25061877..6f63d647 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,7 +1,7 @@ # https://packaging.python.org/en/latest/specifications/pyproject-toml/ [build-system] -requires = ["setuptools>=64"] -build-backend = "setuptools.build_meta" +requires = ["hatchling", "hatch-vcs"] +build-backend = "hatchling.build" # https://peps.python.org/pep-0621/ [project] @@ -117,21 +117,9 @@ opensubtitlescom = "subliminal.converters.opensubtitlescom:OpenSubtitlesComConve subtitulamos = "subliminal.converters.subtitulamos:SubtitulamosConverter" tvsubtitles = "subliminal.converters.tvsubtitles:TVsubtitlesConverter" -[tool.setuptools] -py-modules = ["subliminal"] -include-package-data = true -[tool.setuptools.package-data] -subliminal = [ - "py.typed", -] - -[tool.setuptools.packages.find] -namespaces = false -where = ["src"] - -[tool.setuptools.dynamic] -version = {attr = "subliminal.__version__"} +[tool.hatch.version] +source = "vcs" # https://docs.pytest.org/en/6.2.x/customize.html diff --git a/src/subliminal/__init__.py b/src/subliminal/__init__.py index be2f8027..972a4670 100644 --- a/src/subliminal/__init__.py +++ b/src/subliminal/__init__.py @@ -2,14 +2,20 @@ from __future__ import annotations -__title__: str = 'subliminal' -__version__: str = '2.2.1' +import logging +from importlib.metadata import PackageNotFoundError, version + +# Must be first, otherwise we run into ImportError: partially initialized module +try: + __version__ = version('subliminal') +except PackageNotFoundError: + __version__ = 'undefined' __short_version__: str = '.'.join(__version__.split('.')[:2]) +__title__: str = 'subliminal' __author__: str = 'Antoine Bertin' __license__: str = 'MIT' __copyright__: str = 'Copyright 2016, Antoine Bertin' -import logging from .cache import region from .core import (