@@ -82,6 +82,39 @@ def normalize_ndarray(arg, name=None):
82
82
import functools
83
83
84
84
85
+ def maybe_normalize (arg , parm ):
86
+ """Normalize arg if a normalizer is registred."""
87
+ normalizer = normalizers .get (parm .annotation , None )
88
+ return normalizer (arg ) if normalizer else arg
89
+
90
+
91
+ def normalizer (func ):
92
+ @functools .wraps (func )
93
+ def wrapped (* args , ** kwds ):
94
+ params = inspect .signature (func ).parameters
95
+ first_param = next (iter (params .values ()))
96
+ args_copy = args [:]
97
+ # NumPy's API does not have positional args before variadic positional args
98
+ if first_param .kind == inspect .Parameter .VAR_POSITIONAL :
99
+ args = [maybe_normalize (arg , first_param ) for arg in args ]
100
+ else :
101
+ args = [
102
+ maybe_normalize (arg , parm ) for arg , parm in zip (args , params .values ())
103
+ ]
104
+
105
+ # NB: extra unknown arguments: pass through, will raise in func(*args) below
106
+ args += args_copy [len (args ) :]
107
+
108
+ kwds = {
109
+ name : maybe_normalize (arg , params [name ]) if name in params else arg
110
+ for name , arg in kwds .items ()
111
+ }
112
+ return func (* args , ** kwds )
113
+
114
+ return wrapped
115
+
116
+
117
+ '''
85
118
def normalize_this(arg, parm):
86
119
"""Normalize arg if a normalizer is registred."""
87
120
normalizer = normalizers.get(parm.annotation, None)
@@ -123,3 +156,4 @@ def wrapped(*args, **kwds):
123
156
return result
124
157
125
158
return wrapped
159
+ '''
0 commit comments