From 5c3ecb55bbe8477a49bfe0fc1fcdb1cc7ff38912 Mon Sep 17 00:00:00 2001 From: Ben Mares Date: Sat, 14 Dec 2024 21:41:35 +0100 Subject: [PATCH] Update HatchVersionConfig with cached_property --- backend/src/hatchling/metadata/core.py | 91 ++++++++++---------------- 1 file changed, 35 insertions(+), 56 deletions(-) diff --git a/backend/src/hatchling/metadata/core.py b/backend/src/hatchling/metadata/core.py index c4d19743e..91d09bb6e 100644 --- a/backend/src/hatchling/metadata/core.py +++ b/backend/src/hatchling/metadata/core.py @@ -1353,58 +1353,42 @@ def __init__(self, root: str, config: dict[str, Any], plugin_manager: PluginMana self.config = config self.plugin_manager = plugin_manager - self._cached: str | None = None - self._source_name: str | None = None - self._scheme_name: str | None = None - self._source: VersionSourceInterface | None = None - self._scheme: VersionSchemeInterface | None = None - - @property + @cached_property def cached(self) -> str: - if self._cached is None: - try: - self._cached = self.source.get_version_data()['version'] - except Exception as e: # noqa: BLE001 - message = f'Error getting the version from source `{self.source.PLUGIN_NAME}`: {e}' - raise type(e)(message) from None - - return self._cached + try: + return self.source.get_version_data()['version'] + except Exception as e: # noqa: BLE001 + message = f'Error getting the version from source `{self.source.PLUGIN_NAME}`: {e}' + raise type(e)(message) from None - @property + @cached_property def source_name(self) -> str: - if self._source_name is None: - source: str = self.config.get('source', 'regex') - if not source: - message = 'The `source` option under the `tool.hatch.version` table must not be empty if defined' - raise ValueError(message) - - if not isinstance(source, str): - message = 'Field `tool.hatch.version.source` must be a string' - raise TypeError(message) + source: str = self.config.get('source', 'regex') + if not source: + message = 'The `source` option under the `tool.hatch.version` table must not be empty if defined' + raise ValueError(message) - self._source_name = source + if not isinstance(source, str): + message = 'Field `tool.hatch.version.source` must be a string' + raise TypeError(message) - return self._source_name + return source - @property + @cached_property def scheme_name(self) -> str: - if self._scheme_name is None: - scheme: str = self.config.get('scheme', 'standard') - if not scheme: - message = 'The `scheme` option under the `tool.hatch.version` table must not be empty if defined' - raise ValueError(message) - - if not isinstance(scheme, str): - message = 'Field `tool.hatch.version.scheme` must be a string' - raise TypeError(message) + scheme: str = self.config.get('scheme', 'standard') + if not scheme: + message = 'The `scheme` option under the `tool.hatch.version` table must not be empty if defined' + raise ValueError(message) - self._scheme_name = scheme + if not isinstance(scheme, str): + message = 'Field `tool.hatch.version.scheme` must be a string' + raise TypeError(message) - return self._scheme_name + return scheme - @property + @cached_property def source(self) -> VersionSourceInterface: - if self._source is None: from copy import deepcopy source_name = self.source_name @@ -1415,26 +1399,21 @@ def source(self) -> VersionSourceInterface: message = f'Unknown version source: {source_name}' raise UnknownPluginError(message) - self._source = version_source(self.root, deepcopy(self.config)) + return version_source(self.root, deepcopy(self.config)) - return self._source - - @property + @cached_property def scheme(self) -> VersionSchemeInterface: - if self._scheme is None: - from copy import deepcopy + from copy import deepcopy - scheme_name = self.scheme_name - version_scheme = self.plugin_manager.version_scheme.get(scheme_name) - if version_scheme is None: - from hatchling.plugin.exceptions import UnknownPluginError - - message = f'Unknown version scheme: {scheme_name}' - raise UnknownPluginError(message) + scheme_name = self.scheme_name + version_scheme = self.plugin_manager.version_scheme.get(scheme_name) + if version_scheme is None: + from hatchling.plugin.exceptions import UnknownPluginError - self._scheme = version_scheme(self.root, deepcopy(self.config)) + message = f'Unknown version scheme: {scheme_name}' + raise UnknownPluginError(message) - return self._scheme + return version_scheme(self.root, deepcopy(self.config)) class HatchMetadataSettings(Generic[PluginManagerBound]):