我正在阅读并遵循 Pro Python 书,我使用书中的相同代码创建了这两个装饰器 annotation_decorator 和 typesafe ,但是当我尝试运行此代码时,我收到:
TypeError:decorator()接受1个位置参数,但给出了2个
代码与书中的代码相同,我不知道为什么这样行得通,你们能发现哪里出了问题吗?如果您想测试,我在这里托管了PoC:https://cathalscorner.blogspot.com/search?q=create
import functools
import inspect
from itertools import chain
def annotation_decorator(process):
"""
Creates a decorator that processes annotations for each argument passed
into its target function, raising an exception if there's a problem.
"""
@functools.wraps(process)
def decorator(func):
spec = inspect.getfullargspec(func)
annotations = spec.annotations
defaults = spec.defaults or ()
defaults_zip = zip(spec.args[-len(defaults):], defaults)
kwonlydefaults = spec.kwonlydefaults or {}
for name, value in chain(defaults_zip, kwonlydefaults.items()):
if name in annotations:
process(value, annotations[name])
@functools.wraps(func)
def wrapper(*args, **kwargs):
# Populate a dictionary of explicit arguments passed positionally
explicit_args = dict(zip(spec.args, args))
new_args = []
new_kwargs = {}
keyword_args = kwargs.copy()
# Deal with explicit arguments passed positionally
for name, arg in explicit_args:
if name in annotations:
new_args.append(process(arg, annotations[name]))
# Add all explicit arguments passed by keyword
for name in chain(spec.args, spec.kwonlyargs):
if name in kwargs:
new_kwargs[name] = process(keyword_args.pop(name),
annotations[name])
# Deal with variable positional arguments
if spec.varargs and spec.varargs in annotations:
annotation = annotations[spec.varargs]
for arg in args[len(spec.args):]:
new_args.append(process(arg, annotation))
# Deal with variable keyword arguments
if spec.varkw and spec.varkw in annotations:
annotation = annotations[spec.varkw]
for name, arg in keyword_args.items():
new_kwargs[name] = process(arg, annotation)
r = func(*new_args, **new_kwargs)
if 'return' in annotations:
r = process(r, annotations['return'])
return r
return wrapper
return decorator
@annotation_decorator
def typesafe(value, annotation):
"""
Verify that the function is called with the right argument types and
that it returns a value of the right type, according to its annotations
"""
if not isinstance(value, annotation):
raise TypeError("Expected %s, got %s." % (annotation.__name__,
type(value).__name__))
return value
@annotation_decorator
def coerce_arguments(value, annotation):
return annotation(value)
@typesafe(str, str)
def combine(a, b):
return a + b
combine('spam', 'alot')
答案 0 :(得分:0)
我根据@chepner评论修改了两件事:
我没有在typesafe
函数上使用combine
装饰器,而是简单地用annotation_decorator
装饰typesafe
作为arg,并将函数注释添加到函数变量中。
@annotation_decorator(process=typesafe)
def combine(a: str, b: str):
return a + b