From bcd2a70f8a555cfa856f4808c14b79eada0c57c3 Mon Sep 17 00:00:00 2001 From: eladrich Date: Thu, 6 Jan 2022 08:24:14 +0200 Subject: [PATCH] Fixed a bug when a nested dataclass had required arguments :bug: --- pyrallis/__init__.py | 2 +- pyrallis/wrappers/dataclass_wrapper.py | 10 ++++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/pyrallis/__init__.py b/pyrallis/__init__.py index b98bdd6..9bd9a95 100644 --- a/pyrallis/__init__.py +++ b/pyrallis/__init__.py @@ -1,4 +1,4 @@ -__version__ = "0.1.4.1" +__version__ = "0.1.4.2" from . import wrappers, utils from pyrallis.help_formatter import SimpleHelpFormatter diff --git a/pyrallis/wrappers/dataclass_wrapper.py b/pyrallis/wrappers/dataclass_wrapper.py index 6332560..f05580f 100644 --- a/pyrallis/wrappers/dataclass_wrapper.py +++ b/pyrallis/wrappers/dataclass_wrapper.py @@ -130,8 +130,14 @@ def defaults(self) -> List[Dataclass]: default = getattr(default, self.name) self._defaults.append(default) else: - default_field_value = utils.default_value( - self._field) # NOTE: That's my problem, post_init was called here to create defaults, understand when defaults are used + try: + default_field_value = utils.default_value(self._field) + except TypeError as e: + # utils.default_value tries to construct the field to get default value and might fail + # if the field has some required arguments + logger.debug( + f"Could not get default value for field '{self._field.name}'\n\tUnderlying Error: {e}") + default_field_value = dataclasses.MISSING if isinstance(default_field_value, _MISSING_TYPE): self._defaults = [] else: