Skip to content

bpo-42455: Add decorator_factory to functools module #23501

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion Lib/functools.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
__all__ = ['update_wrapper', 'wraps', 'WRAPPER_ASSIGNMENTS', 'WRAPPER_UPDATES',
'total_ordering', 'cache', 'cmp_to_key', 'lru_cache', 'reduce',
'partial', 'partialmethod', 'singledispatch', 'singledispatchmethod',
'cached_property']
'cached_property', 'decorator_factory']

from abc import get_cache_token
from collections import namedtuple
Expand Down Expand Up @@ -979,3 +979,19 @@ def __get__(self, instance, owner=None):
return val

__class_getitem__ = classmethod(GenericAlias)


def decorator_factory(func):
"""Transform function into decorator"""

@wraps(func)
def decorator(*args, **kwargs):
if args and callable(args[0]):
return func(*args, **kwargs)

def wrapper(f):
return func(f, *args, **kwargs)

return wrapper

return decorator
57 changes: 57 additions & 0 deletions Lib/test/test_functools.py
Original file line number Diff line number Diff line change
Expand Up @@ -2615,5 +2615,62 @@ def test_doc(self):
self.assertEqual(CachedCostItem.cost.__doc__, "The cost of the item.")


class TestDecoratorFactory(unittest.TestCase):
def test_wrapping_attributes(self):
@py_functools.decorator_factory
def function(func, /, a, b):
"""This is function"""
return "Test"

self.assertEqual(function.__name__, "function")
if sys.flags.optimize < 2:
self.assertEqual(function.__doc__, "This is function")

def test_default_values(self):
@py_functools.decorator_factory
def decorator(func, /, a=1):
def wrapper(*args, **kwargs):
return a

return wrapper

@decorator
def a():
pass

@decorator()
def b():
pass

@decorator(a=2)
def c():
pass

self.assertEqual(a(), 1)
self.assertEqual(b(), 1)
self.assertEqual(c(), 2)

def test_params_without_default_value(self):
@py_functools.decorator_factory
def decorator(func, /, a):
return func

with self.assertRaises(TypeError):
@decorator
def a():
pass

def test_wraps_correct_function(self):
@py_functools.decorator_factory
def decorator(func, /):
return func

def a():
pass

self.assertIs(decorator(a), a)
self.assertIs(decorator()(a), a)


if __name__ == '__main__':
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Add ``decorator_factory`` function to ``functools`` module. Patch
provided by Yurii Karabas.