Skip to content

Commit

Permalink
Update HatchVersionConfig with cached_property
Browse files Browse the repository at this point in the history
  • Loading branch information
maresb committed Dec 27, 2024
1 parent 73bc7d7 commit d4d6b0b
Showing 1 changed file with 42 additions and 63 deletions.
105 changes: 42 additions & 63 deletions backend/src/hatchling/metadata/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1356,88 +1356,67 @@ 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
version_source = self.plugin_manager.version_source.get(source_name)
if version_source is None:
from hatchling.plugin.exceptions import UnknownPluginError
from copy import deepcopy

message = f'Unknown version source: {source_name}'
raise UnknownPluginError(message)
source_name = self.source_name
version_source = self.plugin_manager.version_source.get(source_name)
if version_source is None:
from hatchling.plugin.exceptions import UnknownPluginError

self._source = version_source(self.root, deepcopy(self.config))
message = f'Unknown version source: {source_name}'
raise UnknownPluginError(message)

return self._source
return version_source(self.root, deepcopy(self.config))

@property
@cached_property
def scheme(self) -> VersionSchemeInterface:
if self._scheme is None:
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
from copy import deepcopy

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]):
Expand Down

0 comments on commit d4d6b0b

Please sign in to comment.