Fix bugs arising from None and empty dicts causing dupes.

This commit is contained in:
Olivier 'reivilibre' 2020-11-26 22:46:29 +00:00
parent 9da8b4234e
commit 1325183514
2 changed files with 23 additions and 4 deletions

View File

@ -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:

View File

@ -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)
)