-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask_07_03.py
48 lines (36 loc) · 1.61 KB
/
task_07_03.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import collections
from functools import wraps
from inspect import signature
def strict_argument_types(func):
sig = signature(func)
@wraps(func)
def wrapper(*args, **kwargs):
types_of_var = args + tuple(kwargs.values())
i = 0
for key, value in sig.parameters.items():
if not isinstance(types_of_var[i], value.annotation):
raise TypeError('The argument "{}" must be "{}", passed "{}"'.format(key,
value.annotation,
type(types_of_var[i])))
i += 1
return func(*args, **kwargs)
return wrapper
def strict_return_type(func):
sig = signature(func)
@wraps(func)
def wrapper(*args, **kwargs):
result = func(*args, **kwargs)
i = 0
if not isinstance(sig.return_annotation, collections.Iterable):
if not isinstance(result, sig.return_annotation):
raise TypeError('The return value must be "{}", not "{}"'.format(sig.return_annotation,
type(result)))
else:
return result
for item in sig.return_annotation:
if not isinstance(result[i], item):
raise TypeError('The return value must be "{}", not "{}"'.format(item,
type(result[i])))
i += 1
return result
return wrapper