Skip to content

Commit

Permalink
qualname compat
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowone committed May 20, 2018
1 parent 21e3b39 commit 56abf9f
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 24 deletions.
12 changes: 12 additions & 0 deletions ring/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,15 @@
from functools import lru_cache
except ImportError: # for py2
from functools32 import lru_cache # noqa


def qualname(x):
if six.PY34:
return x.__qualname__

# not perfect - but it is ok for cache key
if hasattr(x, 'im_class'):
return '.'.join(
(x.im_class.__name__, x.__name__))
else:
return x.__name__
12 changes: 6 additions & 6 deletions ring/callable.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from __future__ import absolute_import

from collections import OrderedDict
from ring._util import cached_property
from ring._compat import inspect
import collections
from ._util import cached_property
from ._compat import inspect, qualname

__all__ = ('Callable', )

Expand Down Expand Up @@ -41,7 +41,7 @@ def annotations(self):

def kwargify(self, args, kwargs):
"""Create a merged kwargs-like object with given args and kwargs."""
merged = OrderedDict()
merged = collections.OrderedDict()

_params = self.parameters_values
_params_len = len(_params)
Expand Down Expand Up @@ -128,8 +128,8 @@ def kwargify(self, args, kwargs):

@cached_property
def identifier(self):
return '{self.callable.__module__}.{self.callable.__qualname__}'.format(
self=self)
return '.'.join(
(self.callable.__module__, qualname(self.callable)))

@cached_property
def first_parameter(self):
Expand Down
5 changes: 3 additions & 2 deletions ring/django.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
========================================
"""
from __future__ import absolute_import

import functools
from django.core import cache
from ring import func_base as fbase
from ring.func_sync import ring_class_factory, CacheInterface
from . import func_base as fbase
from .func_sync import ring_class_factory, CacheInterface


__all__ = ('django', 'django_default')
Expand Down
4 changes: 2 additions & 2 deletions ring/func.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
Ring object factory functions are aggregated in this module.
"""

from ring.func_sync import dict, memcache, redis_py, redis, disk
from .func_sync import dict, memcache, redis_py, redis, disk

try:
import asyncio
except ImportError:
asyncio = False

if asyncio:
from ring.func_asyncio import aiodict, aiomcache, aioredis
from .func_asyncio import aiodict, aiomcache, aioredis
else:
aiodict = None
aiomcache = None
Expand Down
2 changes: 1 addition & 1 deletion ring/func_asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import asyncio
import inspect
import time
from ring import func_base as fbase
from . import func_base as fbase

__all__ = ('dict', 'aiodict', 'aiomcache', 'aioredis', )

Expand Down
34 changes: 25 additions & 9 deletions ring/func_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
import abc
import functools
import six
from ring._compat import lru_cache
from ring.callable import Callable
from ring.key import CallableKey
from ring.wire import Wire
from ring.coder import registry as coder_registry, coderize

from ._compat import lru_cache, qualname
from .callable import Callable
from .key import CallableKey
from .wire import Wire
from .coder import registry as coder_registry, coderize

__all__ = (
'is_method', 'is_classmethod', 'Ring', 'factory', 'NotFound',
Expand Down Expand Up @@ -52,15 +53,20 @@ def suggest_ignorable_keys(c, ignorable_keys):

def suggest_key_prefix(c, key_prefix):
if key_prefix is None:
cc = c.callable
if is_method(c):
key_prefix = \
'{0.__module__}.{{self.__class__.__qualname__}}.' \
'{0.__qualname__}'.format(c.callable)
'{1}'.format(cc, qualname(cc))
if not six.PY34:
key_prefix = key_prefix.replace('__qualname__', '__name__')
elif is_classmethod(c):
# cls is already a str object somehow
key_prefix = '{0.__module__}.{{cls}}.{0.__qualname__}'.format(c.callable)
key_prefix = '{0.__module__}.{{cls}}.{1}'.format(
cc, qualname(cc))
else:
key_prefix = '{0.__module__}.{0.__qualname__}'.format(c.callable)
key_prefix = '{0.__module__}.{1}'.format(
cc, qualname(cc))
else:
key_prefix = key_prefix.replace('{', '{{').replace('}', '}}')
return key_prefix
Expand Down Expand Up @@ -225,7 +231,17 @@ def impl_f(*args, **kwargs):
if function_args_count == 0:
functools.wraps(c)(impl_f)
impl_f.__name__ = '.'.join((c.__name__, name))
impl_f.__qualname__ = '.'.join((c.__qualname__, name))
if six.PY34:
impl_f.__qualname__ = '.'.join(
(c.__qualname__, name))
else:
# mock __qualname__ for py2
if hasattr(impl_f, 'im_class'):
impl_f.__qualname__ = '.'.join(
(impl_f.im_class.__name__,
impl_f.__name__))
else:
impl_f.__qualname__ = impl_f.__name__
setattr(self, name, impl_f)

return self.__getattribute__(name)
Expand Down
2 changes: 1 addition & 1 deletion ring/func_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import time
import re
import hashlib
from ring import func_base as fbase
from . import func_base as fbase

__all__ = ('dict', 'memcache', 'redis_py', 'redis', 'disk', 'arcus')

Expand Down
5 changes: 2 additions & 3 deletions ring/key.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@

from __future__ import absolute_import

import re
from ring._util import cached_property
from ring.callable import Callable
from ._util import cached_property
from .callable import Callable


class Key(object):
Expand Down
11 changes: 11 additions & 0 deletions tests/test_compat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

from ring._compat import qualname


def test_qualname():

class A(object):
def f():
pass

assert qualname(A.f).endswith('A.f')

0 comments on commit 56abf9f

Please sign in to comment.