Skip to content

Commit 5b7be87

Browse files
committed
-
1 parent 4db2787 commit 5b7be87

File tree

2 files changed

+34
-13
lines changed

2 files changed

+34
-13
lines changed

source_py3/python_toolbox/monkeypatching_tools.py

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

source_py3/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)