|
15 | 15 |
|
16 | 16 | """Separate class and instance methods with the same name.""" |
17 | 17 |
|
18 | | -import functools |
| 18 | +import six |
19 | 19 |
|
20 | 20 | try: # pragma: no cover |
21 | 21 | import typing |
|
28 | 28 | 'SeparateClassMethod', |
29 | 29 | ) |
30 | 30 |
|
31 | | -_WRAPPER_ASSIGNMENTS = ( |
32 | | - '__module__', |
33 | | - '__name__', |
34 | | - '__qualname__', |
35 | | - '__doc__', |
36 | | - '__annotations__' |
37 | | -) |
38 | | -_WRAPPER_UPDATES = ('__dict__',) |
39 | | - |
40 | | - |
41 | | -def _update_wrapper(wrapper, |
42 | | - wrapped, |
43 | | - assigned=_WRAPPER_ASSIGNMENTS, |
44 | | - updated=_WRAPPER_UPDATES): |
45 | | - """Update a wrapper function to look like the wrapped function. |
46 | | -
|
47 | | - wrapper is the function to be updated |
48 | | - wrapped is the original function |
49 | | - assigned is a tuple naming the attributes assigned directly |
50 | | - from the wrapped function to the wrapper function (defaults to |
51 | | - functools.WRAPPER_ASSIGNMENTS) |
52 | | - updated is a tuple naming the attributes of the wrapper that |
53 | | - are updated with the corresponding attribute from the wrapped |
54 | | - function (defaults to functools.WRAPPER_UPDATES) |
55 | | - """ |
56 | | - for attr in assigned: |
57 | | - try: |
58 | | - value = getattr(wrapped, attr) |
59 | | - except AttributeError: |
60 | | - pass |
61 | | - else: |
62 | | - setattr(wrapper, attr, value) |
63 | | - for attr in updated: |
64 | | - getattr(wrapper, attr).update(getattr(wrapped, attr, {})) |
65 | | - # Issue #17482: set __wrapped__ last so we don't inadvertently copy it |
66 | | - # from the wrapped function when updating __dict__ |
67 | | - wrapper.__wrapped__ = wrapped |
68 | | - # Return the wrapper so this can be used as a decorator via partial() |
69 | | - return wrapper |
70 | | - |
71 | | - |
72 | | -def _wraps(wrapped, |
73 | | - assigned=_WRAPPER_ASSIGNMENTS, |
74 | | - updated=_WRAPPER_UPDATES): |
75 | | - """Decorator factory to apply update_wrapper() to a wrapper function. |
76 | | -
|
77 | | - Returns a decorator that invokes update_wrapper() with the decorated |
78 | | - function as the wrapper argument and the arguments to wraps() as the |
79 | | - remaining arguments. Default arguments are as for update_wrapper(). |
80 | | - This is a convenience function to simplify applying partial() to |
81 | | - update_wrapper(). |
82 | | - """ |
83 | | - return functools.partial( |
84 | | - _update_wrapper, wrapped=wrapped, assigned=assigned, updated=updated) |
85 | | - |
86 | 31 |
|
87 | 32 | class SeparateClassMethod(object): |
88 | 33 | """Separate class method and instance methods with the same name. |
@@ -182,14 +127,14 @@ def __get__(self, instance, owner): # type: (...) -> TargetFunctionType |
182 | 127 | if self.__class_method is None: |
183 | 128 | raise AttributeError() |
184 | 129 |
|
185 | | - @_wraps(self.__class_method) |
| 130 | + @six.wraps(self.__class_method) |
186 | 131 | def class_method(*args, **kwargs): |
187 | 132 | """Bound class method.""" |
188 | 133 | return self.__class_method(owner, *args, **kwargs) |
189 | 134 |
|
190 | 135 | return class_method |
191 | 136 |
|
192 | | - @_wraps(self.__class_method) |
| 137 | + @six.wraps(self.__instance_method) |
193 | 138 | def instance_method(*args, **kwargs): |
194 | 139 | """Bound instance method.""" |
195 | 140 | return self.__instance_method(instance, *args, **kwargs) |
|
0 commit comments