Skip to content

Commit 6fb6a2e

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 f6ed998 commit 6fb6a2e

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
@@ -802,31 +802,19 @@ def _make_method(func, instance):
802802

803803

804804
def wraps(wrapped, assigned=functools.WRAPPER_ASSIGNMENTS,
805-
updated=functools.WRAPPER_UPDATES, exclude_args=()):
805+
updated=functools.WRAPPER_UPDATES):
806806
"""
807807
An alternative to `functools.wraps` which also preserves the original
808-
function's call signature by way of
809-
`~astropy.utils.codegen.make_function_with_signature`.
810-
811-
This also adds an optional ``exclude_args`` argument. If given it should
812-
be a sequence of argument names that should not be copied from the wrapped
813-
function (either positional or keyword arguments).
808+
function's call signature by way of assigning the ``__signature__``
809+
attribute with the signature of the wrapped function.
814810
815811
The documentation for the original `functools.wraps` follows:
816-
817812
"""
818813

819-
wrapped_args = _get_function_args(wrapped, exclude_args=exclude_args)
820-
821814
def wrapper(func):
822-
if '__name__' in assigned:
823-
name = wrapped.__name__
824-
else:
825-
name = func.__name__
826-
827-
func = make_function_with_signature(func, name=name, **wrapped_args)
828815
func = functools.update_wrapper(func, wrapped, assigned=assigned,
829816
updated=updated)
817+
func.__signature__ = inspect.signature(wrapped)
830818
return func
831819

832820
return wrapper

‎astropy/utils/tests/test_decorators.py‎

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

4848

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

0 commit comments

Comments
 (0)