Skip to content

Commit 2c82d39

Browse files
committed
-
1 parent 5b7be87 commit 2c82d39

File tree

2 files changed

+34
-13
lines changed

2 files changed

+34
-13
lines changed

source_py2/python_toolbox/monkeypatching_tools.py

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -56,19 +56,25 @@ def decorator(function):
5656
if not monkeypatchee_is_a_class:
5757
raise NotImplemented("I don't know how to monkeypatch a "
5858
"descriptor onto a non-class object.")
59-
### Getting name of descriptor: ###################################
60-
# #
61-
if isinstance(function, caching.CachedProperty):
62-
if not isinstance(function.getter, types.FunctionType):
63-
raise NotImplemented
64-
name_ = function.getter.__name__
65-
elif isinstance(function, (classmethod, staticmethod)):
66-
name_ = function.__func__.__name__
67-
else:
68-
raise NotImplemented("`monkeypatch_method` doesn't know how "
69-
"to handle this kind of function.")
70-
# #
71-
### Finished getting name of descriptor. ##########################
59+
if not name:
60+
### Getting name of descriptor: ###############################
61+
# #
62+
if isinstance(function, caching.CachedProperty):
63+
if not isinstance(function.getter, types.FunctionType):
64+
raise NotImplemented
65+
name_ = function.getter.__name__
66+
elif isinstance(function, (classmethod, staticmethod)):
67+
name_ = function.__func__.__name__
68+
elif isinstance(function, property):
69+
name_ = function.fget.__name__
70+
else:
71+
raise NotImplemented(
72+
"`monkeypatch_method` doesn't know how to get the "
73+
"name of this kind of function automatically, try "
74+
"manually."
75+
)
76+
# #
77+
### Finished getting name of descriptor. ######################
7278
setattr(monkeypatchee, name_, function)
7379
return function
7480

source_py2/test_python_toolbox/test_monkeypatching_tools.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,21 @@ def woof(a):
4747
del meow, woof
4848

4949

50+
def test_monkeypatch_property():
51+
52+
class A(EqualByIdentity):
53+
pass
54+
55+
@monkeypatching_tools.monkeypatch_method(A)
56+
@property
57+
def meow(a):
58+
return (type(a), 'bark')
59+
60+
a0 = A()
61+
a1 = A()
62+
assert a0.meow == a1.meow == (A, 'bark')
63+
64+
5065
def test_monkeypatch_cached_property():
5166

5267
class A(EqualByIdentity):

0 commit comments

Comments
 (0)