summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichele Simionato <michele.simionato@gmail.com>2020-03-15 06:57:13 +0100
committerMichele Simionato <michele.simionato@gmail.com>2020-03-15 06:57:13 +0100
commitf6cdd8ad46439b4cec437fd0080c54ec4252ebdf (patch)
treef90beabdf6b456df1294629e3d5201a4a765cbb2
parent2a9b0abbae5454cf47edddba9e320181dd89257e (diff)
downloadpython-decorator-git-f6cdd8ad46439b4cec437fd0080c54ec4252ebdf.tar.gz
Added a test about changing the signature
-rw-r--r--docs/conf.py2
-rw-r--r--src/decorator.py5
-rw-r--r--src/tests/documentation.py48
3 files changed, 51 insertions, 4 deletions
diff --git a/docs/conf.py b/docs/conf.py
index 9e0ffad..51ce1f8 100644
--- a/docs/conf.py
+++ b/docs/conf.py
@@ -48,7 +48,7 @@ master_doc = 'index'
# General information about the project.
project = 'decorator'
-copyright = '2005-2018, Michele Simionato'
+copyright = '2005-2020, Michele Simionato'
author = 'Michele Simionato'
# The version info for the project you're documenting, acts as replacement for
diff --git a/src/decorator.py b/src/decorator.py
index b1f8b56..a5081e0 100644
--- a/src/decorator.py
+++ b/src/decorator.py
@@ -1,6 +1,6 @@
# ######################### LICENSE ############################ #
-# Copyright (c) 2005-2018, Michele Simionato
+# Copyright (c) 2005-2020, Michele Simionato
# All rights reserved.
# Redistribution and use in source and binary forms, with or without
@@ -28,7 +28,8 @@
# DAMAGE.
"""
-Decorator module, see http://pypi.python.org/pypi/decorator
+Decorator module, see
+https://github.com/micheles/decorator/blob/master/docs/documentation.md
for the documentation.
"""
from __future__ import print_function
diff --git a/src/tests/documentation.py b/src/tests/documentation.py
index 46a932a..70f5c3c 100644
--- a/src/tests/documentation.py
+++ b/src/tests/documentation.py
@@ -1,5 +1,6 @@
from __future__ import print_function
import sys
+import inspect
import threading
import time
import functools
@@ -1420,7 +1421,6 @@ This attribute exists for consistency with the behavior of
Another attribute copied from the original function is ``__qualname__``,
the qualified name. This attribute was introduced in Python 3.3.
"""
-
if sys.version_info < (3,):
function_annotations = ''
@@ -1891,6 +1891,52 @@ def operation2():
"""
time.sleep(.1)
+# ####################### changing the signature ########################## #
+
+
+# see https://github.com/micheles/decorator/pull/85
+def to_method(f):
+ """
+ Takes a function with signature (..., context) and returns a new
+ function with signature (self, ...) to be used a a method in a
+ class with a .context attribute.
+ """
+ sig = inspect.signature(f)
+ params = list(sig.parameters.values())
+ assert params[-1].name == 'context'
+ self = inspect.Parameter('self', inspect.Parameter.POSITIONAL_OR_KEYWORD)
+ params.insert(0, self) # insert self
+ del params[-1] # remove context
+ newsig = sig.replace(parameters=params)
+ return FunctionMaker.create(
+ '%s%s' % (f.__name__, newsig),
+ 'context = self.context; return _func_%s' % sig,
+ dict(_func_=f))
+
+
+def foo(x, context):
+ return x
+
+
+def bar(x, y, context):
+ return x + y
+
+
+class Client:
+ def __init__(self, context):
+ self.context = context
+
+
+if sys.version_info >= (3, 5):
+
+ def test_change_sig():
+ """
+ >>> Client.foo = to_method(foo)
+ >>> Client.bar = to_method(bar)
+ >>> c = Client(None)
+ >>> assert c.foo(1) == 1
+ >>> assert c.bar(1, 2) == 3
+ """
if __name__ == '__main__':
import doctest