Skip to content

Commit

Permalink
cleaning up
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowone committed Jun 12, 2018
1 parent ab2519e commit 91d90e2
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 7 deletions.
12 changes: 6 additions & 6 deletions ring/callable.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,15 @@ def is_barefunction(self):
method_name = cc.__qualname__.split('<locals>.')[-1]
if method_name == cc.__name__:
return True
if method_name.endswith('<locals>.' + cc.__name__):
return True
return False
else:
if self.is_descriptor:
return False
# im_class does not exist at this point
return not (self.is_method or self.is_classmethod)
return not (self.is_membermethod or self.is_classmethod)

@cached_property
def is_method(self):
def is_membermethod(self):
"""Test given argument is a method or not.
:param ring.callable.Callable c: A callable object.
Expand All @@ -81,7 +79,8 @@ def is_method(self):
if not self.is_descriptor:
return True

return self.first_parameter and self.first_parameter.name == 'self'
return self.first_parameter is not None \
and self.first_parameter.name == 'self'

@cached_property
def is_classmethod(self):
Expand All @@ -98,7 +97,8 @@ def is_classmethod(self):
if not self.is_descriptor:
return False

return self.first_parameter and self.first_parameter.name == 'cls'
return self.first_parameter is not None \
and self.first_parameter.name == 'cls'

def kwargify(self, args, kwargs, bound_args=()):
"""Create a merged kwargs-like object with given args and kwargs."""
Expand Down
2 changes: 1 addition & 1 deletion ring/func_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def suggest_key_prefix(c, key_prefix):
if six.PY2:
cc = c.wrapped_callable
# A proper solution is `im_class` of the bound method
if c.is_method:
if c.is_membermethod:
key_prefix = \
'{0.__module__}.{{self.__class__.__name__}}.{0.__name__}' \
.format(cc)
Expand Down
9 changes: 9 additions & 0 deletions tests/_test_callable_py3.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import asyncio
from typing import Any, Optional
from ring.callable import Callable

Expand Down Expand Up @@ -31,3 +32,11 @@ def f(a: int, b: str, *c, d: Any=10, **e) -> Optional[float]:

c = Callable(f)
assert c.annotations == {'a': int, 'b': str, 'd': Any, 'return': Optional[float]}


def test_code():
def f(a): pass # noqa
assert Callable(f).code.co_name == 'f'

h = asyncio.coroutine(f)
assert Callable(h).code.co_name == 'f'
39 changes: 39 additions & 0 deletions tests/test_callable.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,42 @@ def test_kwargify_exc(f, args, kwargs, exc):
def test_empty_annotations():
c = Callable(lambda a, *b, **c: None)
assert c.annotations == {}


def test_callable_attributes():

def f():
pass
w = Callable(f)
assert w.is_barefunction is True
assert w.is_descriptor is False
assert w.is_membermethod is False
assert w.is_classmethod is False

class A():

def m(self):
pass
w = Callable(m)
assert w.is_barefunction is False
assert w.is_descriptor is False
assert w.is_membermethod is True
assert w.is_classmethod is False

@classmethod
def c(cls):
pass
w = Callable(c)
assert w.is_barefunction is False
assert w.is_descriptor is True
assert w.is_membermethod is False
assert w.is_classmethod is True

@staticmethod
def s():
pass
w = Callable(s)
assert w.is_barefunction is False
assert w.is_descriptor is True
assert w.is_membermethod is False
assert w.is_classmethod is False

0 comments on commit 91d90e2

Please sign in to comment.