diff options
| author | Michele Simionato <michele.simionato@gmail.com> | 2021-03-31 06:24:26 +0200 |
|---|---|---|
| committer | Michele Simionato <michele.simionato@gmail.com> | 2021-03-31 06:24:26 +0200 |
| commit | 0a160d0ac0e6ef7a76bdd791a7fc051ce31122cb (patch) | |
| tree | 67f5e866183e5105f63fc891362741ce4c48f8f2 | |
| parent | 9b68169b235bebec5eb9d3f4d999ab087e9a7604 (diff) | |
| download | python-decorator-git-0a160d0ac0e6ef7a76bdd791a7fc051ce31122cb.tar.gz | |
Fixed __kwdefaults__
| -rw-r--r-- | src/decorator.py | 1 | ||||
| -rw-r--r-- | src/tests/documentation.py | 72 | ||||
| -rw-r--r-- | src/tests/test.py | 22 |
3 files changed, 46 insertions, 49 deletions
diff --git a/src/decorator.py b/src/decorator.py index a291557..da1c0c4 100644 --- a/src/decorator.py +++ b/src/decorator.py @@ -213,6 +213,7 @@ def decorate(func, caller, extras=()): fun.__wrapped__ = func fun.__qualname__ = func.__qualname__ fun.__annotations__ = func.__annotations__ + fun.__kwdefaults__ = func.__kwdefaults__ fun.__dict__.update(func.__dict__) return fun diff --git a/src/tests/documentation.py b/src/tests/documentation.py index efb42f2..6a11b5c 100644 --- a/src/tests/documentation.py +++ b/src/tests/documentation.py @@ -6,11 +6,7 @@ import time import functools import itertools import collections -try: - import collections.abc as c -except ImportError: - c = collections - collections.abc = collections +import collections.abc as c from decorator import (decorator, decorate, FunctionMaker, contextmanager, dispatch_on, __version__) @@ -1643,37 +1639,47 @@ def a_test_for_pylons(): """ -if sys.version_info >= (3,): # tests for signatures specific to Python 3 +def test_kwonlydefaults(): + """ + >>> @trace + ... def f(arg, defarg=1, *args, kwonly=2): pass + ... + >>> f.__kwdefaults__ + {'kwonly': 2} + """ - def test_kwonlyargs(): - """ - >>> @trace - ... def func(a, b, *args, y=2, z=3, **kwargs): - ... return y, z - ... - >>> func('a', 'b', 'c', 'd', 'e', y='y', z='z', cat='dog') - calling func with args ('a', 'b', 'c', 'd', 'e'), {'cat': 'dog', 'y': 'y', 'z': 'z'} - ('y', 'z') - """ - def test_kwonly_no_args(): - """# this was broken with decorator 3.3.3 - >>> @trace - ... def f(**kw): pass - ... - >>> f() - calling f with args (), {} - """ +def test_kwonlyargs(): + """ + >>> @trace + ... def func(a, b, *args, y=2, z=3, **kwargs): + ... return y, z + ... + >>> func('a', 'b', 'c', 'd', 'e', y='y', z='z', cat='dog') + calling func with args ('a', 'b', 'c', 'd', 'e'), {'cat': 'dog', 'y': 'y', 'z': 'z'} + ('y', 'z') + """ - def test_kwonly_star_notation(): - """ - >>> @trace - ... def f(*, a=1, **kw): pass - ... - >>> import inspect - >>> inspect.getfullargspec(f) - FullArgSpec(args=[], varargs=None, varkw='kw', defaults=None, kwonlyargs=['a'], kwonlydefaults={'a': 1}, annotations={}) - """ + +def test_kwonly_no_args(): + """# this was broken with decorator 3.3.3 + >>> @trace + ... def f(**kw): pass + ... + >>> f() + calling f with args (), {} + """ + + +def test_kwonly_star_notation(): + """ + >>> @trace + ... def f(*, a=1, **kw): pass + ... + >>> import inspect + >>> inspect.getfullargspec(f) + FullArgSpec(args=[], varargs=None, varkw='kw', defaults=None, kwonlyargs=['a'], kwonlydefaults={'a': 1}, annotations={}) + """ @contextmanager diff --git a/src/tests/test.py b/src/tests/test.py index a72b1d7..83c5649 100644 --- a/src/tests/test.py +++ b/src/tests/test.py @@ -1,21 +1,13 @@ -from __future__ import absolute_import import sys import doctest import unittest import decimal import inspect import functools -import collections -from collections import defaultdict -try: - c = collections.abc -except AttributeError: - c = collections +from asyncio import get_event_loop +from collections import defaultdict, abc as c from decorator import dispatch_on, contextmanager, decorator -try: - from . import documentation as doc -except (ImportError, ValueError, SystemError): # depending on the py-version - import documentation as doc +import documentation as doc @contextmanager @@ -29,22 +21,21 @@ def assertRaises(etype): raise Exception('Expected %s' % etype.__name__) -if sys.version_info >= (3, 5): - exec('''from asyncio import get_event_loop - @decorator async def before_after(coro, *args, **kwargs): return "<before>" + (await coro(*args, **kwargs)) + "<after>" + @decorator def coro_to_func(coro, *args, **kw): return get_event_loop().run_until_complete(coro(*args, **kw)) + class CoroutineTestCase(unittest.TestCase): def test_before_after(self): @before_after async def coro(x): - return x + return x self.assertTrue(inspect.iscoroutinefunction(coro)) out = get_event_loop().run_until_complete(coro('x')) self.assertEqual(out, '<before>x<after>') @@ -55,7 +46,6 @@ class CoroutineTestCase(unittest.TestCase): return x self.assertFalse(inspect.iscoroutinefunction(coro)) self.assertEqual(coro('x'), 'x') -''') def gen123(): |
