From: Ben Rowland Date: Fri, 8 Nov 2024 16:10:57 +0000 Subject: fix: make compatible with towncrier>=24.7 towncrier 24.7 changed the way that its find_fragments() function works to accept a Config dataclass instead of specific components of the config. This commit adds a new version of lookup_towncrier_fragments() to use the new API and chooses which one to use based on the version of towncrier obtained from importlib.metadata. It also slightly changes the way that towncrier config can be obtained: the previous get_towncrier_config() function (which returned a dict) is now renamed as get_towncrier_config_as_dict() and get_towncrier_config() returns the new Config dataclass object for towncrier versions above 22.12.0rc1 and raises NotImplementedError for older versions. This API change should be ok as get_towncrier_config() was only called in one place before. Origin: other, https://github.com/sphinx-contrib/sphinxcontrib-towncrier/pull/96 Bug: https://github.com/sphinx-contrib/sphinxcontrib-towncrier/issues/92 Bug-Debian: https://bugs.debian.org/1088147 Last-Update: 2024-11-24 --- src/sphinxcontrib/towncrier/_fragment_discovery.py | 50 +++++++++++++++++++++- src/sphinxcontrib/towncrier/_towncrier.py | 16 +++++++ 2 files changed, 65 insertions(+), 1 deletion(-) diff --git a/src/sphinxcontrib/towncrier/_fragment_discovery.py b/src/sphinxcontrib/towncrier/_fragment_discovery.py index e4836d5..fdcb30c 100644 --- a/src/sphinxcontrib/towncrier/_fragment_discovery.py +++ b/src/sphinxcontrib/towncrier/_fragment_discovery.py @@ -2,11 +2,14 @@ from functools import lru_cache +from importlib.metadata import version from pathlib import Path from typing import Optional, Set from sphinx.util import logging +from packaging.version import Version + try: # pylint: disable=no-name-in-module @@ -17,7 +20,9 @@ except ImportError: find_fragments, ) -from ._towncrier import get_towncrier_config # noqa: WPS436 +from ._towncrier import ( # noqa: WPS436 + get_towncrier_config, get_towncrier_config_as_dict, +) logger = logging.getLogger(__name__) @@ -47,6 +52,17 @@ def lookup_towncrier_fragments( # noqa: WPS210 config_path: Optional[str] = None, ) -> Set[Path]: """Emit RST-formatted Towncrier changelog fragment paths.""" + tc_version = Version(version('towncrier')) + if tc_version >= Version('24.7.0'): + return _lookup_towncrier_fragments_post24_7(working_dir, config_path) + else: + return _lookup_towncrier_fragments_pre24_7(working_dir, config_path) + + +# used for towncrier version 24.7 and above +def _lookup_towncrier_fragments_post24_7( + working_dir: Optional[str] = None, config_path: Optional[str] = None, +) -> Set[Path]: project_path = Path.cwd() if working_dir is None else Path(working_dir) final_config_path = ( @@ -66,6 +82,38 @@ def lookup_towncrier_fragments( # noqa: WPS210 ) return set() + fragment_base_directory = project_path / (towncrier_config.directory or 'newsfragments') + + _fragments, fragment_filenames = find_fragments( + str(fragment_base_directory), towncrier_config, strict=False, + ) + + return {f[0] for f in fragment_filenames} + + +# used for versions of towncrier before 24.7 +def _lookup_towncrier_fragments_pre24_7( + working_dir: Optional[str] = None, config_path: Optional[str] = None, +) -> Set[Path]: + project_path = Path.cwd() if working_dir is None else Path(working_dir) + + final_config_path = ( + _resolve_spec_config(project_path, config_path) + or _find_config_file(project_path) + ) + + try: + towncrier_config = get_towncrier_config_as_dict( + project_path, + final_config_path, + ) + except KeyError as key_err: + # NOTE: The error is missing key 'towncrier' or similar + logger.warning( + f'Missing key {key_err!s} in file {final_config_path!s}', + ) + return set() + fragment_directory: Optional[str] = 'newsfragments' try: fragment_base_directory = project_path / towncrier_config['directory'] diff --git a/src/sphinxcontrib/towncrier/_towncrier.py b/src/sphinxcontrib/towncrier/_towncrier.py index ecba95e..498976d 100644 --- a/src/sphinxcontrib/towncrier/_towncrier.py +++ b/src/sphinxcontrib/towncrier/_towncrier.py @@ -1,9 +1,12 @@ """Towncrier related shims.""" from contextlib import suppress as _suppress_exception +from importlib.metadata import version from pathlib import Path from typing import Any, Dict, Union +from packaging.version import Version + with _suppress_exception(ImportError): # NOTE: This will not raise an exception under Python >= 3.7, and is only @@ -25,6 +28,19 @@ except ImportError: def get_towncrier_config( + project_path: Path, + final_config_path: Union[Path, None], +): + tc_version = Version(version('towncrier')) + if tc_version >= Version('22.12.0rc1'): + return load_config_from_file(str(project_path), str(final_config_path)) + raise NotImplementedError( + 'Towncrier Config is not available before version 22.12.0rc1, consider ' + 'using "get_towncrier_config_as_dict()" instead', + ) + + +def get_towncrier_config_as_dict( project_path: Path, final_config_path: Union[Path, None], ) -> Dict[str, Any]: # FIXME: add a better type # pylint: disable=fixme