Skip to content

Commit

Permalink
Property support
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowone committed Jul 22, 2018
1 parent 067004a commit dec78eb
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 9 deletions.
1 change: 0 additions & 1 deletion docs/coder.rst
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ Now `FloatCoder` is registered as `float`. Use it in a familiar way.
:func:`ring.coder.coderize` for details.



Override a coder
----------------

Expand Down
3 changes: 1 addition & 2 deletions ring/callable.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ def _kwargify(self, args, kwargs, bound_args=()):
# no .POSITIONAL_ONLY support

if bound_args:
while i in bound_args:
i += 1
i += len(bound_args)

while i < parameters_len and \
parameters[i].kind == inspect.Parameter.POSITIONAL_OR_KEYWORD:
Expand Down
12 changes: 8 additions & 4 deletions ring/func/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -506,10 +506,11 @@ def _merge_args(self, args, kwargs):
The merging follows the signature of wrapped function and current
instance.
"""
if type(self.__func__) is types.MethodType: # noqa
bound_args = range(len(self._bound_objects))
else:
# TODO: self._bound_objects must be empty for non-binding functions
if type(self.__func__) is types.FunctionType: # noqa
bound_args = ()
else:
bound_args = range(len(self._bound_objects))
full_kwargs = self._callable.kwargify(
args, kwargs, bound_args=bound_args)
return full_kwargs
Expand Down Expand Up @@ -630,14 +631,17 @@ def __init__(self, *args, **kwargs):

self.ring = PublicRing(self)

func = f if type(f) is types.FunctionType else f.__func__ # noqa
func = f if type(f) is types.FunctionType else Callable(f).wrapped_callable # noqa
interface_keys = tuple(k for k in dir(user_interface) if k[0] != '_')

class _RingWire(RingWire):
if wire_slots is not False:
assert isinstance(wire_slots, tuple)
__slots__ = interface_keys + wire_slots

def _on_property(self):
return self.run(self._rope.default_action)

if default_action:
@functools.wraps(func)
def __call__(self, *args, **kwargs):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def get_version():

install_requires = [
'six>=1.11.0',
'wirerope>=0.1.2',
'wirerope>=0.2.0',
]
tests_require = [
'pytest>=3.0.2', 'pytest-cov', 'pytest-lazy-fixture', 'mock', 'patch',
Expand Down
34 changes: 33 additions & 1 deletion tests/test_ring.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,19 @@
__all__ = ('pythonmemcache_client', )


class A():
class hybridmethod(object):

def __init__(self, func):
self.__func__ = func

def __get__(self, obj, type=None):
bound = obj if obj is not None else type
return self.__func__.__get__(bound, type)


class A(object):

v = -10

def __init__(self, v):
self.v = v
Expand All @@ -18,6 +30,16 @@ def __ring_key__(self):
def x(self):
return self.v

@ring.dict({})
@hybridmethod
def h(self):
return self.v

@ring.dict({})
@property
def p(self):
return self.v


def test_ring_wrapper():
a = A(10)
Expand All @@ -32,6 +54,16 @@ def test_ring_wrapper():
assert b.x() == 20


def test_custom_method():
assert A.h() == -10
assert A(10).h() == 10


def test_ring_property():
a = A(15)
assert a.p == 15


@pytest.mark.parametrize('value', [
1,
0,
Expand Down

0 comments on commit dec78eb

Please sign in to comment.