From 150372e29728459b93f0fb46028ced9c2afe1d15 Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Sat, 3 Dec 2016 12:16:50 +1300 Subject: [PATCH] options: defaults are a class attribute --- mitmproxy/optmanager.py | 13 ++++--------- test/mitmproxy/test_optmanager.py | 5 +++++ 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/mitmproxy/optmanager.py b/mitmproxy/optmanager.py index fd777d742..61421ba75 100644 --- a/mitmproxy/optmanager.py +++ b/mitmproxy/optmanager.py @@ -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(): diff --git a/test/mitmproxy/test_optmanager.py b/test/mitmproxy/test_optmanager.py index 97124368e..0c98daea0 100644 --- a/test/mitmproxy/test_optmanager.py +++ b/test/mitmproxy/test_optmanager.py @@ -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",