Skip to content

move version into pyproject.toml #640

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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__
6 changes: 6 additions & 0 deletions src/towncrier/_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
from __future__ import annotations

import contextlib
import importlib
import sys

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

Expand Down Expand Up @@ -67,6 +69,10 @@ def get_version(package_dir: str, package: str) -> str:
if version := _get_metadata_version(package):
return version

with contextlib.suppress(PackageNotFoundError):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this code?

What does it to, on top of the existing _get_metadata_version ?

Maybe move this to _get_metadata_version

If this needs to stay here, then it needs a comment to explain why _get_metadata_version is not good enough here and why we get the version again via `importlib.metadata.

Copy link
Contributor Author

@sigma67 sigma67 Aug 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test test_version_from_metadata will fail without this code.

The existing function _get_metadata_version_ uses importlib.metadata.packages_distributions, which seems to not contain towncrier at test runtime. Not sure why, but I also don't know why this method of getting the version is used instead of straight up passing package to importlib.metadata.version.

I can move the check into the function, maybe inside the if not distribution_names block. Not sure.

What do you think?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that _get_metadata_version should do everything.

So the code should be moved there.

Maybe also remove importlib.metadata.packages_distributions and replace it with the current method.

Do we need both methods? Just asking.

I am not very familiar with the importlib API ... but as long as the tests pass, we should be ok.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I thought there might be some reason why it was done this way.

I replaced it as you suggested.

if version := importlib.metadata.version(package):
return version

# When no version if found, fall back to looking for the package in `package_dir`.
module = _get_package(package_dir, package)
version = getattr(module, "__version__", None)
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.

3 changes: 3 additions & 0 deletions src/towncrier/newsfragments/640.misc
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

Removed deprecated __version__ attribute from towncrier/__init__.py
26 changes: 0 additions & 26 deletions src/towncrier/test/test_packaging.py

This file was deleted.

Loading