Skip to content

Commit

Permalink
move version into pyproject.toml (#640)
Browse files Browse the repository at this point in the history
  • Loading branch information
adiroiban authored Aug 1, 2024
2 parents bc9fb41 + 040bafe commit fc3a255
Show file tree
Hide file tree
Showing 10 changed files with 26 additions and 91 deletions.
12 changes: 6 additions & 6 deletions RELEASE.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ The same branch is used for the release candidate and the final release.
In the end, the release branch is merged into the main branch.

Update the version to the release candidate with the first being ``rc1`` (as opposed to 0).
In ``src/towncrier/_version.py`` the version is set using a PEP440 compliant string:
In ``pyproject.toml`` the version is set using a PEP440 compliant string:

__version__ = "19.9.0rc1"
version = "19.9.0rc1"

Run ``venv/bin/towncrier build --yes`` to generate the news release NEWS file.
Commit and push to the primary repository, not a fork.
Expand Down Expand Up @@ -84,9 +84,9 @@ Final release
Once the PR is approved, you can trigger the final release.

Update the version to the final version.
In ``src/towncrier/_version.py`` the version is set using a PEP440 compliant string:
In ``pyproject.toml`` the version is set using a PEP440 compliant string:

__version__ = "19.9.0"
version = "19.9.0"

Manually update the `NEWS.rst` file to include the final release version and date.
Usually it will look like this.
Expand Down Expand Up @@ -115,9 +115,9 @@ Similar to the release candidate, with the difference:
No need for another review request.

Update the version to the development version.
In ``src/towncrier/_version.py`` the version is set using a PEP440 compliant string:
In ``pyproject.toml`` the version is set using a PEP440 compliant string:

__version__ = "19.9.0.dev0"
version = "19.9.0.dev0"

Commit and push the changes.

Expand Down
4 changes: 3 additions & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@
import os

from datetime import date
from importlib.metadata import version

from towncrier import __version__ as towncrier_version

towncrier_version = version("towncrier")


extensions = []
Expand Down
12 changes: 5 additions & 7 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ build-backend = "hatchling.build"


[project]
dynamic = ["version"]
name = "towncrier"
# For dev - 23.11.0.dev0
# For RC - 23.11.0rc1 (release candidate starts at 1)
# For final - 23.11.0
# make sure to follow PEP440
version = "24.7.2.dev0"
description = "Building newsfiles for your project."
readme = "README.rst"
license = "MIT"
Expand Down Expand Up @@ -55,12 +59,6 @@ Tests = "https://github.com/twisted/towncrier/actions?query=branch%3Atrunk"
Coverage = "https://codecov.io/gh/twisted/towncrier"
Distribution = "https://pypi.org/project/towncrier"


[tool.hatch.version]
source = "code"
path = "src/towncrier/_version.py"
expression = "_hatchling_version"

[tool.hatch.build]
exclude = [
"admin",
Expand Down
24 changes: 0 additions & 24 deletions src/towncrier/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,3 @@
"""
towncrier, a builder for your news files.
"""

from __future__ import annotations


__all__ = ["__version__"]


def __getattr__(name: str) -> str:
if name != "__version__":
raise AttributeError(f"module {__name__} has no attribute {name}")

import warnings

from ._version import __version__

warnings.warn(
"Accessing towncrier.__version__ is deprecated and will be "
"removed in a future release. Use importlib.metadata directly "
"to query for towncrier's packaging metadata.",
DeprecationWarning,
stacklevel=2,
)

return __version__
20 changes: 7 additions & 13 deletions src/towncrier/_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,14 @@
from __future__ import annotations

import contextlib
import importlib.metadata
import sys

from importlib import import_module
from importlib.metadata import version as metadata_version
from importlib.metadata import PackageNotFoundError
from types import ModuleType


if sys.version_info >= (3, 10):
from importlib.metadata import packages_distributions
else:
from importlib_metadata import packages_distributions # type: ignore


def _get_package(package_dir: str, package: str) -> ModuleType:
try:
module = import_module(package)
Expand All @@ -46,12 +41,11 @@ def _get_metadata_version(package: str) -> str | None:
"""
Try to get the version from the package metadata.
"""
distributions = packages_distributions()
distribution_names = distributions.get(package)
if not distribution_names or len(distribution_names) != 1:
# We can only determine the version if there is exactly one matching distribution.
return None
return metadata_version(distribution_names[0])
with contextlib.suppress(PackageNotFoundError):
if version := importlib.metadata.version(package):
return version

return None


def get_version(package_dir: str, package: str) -> str:
Expand Down
3 changes: 1 addition & 2 deletions src/towncrier/_shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@

import click

from ._version import __version__
from .build import _main as _build_cmd
from .check import _main as _check_cmd
from .click_default_group import DefaultGroup
from .create import _main as _create_cmd


@click.group(cls=DefaultGroup, default="build", default_if_no_args=True)
@click.version_option(__version__)
@click.version_option()
def cli() -> None:
"""
Towncrier is a utility to produce useful, summarised news files for your project.
Expand Down
12 changes: 0 additions & 12 deletions src/towncrier/_version.py

This file was deleted.

1 change: 1 addition & 0 deletions src/towncrier/newsfragments/640.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Use importlib.metadata to get the towncrier's version.
3 changes: 3 additions & 0 deletions src/towncrier/newsfragments/640.removal
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Moved towncrier version definition from src/towncrier/_version.py to pyproject.toml

towncrier.__version__ was removed, after being deprecated in 23.6.0.
26 changes: 0 additions & 26 deletions src/towncrier/test/test_packaging.py

This file was deleted.

0 comments on commit fc3a255

Please sign in to comment.