Ver código fonte

Merge branch 'master' of github.com:celery/celery

Ask Solem 9 anos atrás
pai
commit
1da7dba9ff
2 arquivos alterados com 27 adições e 2 exclusões
  1. 17 0
      celery/tests/utils/test_functional.py
  2. 10 2
      celery/utils/functional.py

+ 17 - 0
celery/tests/utils/test_functional.py

@@ -291,3 +291,20 @@ class test_head_from_fun(Case):
             g(1)
         g(1, 2)
         g(1, 2, kwarg=3)
+
+    def test_from_fun_with_hints(self):
+        local = {}
+        fun = ('def f_hints(x: int, y: int, kwarg: int=1):'
+               '    pass')
+        try:
+            exec(fun, {}, local)
+        except SyntaxError:
+            # py2
+            return
+        f_hints = local['f_hints']
+
+        g = head_from_fun(f_hints)
+        with self.assertRaises(TypeError):
+            g(1)
+        g(1, 2)
+        g(1, 2, kwarg=3)

+ 10 - 2
celery/utils/functional.py

@@ -13,7 +13,10 @@ import threading
 
 from collections import OrderedDict
 from functools import partial, wraps
-from inspect import getargspec, isfunction
+try:
+    from inspect import isfunction, getfullargspec as getargspec
+except ImportError:  # Py2
+    from inspect import isfunction, getargspec  # noqa
 from itertools import chain, islice
 
 from amqp import promise
@@ -28,6 +31,7 @@ __all__ = ['LRUCache', 'is_list', 'maybe_list', 'memoize', 'mlazy', 'noop',
            'regen', 'dictfilter', 'lazy', 'maybe_evaluate', 'head_from_fun']
 
 IS_PY3 = sys.version_info[0] == 3
+IS_PY2 = sys.version_info[0] == 2
 
 KEYWORD_MARK = object()
 
@@ -365,11 +369,15 @@ def _argsfromspec(spec, replace_defaults=True):
         optional = list(zip(spec.args[-split:], defaults))
     else:
         positional, optional = spec.args, []
+    if IS_PY3:  # pragma: no cover
+        keywords = spec.varkw
+    elif IS_PY2:
+        keywords = spec.keywords  # noqa
     return ', '.join(filter(None, [
         ', '.join(positional),
         ', '.join('{0}={1}'.format(k, v) for k, v in optional),
         '*{0}'.format(spec.varargs) if spec.varargs else None,
-        '**{0}'.format(spec.keywords) if spec.keywords else None,
+        '**{0}'.format(keywords) if keywords else None,
     ]))