Skip to content

Commit 11dbc7a

Browse files
committed
Update HatchVersionConfig with cached_property
1 parent 11f8879 commit 11dbc7a

File tree

1 file changed

+42
-63
lines changed
  • backend/src/hatchling/metadata

1 file changed

+42
-63
lines changed

backend/src/hatchling/metadata/core.py

Lines changed: 42 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1356,88 +1356,67 @@ def __init__(self, root: str, config: dict[str, Any], plugin_manager: PluginMana
13561356
self.config = config
13571357
self.plugin_manager = plugin_manager
13581358

1359-
self._cached: str | None = None
1360-
self._source_name: str | None = None
1361-
self._scheme_name: str | None = None
1362-
self._source: VersionSourceInterface | None = None
1363-
self._scheme: VersionSchemeInterface | None = None
1364-
1365-
@property
1359+
@cached_property
13661360
def cached(self) -> str:
1367-
if self._cached is None:
1368-
try:
1369-
self._cached = self.source.get_version_data()['version']
1370-
except Exception as e: # noqa: BLE001
1371-
message = f'Error getting the version from source `{self.source.PLUGIN_NAME}`: {e}'
1372-
raise type(e)(message) from None
1373-
1374-
return self._cached
1361+
try:
1362+
return self.source.get_version_data()['version']
1363+
except Exception as e: # noqa: BLE001
1364+
message = f'Error getting the version from source `{self.source.PLUGIN_NAME}`: {e}'
1365+
raise type(e)(message) from None
13751366

1376-
@property
1367+
@cached_property
13771368
def source_name(self) -> str:
1378-
if self._source_name is None:
1379-
source: str = self.config.get('source', 'regex')
1380-
if not source:
1381-
message = 'The `source` option under the `tool.hatch.version` table must not be empty if defined'
1382-
raise ValueError(message)
1383-
1384-
if not isinstance(source, str):
1385-
message = 'Field `tool.hatch.version.source` must be a string'
1386-
raise TypeError(message)
1369+
source: str = self.config.get('source', 'regex')
1370+
if not source:
1371+
message = 'The `source` option under the `tool.hatch.version` table must not be empty if defined'
1372+
raise ValueError(message)
13871373

1388-
self._source_name = source
1374+
if not isinstance(source, str):
1375+
message = 'Field `tool.hatch.version.source` must be a string'
1376+
raise TypeError(message)
13891377

1390-
return self._source_name
1378+
return source
13911379

1392-
@property
1380+
@cached_property
13931381
def scheme_name(self) -> str:
1394-
if self._scheme_name is None:
1395-
scheme: str = self.config.get('scheme', 'standard')
1396-
if not scheme:
1397-
message = 'The `scheme` option under the `tool.hatch.version` table must not be empty if defined'
1398-
raise ValueError(message)
1399-
1400-
if not isinstance(scheme, str):
1401-
message = 'Field `tool.hatch.version.scheme` must be a string'
1402-
raise TypeError(message)
1382+
scheme: str = self.config.get('scheme', 'standard')
1383+
if not scheme:
1384+
message = 'The `scheme` option under the `tool.hatch.version` table must not be empty if defined'
1385+
raise ValueError(message)
14031386

1404-
self._scheme_name = scheme
1387+
if not isinstance(scheme, str):
1388+
message = 'Field `tool.hatch.version.scheme` must be a string'
1389+
raise TypeError(message)
14051390

1406-
return self._scheme_name
1391+
return scheme
14071392

1408-
@property
1393+
@cached_property
14091394
def source(self) -> VersionSourceInterface:
1410-
if self._source is None:
1411-
from copy import deepcopy
1412-
1413-
source_name = self.source_name
1414-
version_source = self.plugin_manager.version_source.get(source_name)
1415-
if version_source is None:
1416-
from hatchling.plugin.exceptions import UnknownPluginError
1395+
from copy import deepcopy
14171396

1418-
message = f'Unknown version source: {source_name}'
1419-
raise UnknownPluginError(message)
1397+
source_name = self.source_name
1398+
version_source = self.plugin_manager.version_source.get(source_name)
1399+
if version_source is None:
1400+
from hatchling.plugin.exceptions import UnknownPluginError
14201401

1421-
self._source = version_source(self.root, deepcopy(self.config))
1402+
message = f'Unknown version source: {source_name}'
1403+
raise UnknownPluginError(message)
14221404

1423-
return self._source
1405+
return version_source(self.root, deepcopy(self.config))
14241406

1425-
@property
1407+
@cached_property
14261408
def scheme(self) -> VersionSchemeInterface:
1427-
if self._scheme is None:
1428-
from copy import deepcopy
1429-
1430-
scheme_name = self.scheme_name
1431-
version_scheme = self.plugin_manager.version_scheme.get(scheme_name)
1432-
if version_scheme is None:
1433-
from hatchling.plugin.exceptions import UnknownPluginError
1409+
from copy import deepcopy
14341410

1435-
message = f'Unknown version scheme: {scheme_name}'
1436-
raise UnknownPluginError(message)
1411+
scheme_name = self.scheme_name
1412+
version_scheme = self.plugin_manager.version_scheme.get(scheme_name)
1413+
if version_scheme is None:
1414+
from hatchling.plugin.exceptions import UnknownPluginError
14371415

1438-
self._scheme = version_scheme(self.root, deepcopy(self.config))
1416+
message = f'Unknown version scheme: {scheme_name}'
1417+
raise UnknownPluginError(message)
14391418

1440-
return self._scheme
1419+
return version_scheme(self.root, deepcopy(self.config))
14411420

14421421

14431422
class HatchMetadataSettings(Generic[PluginManagerBound]):

0 commit comments

Comments
 (0)