Skip to content

Commit

Permalink
Merge pull request #4 from shakefu/preserve_staticmethods
Browse files Browse the repository at this point in the history
Preserve staticmethods
  • Loading branch information
shakefu committed Mar 19, 2018
2 parents 035c2f7 + d8dbe3c commit 4fae8af
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
10 changes: 10 additions & 0 deletions pytool/lang.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@ class Test(object):
if hasattr(klass, attr):
cls_dict[attr] = getattr(klass, attr)

# Preserve static methods on the wrapped class type
for attr in klass.__dict__:
if isinstance(klass.__dict__[attr], staticmethod):
cls_dict[attr] = klass.__dict__[attr]

# Make new method that controls singleton behavior
def __new__(cls, *args, **kwargs):
if not cls._singleton:
Expand Down Expand Up @@ -185,6 +190,11 @@ def __init__(self, *args, **kwargs):
if hasattr(klass, attr):
cls_dict[attr] = getattr(klass, attr)

# Preserve static methods on the wrapped class type
for attr in klass.__dict__:
if isinstance(klass.__dict__[attr], staticmethod):
cls_dict[attr] = klass.__dict__[attr]

# Make new method that controls singleton behavior
def __new__(cls, *args, **kwargs):
hashable_kwargs = tuple(sorted(six.iteritems(kwargs)))
Expand Down
25 changes: 25 additions & 0 deletions test/test_lang.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,20 @@ class HashedSingleton(object):
def __init__(self, *args, **kwargs):
pass

@staticmethod
def static():
return 'static'


@pytool.lang.singleton
class Singleton(object):
def __init__(self, *args, **kwargs):
pass

@staticmethod
def static():
return 'static'


def test_get_name():
frame = inspect.currentframe()
Expand Down Expand Up @@ -268,6 +276,15 @@ def test_hashed_singleton_weakref():
ok_(ts != str(t2), "{} != {}".format(ts, str(t2)))


def test_hashed_singleton_preserves_staticmethods():
ok_(HashedSingleton.static)
eq_(HashedSingleton.static(), 'static')

t = HashedSingleton()
ok_(t.static)
eq_(t.static(), 'static')


def test_singleton_no_args():
s = Singleton()
ok_(s is Singleton())
Expand All @@ -281,3 +298,11 @@ def test_singleton_args():
def test_singleton_kwarg():
s = Singleton(kwarg='kwarg')
ok_(s is Singleton(grawk='grawk'))

def test_singleton_preserves_staticmethods():
ok_(Singleton.static)
eq_(Singleton.static(), 'static')

t = Singleton()
ok_(t.static)
eq_(t.static(), 'static')
30 changes: 29 additions & 1 deletion test/test_proxy.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import six

import pytool
from .util import eq_, ok_, SkipTest
from .util import eq_, ok_, raises, SkipTest


def test_list_proxy_instantiates_ok():
Expand Down Expand Up @@ -47,6 +47,21 @@ def test_list_proxy_comparison_operator():
eq_(cmp(p, a), p.__cmp__(a))


def test_list_proxy_comparison_operator_again():
l = [1, 2]
a = [1, 2]
b = [3, 4]
p = pytool.proxy.ListProxy(l)
eq_(l, a)
eq_(p, a)
eq_(p, l)
eq_(p, p)
ok_(p == p)
ok_(not p == b)
ok_(not p != a)
ok_(p < b)


def test_list_proxy_contains_operator():
l = [1, 2]
p = pytool.proxy.ListProxy(l)
Expand Down Expand Up @@ -181,6 +196,13 @@ def test_dict_proxy_compare():
eq_(p.__cmp__(p), cmp(p, p))


def test_dict_proxy_compare_again():
d = {'one': 1, 'two': 2}
p = pytool.proxy.DictProxy(d)
eq_(d, p)
eq_(p, p)


def test_dict_proxy_get_set_del_item():
d = {'one': 1}
p = pytool.proxy.DictProxy(d)
Expand Down Expand Up @@ -284,3 +306,9 @@ def test_dict_proxy_as_json():
d['foo'] = 'bar'
eq_(pytool.json.as_json(d), '{"foo": "bar"}')


@raises(KeyError)
def test_dict_proxy_raises_key_error():
d = pytool.proxy.DictProxy({})
d['foo']

0 comments on commit 4fae8af

Please sign in to comment.