Fix Method to be non positional as there is a randomly happening bug that I cannot trace.
This commit is contained in:
@ -26,16 +26,19 @@ class ConfigurableMeta(type):
|
|||||||
if isinstance(value, ContextProcessor):
|
if isinstance(value, ContextProcessor):
|
||||||
cls.__processors__.append(value)
|
cls.__processors__.append(value)
|
||||||
else:
|
else:
|
||||||
|
if not value.name:
|
||||||
|
value.name = name
|
||||||
|
|
||||||
if isinstance(value, Method):
|
if isinstance(value, Method):
|
||||||
if cls.__wrappable__:
|
if cls.__wrappable__:
|
||||||
raise ConfigurationError(
|
raise ConfigurationError(
|
||||||
'Cannot define more than one "Method" option in a configurable. That may change in the future.'
|
'Cannot define more than one "Method" option in a configurable. That may change in the future.'
|
||||||
)
|
)
|
||||||
cls.__wrappable__ = name
|
cls.__wrappable__ = name
|
||||||
if not value.name:
|
|
||||||
value.name = name
|
|
||||||
if not name in cls.__options__:
|
if not name in cls.__options__:
|
||||||
cls.__options__[name] = value
|
cls.__options__[name] = value
|
||||||
|
|
||||||
if value.positional:
|
if value.positional:
|
||||||
cls.__positional_options__.append(name)
|
cls.__positional_options__.append(name)
|
||||||
|
|
||||||
@ -53,11 +56,9 @@ class Configurable(metaclass=ConfigurableMeta):
|
|||||||
|
|
||||||
def __new__(cls, *args, **kwargs):
|
def __new__(cls, *args, **kwargs):
|
||||||
if cls.__wrappable__ and len(args) == 1 and hasattr(args[0], '__call__'):
|
if cls.__wrappable__ and len(args) == 1 and hasattr(args[0], '__call__'):
|
||||||
wrapped, args = args[0], args[1:]
|
return type(args[0].__name__, (cls,), {cls.__wrappable__: args[0]})
|
||||||
return type(wrapped.__name__, (cls, ), {cls.__wrappable__: wrapped})
|
|
||||||
|
|
||||||
# XXX is that correct ??? how does it pass args/kwargs to __init__ ???
|
return super(Configurable, cls).__new__(cls)
|
||||||
return super().__new__(cls)
|
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
|
|||||||
@ -67,11 +67,12 @@ class Option:
|
|||||||
def __set__(self, inst, value):
|
def __set__(self, inst, value):
|
||||||
inst.__options_values__[self.name] = self.clean(value)
|
inst.__options_values__[self.name] = self.clean(value)
|
||||||
|
|
||||||
|
def clean(self, value):
|
||||||
|
return self.type(value) if self.type else value
|
||||||
|
|
||||||
def get_default(self):
|
def get_default(self):
|
||||||
return self.default() if callable(self.default) else self.default
|
return self.default() if callable(self.default) else self.default
|
||||||
|
|
||||||
def clean(self, value):
|
|
||||||
return self.type(value) if self.type else value
|
|
||||||
|
|
||||||
|
|
||||||
class Method(Option):
|
class Method(Option):
|
||||||
@ -106,7 +107,7 @@ class Method(Option):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super().__init__(None, required=False, positional=True)
|
super().__init__(None, required=False)
|
||||||
|
|
||||||
def __get__(self, inst, typ):
|
def __get__(self, inst, typ):
|
||||||
if not self.name in inst.__options_values__:
|
if not self.name in inst.__options_values__:
|
||||||
@ -114,6 +115,8 @@ class Method(Option):
|
|||||||
return inst.__options_values__[self.name]
|
return inst.__options_values__[self.name]
|
||||||
|
|
||||||
def __set__(self, inst, value):
|
def __set__(self, inst, value):
|
||||||
|
if isinstance(value, str):
|
||||||
|
raise ValueError('should be callable')
|
||||||
inst.__options_values__[self.name] = self.type(value) if self.type else value
|
inst.__options_values__[self.name] = self.type(value) if self.type else value
|
||||||
|
|
||||||
def clean(self, value):
|
def clean(self, value):
|
||||||
|
|||||||
@ -15,7 +15,6 @@ class MethodBasedConfigurable(Configurable):
|
|||||||
|
|
||||||
def test_one_wrapper_only():
|
def test_one_wrapper_only():
|
||||||
with pytest.raises(ConfigurationError):
|
with pytest.raises(ConfigurationError):
|
||||||
|
|
||||||
class TwoMethods(Configurable):
|
class TwoMethods(Configurable):
|
||||||
h1 = Method()
|
h1 = Method()
|
||||||
h2 = Method()
|
h2 = Method()
|
||||||
@ -28,7 +27,12 @@ def test_define_with_decorator():
|
|||||||
def Concrete(self, *args, **kwargs):
|
def Concrete(self, *args, **kwargs):
|
||||||
calls.append((args, kwargs, ))
|
calls.append((args, kwargs, ))
|
||||||
|
|
||||||
|
print('handler', Concrete.handler)
|
||||||
|
|
||||||
|
assert callable(Concrete.handler)
|
||||||
t = Concrete('foo', bar='baz')
|
t = Concrete('foo', bar='baz')
|
||||||
|
|
||||||
|
assert callable(t.handler)
|
||||||
assert len(calls) == 0
|
assert len(calls) == 0
|
||||||
t()
|
t()
|
||||||
assert len(calls) == 1
|
assert len(calls) == 1
|
||||||
@ -41,6 +45,7 @@ def test_define_with_argument():
|
|||||||
calls.append((args, kwargs, ))
|
calls.append((args, kwargs, ))
|
||||||
|
|
||||||
t = MethodBasedConfigurable('foo', bar='baz', handler=concrete_handler)
|
t = MethodBasedConfigurable('foo', bar='baz', handler=concrete_handler)
|
||||||
|
assert callable(t.handler)
|
||||||
assert len(calls) == 0
|
assert len(calls) == 0
|
||||||
t()
|
t()
|
||||||
assert len(calls) == 1
|
assert len(calls) == 1
|
||||||
@ -54,6 +59,7 @@ def test_define_with_inheritance():
|
|||||||
calls.append((args, kwargs, ))
|
calls.append((args, kwargs, ))
|
||||||
|
|
||||||
t = Inheriting('foo', bar='baz')
|
t = Inheriting('foo', bar='baz')
|
||||||
|
assert callable(t.handler)
|
||||||
assert len(calls) == 0
|
assert len(calls) == 0
|
||||||
t()
|
t()
|
||||||
assert len(calls) == 1
|
assert len(calls) == 1
|
||||||
@ -69,7 +75,9 @@ def test_inheritance_then_decorate():
|
|||||||
def Concrete(self, *args, **kwargs):
|
def Concrete(self, *args, **kwargs):
|
||||||
calls.append((args, kwargs, ))
|
calls.append((args, kwargs, ))
|
||||||
|
|
||||||
|
assert callable(Concrete.handler)
|
||||||
t = Concrete('foo', bar='baz')
|
t = Concrete('foo', bar='baz')
|
||||||
|
assert callable(t.handler)
|
||||||
assert len(calls) == 0
|
assert len(calls) == 0
|
||||||
t()
|
t()
|
||||||
assert len(calls) == 1
|
assert len(calls) == 1
|
||||||
|
|||||||
Reference in New Issue
Block a user