Skip to content

Commit 730ed6b

Browse files
committed
Make astropy.utils.decorators.wraps much more like functools.wraps
Here is a controversial fix to astropy#7551. This removes two features of `astropy.utils.decorators.wraps` the `exclude_args` method and the way it overwrote the functions name. In return for this, we make use of the new `__signature__` attribute in Python 3 and we probably don't need a lot more code that I have currently deleted in here.
1 parent 9b873f8 commit 730ed6b

3 files changed

Lines changed: 5 additions & 66 deletions

File tree

‎astropy/tests/runner.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ def make_test_runner_in(cls, path):
255255

256256
runner = cls(path)
257257

258-
@wraps(runner.run_tests, ('__doc__',), exclude_args=('self',))
258+
@wraps(runner.run_tests, ('__doc__',))
259259
def test(**kwargs):
260260
return runner.run_tests(**kwargs)
261261

‎astropy/utils/decorators.py‎

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -817,31 +817,19 @@ def _make_method(func, instance):
817817

818818

819819
def wraps(wrapped, assigned=functools.WRAPPER_ASSIGNMENTS,
820-
updated=functools.WRAPPER_UPDATES, exclude_args=()):
820+
updated=functools.WRAPPER_UPDATES):
821821
"""
822822
An alternative to `functools.wraps` which also preserves the original
823-
function's call signature by way of
824-
`~astropy.utils.codegen.make_function_with_signature`.
825-
826-
This also adds an optional ``exclude_args`` argument. If given it should
827-
be a sequence of argument names that should not be copied from the wrapped
828-
function (either positional or keyword arguments).
823+
function's call signature by way of assigning the ``__signature__``
824+
attribute with the signature of the wrapped function.
829825
830826
The documentation for the original `functools.wraps` follows:
831-
832827
"""
833828

834-
wrapped_args = _get_function_args(wrapped, exclude_args=exclude_args)
835-
836829
def wrapper(func):
837-
if '__name__' in assigned:
838-
name = wrapped.__name__
839-
else:
840-
name = func.__name__
841-
842-
func = make_function_with_signature(func, name=name, **wrapped_args)
843830
func = functools.update_wrapper(func, wrapped, assigned=assigned,
844831
updated=updated)
832+
func.__signature__ = inspect.signature(wrapped)
845833
return func
846834

847835
return wrapper

‎astropy/utils/tests/test_decorators.py‎

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -53,55 +53,6 @@ def bar(*args, **kwargs):
5353
assert argspec.defaults == (1, 2, 3)
5454

5555

56-
def test_wraps_exclude_names():
57-
"""
58-
Test the optional ``exclude_names`` argument to the wraps decorator.
59-
"""
60-
61-
# This particular test demonstrates wrapping an instance method
62-
# as a function and excluding the "self" argument:
63-
64-
class TestClass:
65-
def method(self, a, b, c=1, d=2, **kwargs):
66-
return (a, b, c, d, kwargs)
67-
68-
test = TestClass()
69-
70-
@wraps(test.method, exclude_args=('self',))
71-
def func(*args, **kwargs):
72-
return test.method(*args, **kwargs)
73-
74-
argspec = inspect.getfullargspec(func)
75-
assert argspec.args == ['a', 'b', 'c', 'd']
76-
77-
assert func('a', 'b', e=3) == ('a', 'b', 1, 2, {'e': 3})
78-
79-
80-
def test_wraps_keep_orig_name():
81-
"""
82-
Test that when __name__ is excluded from the ``assigned`` argument
83-
to ``wrap`` that the function being wrapped keeps its original name.
84-
85-
Regression test for https://github.com/astropy/astropy/pull/4016
86-
"""
87-
88-
def foo():
89-
pass
90-
91-
assigned = list(functools.WRAPPER_ASSIGNMENTS)
92-
assigned.remove('__name__')
93-
94-
def bar():
95-
pass
96-
97-
orig_bar = bar
98-
99-
bar = wraps(foo, assigned=assigned)(bar)
100-
101-
assert bar is not orig_bar
102-
assert bar.__name__ == 'bar'
103-
104-
10556
def test_deprecated_attribute():
10657
class DummyClass:
10758
def __init__(self):

0 commit comments

Comments
 (0)