Skip to content

Commit b5d5318

Browse files
committed
-
1 parent 58259ad commit b5d5318

File tree

2 files changed

+22
-20
lines changed

2 files changed

+22
-20
lines changed

source_py3/python_toolbox/monkeypatching_tools.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,8 @@ def decorator(function):
4646
if isinstance(function, types.FunctionType):
4747
name_ = name or function.__name__
4848

49-
new_method = types.MethodType(function, monkeypatchee) if \
50-
monkeypatchee_is_a_class else types.MethodType(function,
51-
class_of_monkeypatchee)
49+
new_method = function if monkeypatchee_is_a_class else \
50+
types.MethodType(function, class_of_monkeypatchee)
5251
setattr(monkeypatchee, name_, new_method)
5352
return function
5453
else:

source_py3/test_python_toolbox/test_monkeypatching_tools.py

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,30 @@
1818
from python_toolbox import caching
1919

2020

21+
class EqualByIdentity:
22+
def __eq__(self, other):
23+
return self is other
24+
25+
2126
def test():
2227
'''Test basic workings of `monkeypatch_method`.'''
2328

24-
class A:
29+
class A(EqualByIdentity):
2530
pass
2631

2732
@monkeypatching_tools.monkeypatch_method(A)
2833
def meow(a):
29-
return 1
34+
return (a, 1)
3035

3136
a = A()
3237

33-
assert a.meow() == meow(a) == 1
38+
assert a.meow() == meow(a) == (a, 1)
3439

3540
@monkeypatching_tools.monkeypatch_method(A, 'roar')
3641
def woof(a):
37-
return 2
42+
return (a, 2)
3843

39-
assert a.roar() == woof(a) == 2
44+
assert a.roar() == woof(a) == (a, 2)
4045

4146
assert not hasattr(a, 'woof')
4247

@@ -45,7 +50,7 @@ def woof(a):
4550

4651
def test_monkeypatch_cached_property():
4752

48-
class A:
53+
class A(EqualByIdentity):
4954
pass
5055

5156
@monkeypatching_tools.monkeypatch_method(A)
@@ -82,31 +87,29 @@ def f(): pass
8287

8388
def test_monkeypatch_staticmethod():
8489

85-
class A:
90+
class A(EqualByIdentity):
8691
@staticmethod
8792
def my_static_method(x):
8893
raise 'Flow should never reach here.'
8994

9095
@monkeypatching_tools.monkeypatch_method(A)
9196
@staticmethod
9297
def my_static_method(x):
93-
return 'Success'
98+
return (x, 'Success')
9499

95100
assert isinstance(cute_inspect.getattr_static(A, 'my_static_method'),
96101
staticmethod)
97102
assert isinstance(A.my_static_method, types.FunctionType)
98103

99-
assert A.my_static_method(3) == A.my_static_method('Whatever') == \
100-
'Success'
104+
assert A.my_static_method(3) == A.my_static_method(3) == (3, 'Success')
101105

102106
a0 = A()
103-
assert a0.my_static_method(3) == a0.my_static_method('Whatever') == \
104-
'Success'
107+
assert a0.my_static_method(3) == a0.my_static_method(3) == (3, 'Success')
105108

106109

107110
def test_monkeypatch_classmethod():
108111

109-
class A:
112+
class A(EqualByIdentity):
110113
@classmethod
111114
def my_class_method(cls):
112115
raise 'Flow should never reach here.'
@@ -136,7 +139,7 @@ def test_monkeypatch_classmethod_subclass():
136139
class FunkyClassMethod(classmethod):
137140
is_funky = True
138141

139-
class A:
142+
class A(EqualByIdentity):
140143
@FunkyClassMethod
141144
def my_funky_class_method(cls):
142145
raise 'Flow should never reach here.'
@@ -161,7 +164,7 @@ def test_directly_on_object():
161164

162165
class A:
163166
def woof(self):
164-
return 'woof'
167+
return (self, 'woof')
165168

166169
a0 = A()
167170
a1 = A()
@@ -177,12 +180,12 @@ def woof(a):
177180
assert a0.meow() == 'not meow'
178181
assert a0.woof() == 'not woof'
179182

180-
assert a1.woof() == 'woof'
183+
assert a1.woof() == (a1, 'woof')
181184

182185
with cute_testing.RaiseAssertor(AttributeError):
183186
A.meow()
184187
with cute_testing.RaiseAssertor(AttributeError):
185188
a1.meow()
186189

187-
assert A.woof(a0) == 'woof'
190+
assert A.woof(a0) == (a0, 'woof')
188191

0 commit comments

Comments
 (0)