Skip to content

Commit 88d9a28

Browse files
Kasper JungeRalphify
authored andcommitted
refactor: unify LockedEntry TOML key mapping for serialization and deserialization
from_dict was duplicating TOML key names as string literals instead of reusing the _TOML_OPTIONAL_FIELDS mapping already used by to_toml_table. Now both methods reference the same field-to-key mapping, eliminating a source of potential drift. Co-authored-by: Ralphify <noreply@ralphify.co>
1 parent 6abe036 commit 88d9a28

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

agr/lockfile.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ class LockedEntry:
4141
# Local field
4242
path: str | None = None
4343

44+
# TOML key for the required installed-name field.
45+
_TOML_KEY_INSTALLED_NAME: ClassVar[str] = "installed-name"
46+
4447
# Mapping from dataclass field names to TOML key names.
4548
# Order defines the serialization order in the lockfile.
4649
_TOML_OPTIONAL_FIELDS: ClassVar[tuple[tuple[str, str], ...]] = (
@@ -68,13 +71,13 @@ def _get_optional_str(key: str) -> str | None:
6871
value = data.get(key)
6972
return str(value) if value is not None else None
7073

74+
kwargs: dict[str, str | None] = {
75+
attr: _get_optional_str(toml_key)
76+
for attr, toml_key in cls._TOML_OPTIONAL_FIELDS
77+
}
7178
return cls(
72-
installed_name=str(data.get("installed-name", "")),
73-
handle=_get_optional_str("handle"),
74-
path=_get_optional_str("path"),
75-
source=_get_optional_str("source"),
76-
commit=_get_optional_str("commit"),
77-
content_hash=_get_optional_str("content-hash"),
79+
installed_name=str(data.get(cls._TOML_KEY_INSTALLED_NAME, "")),
80+
**kwargs,
7881
)
7982

8083
def to_toml_table(self) -> tomlkit.items.Table:
@@ -84,7 +87,7 @@ def to_toml_table(self) -> tomlkit.items.Table:
8487
value = getattr(self, attr)
8588
if value is not None:
8689
table[key] = value
87-
table["installed-name"] = self.installed_name
90+
table[self._TOML_KEY_INSTALLED_NAME] = self.installed_name
8891
return table
8992

9093

0 commit comments

Comments
 (0)