Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nixops.util.ImmutableValidatedObject: Handle default values #1330

Merged
merged 1 commit into from
Apr 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion nixops/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,13 @@ def _transform_value(key: Any, value: Any) -> Any:
return value

for key in set(list(anno.keys()) + list(kwargs.keys())):
value = kwargs.get(key)
# If a default value:
# class SomeSubClass(ImmutableValidatedObject):
# x: int = 1
#
# is set this attribute is set on self before __init__ is called
default = getattr(self, key) if hasattr(self, key) else None
value = kwargs.get(key, default)
setattr(self, key, _transform_value(key, value))

self._frozen = True
Expand Down
5 changes: 5 additions & 0 deletions tests/unit/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,8 @@ class MustRaise(util.ImmutableValidatedObject):
fuzz: str

self.assertRaises(TypeError, lambda: MustRaise())

class WithDefaults(util.ImmutableValidatedObject):
x: int = 1

self.assertEqual(WithDefaults().x, 1)