options: defaults are a class attribute

This commit is contained in:
Aldo Cortesi 2016-12-03 12:16:50 +13:00
parent 2cb1f70381
commit 150372e297
2 changed files with 9 additions and 9 deletions

View File

@ -54,15 +54,6 @@ class OptManager(metaclass=_DefaultsMeta):
# ._initialized = True as the final operation.
instance = super().__new__(cls)
instance.__dict__["_opts"] = {}
defaults = {}
for klass in reversed(inspect.getmro(cls)):
for p in inspect.signature(klass.__init__).parameters.values():
if p.kind in (p.KEYWORD_ONLY, p.POSITIONAL_OR_KEYWORD):
if not p.default == p.empty:
defaults[p.name] = p.default
instance.__dict__["_defaults"] = defaults
return instance
def __init__(self):
@ -143,6 +134,10 @@ class OptManager(metaclass=_DefaultsMeta):
"""
self.update(**self._defaults)
@classmethod
def default(klass, opt):
return copy.deepcopy(klass._defaults[opt])
def update(self, **kwargs):
updated = set(kwargs.keys())
for k, v in kwargs.items():

View File

@ -30,6 +30,11 @@ class TD2(TD):
def test_defaults():
assert TD2.default("one") == "done"
assert TD2.default("two") == "dtwo"
assert TD2.default("three") == "dthree"
assert TD2.default("four") == "dfour"
o = TD2()
assert o._defaults == {
"one": "done",