@@ -2137,9 +2137,9 @@ def p(name_node, default_node, default=empty):
2137
2137
2138
2138
return cls (parameters , return_annotation = cls .empty )
2139
2139
2140
- def _get_type_hints (func ):
2140
+ def _get_type_hints (func , ** kwargs ):
2141
2141
try :
2142
- return typing .get_type_hints (func )
2142
+ return typing .get_type_hints (func , ** kwargs )
2143
2143
except Exception :
2144
2144
# First, try to use the get_type_hints to resolve
2145
2145
# annotations. But for keeping the behavior intact
@@ -2164,7 +2164,8 @@ def _signature_from_builtin(cls, func, skip_bound_arg=True):
2164
2164
return _signature_fromstr (cls , func , s , skip_bound_arg )
2165
2165
2166
2166
2167
- def _signature_from_function (cls , func , skip_bound_arg = True ):
2167
+ def _signature_from_function (cls , func , skip_bound_arg = True ,
2168
+ globalns = None , localns = None ):
2168
2169
"""Private helper: constructs Signature for the given python function."""
2169
2170
2170
2171
is_duck_function = False
@@ -2190,7 +2191,7 @@ def _signature_from_function(cls, func, skip_bound_arg=True):
2190
2191
positional = arg_names [:pos_count ]
2191
2192
keyword_only_count = func_code .co_kwonlyargcount
2192
2193
keyword_only = arg_names [pos_count :pos_count + keyword_only_count ]
2193
- annotations = _get_type_hints (func )
2194
+ annotations = _get_type_hints (func , globalns = globalns , localns = localns )
2194
2195
2195
2196
defaults = func .__defaults__
2196
2197
kwdefaults = func .__kwdefaults__
@@ -2262,23 +2263,28 @@ def _signature_from_function(cls, func, skip_bound_arg=True):
2262
2263
def _signature_from_callable (obj , * ,
2263
2264
follow_wrapper_chains = True ,
2264
2265
skip_bound_arg = True ,
2266
+ globalns = None ,
2267
+ localns = None ,
2265
2268
sigcls ):
2266
2269
2267
2270
"""Private helper function to get signature for arbitrary
2268
2271
callable objects.
2269
2272
"""
2270
2273
2274
+ _get_signature_of = functools .partial (_signature_from_callable ,
2275
+ follow_wrapper_chains = follow_wrapper_chains ,
2276
+ skip_bound_arg = skip_bound_arg ,
2277
+ globalns = globalns ,
2278
+ localns = localns ,
2279
+ sigcls = sigcls )
2280
+
2271
2281
if not callable (obj ):
2272
2282
raise TypeError ('{!r} is not a callable object' .format (obj ))
2273
2283
2274
2284
if isinstance (obj , types .MethodType ):
2275
2285
# In this case we skip the first parameter of the underlying
2276
2286
# function (usually `self` or `cls`).
2277
- sig = _signature_from_callable (
2278
- obj .__func__ ,
2279
- follow_wrapper_chains = follow_wrapper_chains ,
2280
- skip_bound_arg = skip_bound_arg ,
2281
- sigcls = sigcls )
2287
+ sig = _get_signature_of (obj .__func__ )
2282
2288
2283
2289
if skip_bound_arg :
2284
2290
return _signature_bound_method (sig )
@@ -2292,11 +2298,7 @@ def _signature_from_callable(obj, *,
2292
2298
# If the unwrapped object is a *method*, we might want to
2293
2299
# skip its first parameter (self).
2294
2300
# See test_signature_wrapped_bound_method for details.
2295
- return _signature_from_callable (
2296
- obj ,
2297
- follow_wrapper_chains = follow_wrapper_chains ,
2298
- skip_bound_arg = skip_bound_arg ,
2299
- sigcls = sigcls )
2301
+ return _get_signature_of (obj )
2300
2302
2301
2303
try :
2302
2304
sig = obj .__signature__
@@ -2323,11 +2325,7 @@ def _signature_from_callable(obj, *,
2323
2325
# (usually `self`, or `cls`) will not be passed
2324
2326
# automatically (as for boundmethods)
2325
2327
2326
- wrapped_sig = _signature_from_callable (
2327
- partialmethod .func ,
2328
- follow_wrapper_chains = follow_wrapper_chains ,
2329
- skip_bound_arg = skip_bound_arg ,
2330
- sigcls = sigcls )
2328
+ wrapped_sig = _get_signature_of (partialmethod .func )
2331
2329
2332
2330
sig = _signature_get_partial (wrapped_sig , partialmethod , (None ,))
2333
2331
first_wrapped_param = tuple (wrapped_sig .parameters .values ())[0 ]
@@ -2346,18 +2344,15 @@ def _signature_from_callable(obj, *,
2346
2344
# If it's a pure Python function, or an object that is duck type
2347
2345
# of a Python function (Cython functions, for instance), then:
2348
2346
return _signature_from_function (sigcls , obj ,
2349
- skip_bound_arg = skip_bound_arg )
2347
+ skip_bound_arg = skip_bound_arg ,
2348
+ globalns = globalns , localns = localns )
2350
2349
2351
2350
if _signature_is_builtin (obj ):
2352
2351
return _signature_from_builtin (sigcls , obj ,
2353
2352
skip_bound_arg = skip_bound_arg )
2354
2353
2355
2354
if isinstance (obj , functools .partial ):
2356
- wrapped_sig = _signature_from_callable (
2357
- obj .func ,
2358
- follow_wrapper_chains = follow_wrapper_chains ,
2359
- skip_bound_arg = skip_bound_arg ,
2360
- sigcls = sigcls )
2355
+ wrapped_sig = _get_signature_of (obj .func )
2361
2356
return _signature_get_partial (wrapped_sig , obj )
2362
2357
2363
2358
sig = None
@@ -2368,29 +2363,17 @@ def _signature_from_callable(obj, *,
2368
2363
# in its metaclass
2369
2364
call = _signature_get_user_defined_method (type (obj ), '__call__' )
2370
2365
if call is not None :
2371
- sig = _signature_from_callable (
2372
- call ,
2373
- follow_wrapper_chains = follow_wrapper_chains ,
2374
- skip_bound_arg = skip_bound_arg ,
2375
- sigcls = sigcls )
2366
+ sig = _get_signature_of (call )
2376
2367
else :
2377
2368
# Now we check if the 'obj' class has a '__new__' method
2378
2369
new = _signature_get_user_defined_method (obj , '__new__' )
2379
2370
if new is not None :
2380
- sig = _signature_from_callable (
2381
- new ,
2382
- follow_wrapper_chains = follow_wrapper_chains ,
2383
- skip_bound_arg = skip_bound_arg ,
2384
- sigcls = sigcls )
2371
+ sig = _get_signature_of (new )
2385
2372
else :
2386
2373
# Finally, we should have at least __init__ implemented
2387
2374
init = _signature_get_user_defined_method (obj , '__init__' )
2388
2375
if init is not None :
2389
- sig = _signature_from_callable (
2390
- init ,
2391
- follow_wrapper_chains = follow_wrapper_chains ,
2392
- skip_bound_arg = skip_bound_arg ,
2393
- sigcls = sigcls )
2376
+ sig = _get_signature_of (init )
2394
2377
2395
2378
if sig is None :
2396
2379
# At this point we know, that `obj` is a class, with no user-
@@ -2436,11 +2419,7 @@ def _signature_from_callable(obj, *,
2436
2419
call = _signature_get_user_defined_method (type (obj ), '__call__' )
2437
2420
if call is not None :
2438
2421
try :
2439
- sig = _signature_from_callable (
2440
- call ,
2441
- follow_wrapper_chains = follow_wrapper_chains ,
2442
- skip_bound_arg = skip_bound_arg ,
2443
- sigcls = sigcls )
2422
+ sig = _get_signature_of (call )
2444
2423
except ValueError as ex :
2445
2424
msg = 'no signature found for {!r}' .format (obj )
2446
2425
raise ValueError (msg ) from ex
@@ -2892,10 +2871,12 @@ def from_builtin(cls, func):
2892
2871
return _signature_from_builtin (cls , func )
2893
2872
2894
2873
@classmethod
2895
- def from_callable (cls , obj , * , follow_wrapped = True ):
2874
+ def from_callable (cls , obj , * ,
2875
+ follow_wrapped = True , globalns = None , localns = None ):
2896
2876
"""Constructs Signature for the given callable object."""
2897
2877
return _signature_from_callable (obj , sigcls = cls ,
2898
- follow_wrapper_chains = follow_wrapped )
2878
+ follow_wrapper_chains = follow_wrapped ,
2879
+ globalns = globalns , localns = localns )
2899
2880
2900
2881
@property
2901
2882
def parameters (self ):
@@ -3143,9 +3124,10 @@ def __str__(self):
3143
3124
return rendered
3144
3125
3145
3126
3146
- def signature (obj , * , follow_wrapped = True ):
3127
+ def signature (obj , * , follow_wrapped = True , globalns = None , localns = None ):
3147
3128
"""Get a signature object for the passed callable."""
3148
- return Signature .from_callable (obj , follow_wrapped = follow_wrapped )
3129
+ return Signature .from_callable (obj , follow_wrapped = follow_wrapped ,
3130
+ globalns = globalns , localns = localns )
3149
3131
3150
3132
3151
3133
def _main ():
0 commit comments