diff --git a/scone/head/dag.py b/scone/head/dag.py index 6fdc318..5d7f3e9 100644 --- a/scone/head/dag.py +++ b/scone/head/dag.py @@ -92,13 +92,32 @@ class Resource: though should only be used where necessary and sensible to do so. """ # extra_params: Optional[frozendict[str, str]] = None - extra_params: Optional[frozendict] = None + extra_params: Optional[frozendict] = attr.ib(default=None) def __str__(self) -> str: - extra_str = "" if not self.extra_params else f" {self.extra_params!r}" + extra_str = "" if not self.extra_params else f"{self.extra_params!r}" sous_str = "" if not self.sous else f" on {self.sous}" return f"{self.kind}({self.id}){extra_str}{sous_str}" + @extra_params.validator + def _check_extra_params(self, _attribute, value): + if value is not None and len(value) == 0: + raise ValueError( + "Resources must not contain an empty extra_params dict." + " Use None instead." + ) + + @staticmethod + def new_lenient( + kind: str, id: str, sous: Optional[str], extra_params: Optional[frozendict] + ): + """ + Alternative constructor which will correct an empty extra_params dict. + """ + if extra_params is not None and len(extra_params) == 0: + extra_params = None + return Resource(kind, id, sous, extra_params) + @attr.s(auto_attribs=True) class ResourceMeta: diff --git a/scone/head/kitchen.py b/scone/head/kitchen.py index 0d42c64..60cfd9e 100644 --- a/scone/head/kitchen.py +++ b/scone/head/kitchen.py @@ -70,7 +70,7 @@ class Preparation: if sous == "(self)": sous = self._current_recipe.recipe_context.sous - resource = Resource( + resource = Resource.new_lenient( requirement, identifier, sous, frozendict(extra_identifiers) ) @@ -91,7 +91,7 @@ class Preparation: if sous == "(self)": sous = self._current_recipe.recipe_context.sous - resource = Resource( + resource = Resource.new_lenient( requirement, identifier, sous, frozendict(extra_identifiers) )