Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: NixOS/nixops
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 5d242eeedd8f
Choose a base ref
...
head repository: NixOS/nixops
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 92abb110bfaa
Choose a head ref
  • 2 commits
  • 2 files changed
  • 2 contributors

Commits on Apr 28, 2020

  1. Copy the full SHA
    944c00c View commit details
  2. Merge pull request #1330 from adisbladis/validated-defaults

    nixops.util.ImmutableValidatedObject: Handle default values
    grahamc authored Apr 28, 2020
    Copy the full SHA
    92abb11 View commit details
Showing with 12 additions and 1 deletion.
  1. +7 −1 nixops/util.py
  2. +5 −0 tests/unit/test_util.py
8 changes: 7 additions & 1 deletion nixops/util.py
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions tests/unit/test_util.py
Original file line number Diff line number Diff line change
@@ -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)