Skip to content

Commit 413f541

Browse files
committed
Update HatchVersionConfig with cached_property
1 parent ff5707b commit 413f541

File tree

1 file changed

+35
-56
lines changed
  • backend/src/hatchling/metadata

1 file changed

+35
-56
lines changed

backend/src/hatchling/metadata/core.py

Lines changed: 35 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1348,58 +1348,42 @@ def __init__(self, root: str, config: dict[str, Any], plugin_manager: PluginMana
13481348
self.config = config
13491349
self.plugin_manager = plugin_manager
13501350

1351-
self._cached: str | None = None
1352-
self._source_name: str | None = None
1353-
self._scheme_name: str | None = None
1354-
self._source: VersionSourceInterface | None = None
1355-
self._scheme: VersionSchemeInterface | None = None
1356-
1357-
@property
1351+
@cached_property
13581352
def cached(self) -> str:
1359-
if self._cached is None:
1360-
try:
1361-
self._cached = self.source.get_version_data()['version']
1362-
except Exception as e: # noqa: BLE001
1363-
message = f'Error getting the version from source `{self.source.PLUGIN_NAME}`: {e}'
1364-
raise type(e)(message) from None
1365-
1366-
return self._cached
1353+
try:
1354+
return self.source.get_version_data()['version']
1355+
except Exception as e: # noqa: BLE001
1356+
message = f'Error getting the version from source `{self.source.PLUGIN_NAME}`: {e}'
1357+
raise type(e)(message) from None
13671358

1368-
@property
1359+
@cached_property
13691360
def source_name(self) -> str:
1370-
if self._source_name is None:
1371-
source: str = self.config.get('source', 'regex')
1372-
if not source:
1373-
message = 'The `source` option under the `tool.hatch.version` table must not be empty if defined'
1374-
raise ValueError(message)
1375-
1376-
if not isinstance(source, str):
1377-
message = 'Field `tool.hatch.version.source` must be a string'
1378-
raise TypeError(message)
1361+
source: str = self.config.get('source', 'regex')
1362+
if not source:
1363+
message = 'The `source` option under the `tool.hatch.version` table must not be empty if defined'
1364+
raise ValueError(message)
13791365

1380-
self._source_name = source
1366+
if not isinstance(source, str):
1367+
message = 'Field `tool.hatch.version.source` must be a string'
1368+
raise TypeError(message)
13811369

1382-
return self._source_name
1370+
return source
13831371

1384-
@property
1372+
@cached_property
13851373
def scheme_name(self) -> str:
1386-
if self._scheme_name is None:
1387-
scheme: str = self.config.get('scheme', 'standard')
1388-
if not scheme:
1389-
message = 'The `scheme` option under the `tool.hatch.version` table must not be empty if defined'
1390-
raise ValueError(message)
1391-
1392-
if not isinstance(scheme, str):
1393-
message = 'Field `tool.hatch.version.scheme` must be a string'
1394-
raise TypeError(message)
1374+
scheme: str = self.config.get('scheme', 'standard')
1375+
if not scheme:
1376+
message = 'The `scheme` option under the `tool.hatch.version` table must not be empty if defined'
1377+
raise ValueError(message)
13951378

1396-
self._scheme_name = scheme
1379+
if not isinstance(scheme, str):
1380+
message = 'Field `tool.hatch.version.scheme` must be a string'
1381+
raise TypeError(message)
13971382

1398-
return self._scheme_name
1383+
return scheme
13991384

1400-
@property
1385+
@cached_property
14011386
def source(self) -> VersionSourceInterface:
1402-
if self._source is None:
14031387
from copy import deepcopy
14041388

14051389
source_name = self.source_name
@@ -1410,26 +1394,21 @@ def source(self) -> VersionSourceInterface:
14101394
message = f'Unknown version source: {source_name}'
14111395
raise UnknownPluginError(message)
14121396

1413-
self._source = version_source(self.root, deepcopy(self.config))
1397+
return version_source(self.root, deepcopy(self.config))
14141398

1415-
return self._source
1416-
1417-
@property
1399+
@cached_property
14181400
def scheme(self) -> VersionSchemeInterface:
1419-
if self._scheme is None:
1420-
from copy import deepcopy
1401+
from copy import deepcopy
14211402

1422-
scheme_name = self.scheme_name
1423-
version_scheme = self.plugin_manager.version_scheme.get(scheme_name)
1424-
if version_scheme is None:
1425-
from hatchling.plugin.exceptions import UnknownPluginError
1426-
1427-
message = f'Unknown version scheme: {scheme_name}'
1428-
raise UnknownPluginError(message)
1403+
scheme_name = self.scheme_name
1404+
version_scheme = self.plugin_manager.version_scheme.get(scheme_name)
1405+
if version_scheme is None:
1406+
from hatchling.plugin.exceptions import UnknownPluginError
14291407

1430-
self._scheme = version_scheme(self.root, deepcopy(self.config))
1408+
message = f'Unknown version scheme: {scheme_name}'
1409+
raise UnknownPluginError(message)
14311410

1432-
return self._scheme
1411+
return version_scheme(self.root, deepcopy(self.config))
14331412

14341413

14351414
class HatchMetadataSettings(Generic[PluginManagerBound]):

0 commit comments

Comments
 (0)