diff --git a/src/towncrier/__init__.py b/src/towncrier/__init__.py index 0c504e1f..a7578e17 100644 --- a/src/towncrier/__init__.py +++ b/src/towncrier/__init__.py @@ -4,3 +4,27 @@ """ 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__ diff --git a/src/towncrier/_project.py b/src/towncrier/_project.py index 3489e123..3fe6fa94 100644 --- a/src/towncrier/_project.py +++ b/src/towncrier/_project.py @@ -7,13 +7,13 @@ from __future__ import annotations +import contextlib import importlib.metadata as importlib_metadata import sys from importlib import import_module from importlib.metadata import version as metadata_version from types import ModuleType -from typing import Any if sys.version_info >= (3, 10): @@ -62,7 +62,7 @@ def get_version(package_dir: str, package: str) -> str: Try to extract the version from the distribution version metadata that matches `package`, then fall back to looking for the package in `package_dir`. """ - version: Any + version: str # First try to get the version from the package metadata. if version := _get_metadata_version(package): @@ -105,9 +105,7 @@ def get_project_name(package_dir: str, package: str) -> str: module = _get_package(package_dir, package) version = getattr(module, "__version__", None) # Incremental has support for package names, try duck-typing it. - try: + with contextlib.suppress(AttributeError): return str(version.package) # type: ignore - except AttributeError: - pass return package.title() diff --git a/src/towncrier/_shell.py b/src/towncrier/_shell.py index 24642e3e..3e36e5cc 100644 --- a/src/towncrier/_shell.py +++ b/src/towncrier/_shell.py @@ -13,10 +13,9 @@ import click -from click_default_group import DefaultGroup - 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 diff --git a/src/towncrier/_version.py b/src/towncrier/_version.py index a0a1263f..93736bae 100644 --- a/src/towncrier/_version.py +++ b/src/towncrier/_version.py @@ -2,13 +2,11 @@ Provides towncrier version information. """ -from importlib.metadata import PackageNotFoundError, version +# For dev - 23.11.1.dev0 +# For RC - 23.11.1.rc1 +# For final - 23.11.1 +# make sure to follow PEP440 +__version__ = "23.11.1.dev0" - -try: - _version = version("towncrier") -except PackageNotFoundError: # pragma: no cover - _version = "0.0.0.dev" - -_hatchling_version = _version +_hatchling_version = __version__ __all__ = ["_hatchling_version"] diff --git a/src/towncrier/test/test_packaging.py b/src/towncrier/test/test_packaging.py index 2dbcbfbf..199adffd 100644 --- a/src/towncrier/test/test_packaging.py +++ b/src/towncrier/test/test_packaging.py @@ -7,10 +7,20 @@ class TestPackaging(TestCase): - def no_version_attr(self): + def test_version_attr(self): """ - towncrier.__version__ was deprecated, now no longer exists. + towncrier.__version__ was deprecated, but still exists for now. """ - with self.assertRaises(AttributeError): - towncrier.__version__ + def access__version(): + return towncrier.__version__ + + expected_warning = ( + "Accessing towncrier.__version__ is deprecated and will be " + "removed in a future release. Use importlib.metadata directly " + "to query for towncrier's packaging metadata." + ) + + self.assertWarns( + DeprecationWarning, expected_warning, __file__, access__version + ) diff --git a/src/towncrier/test/test_project.py b/src/towncrier/test/test_project.py index 30939d2f..3beda360 100644 --- a/src/towncrier/test/test_project.py +++ b/src/towncrier/test/test_project.py @@ -136,7 +136,7 @@ def test_missing_version(self): get_version(tmp_dir, "missing") self.assertEqual( - ("Package not installed and no missing.__version__ found",), + ("No __version__ or metadata version info for the 'missing' package.",), e.exception.args, )