Private helper: constructs Signature for the given python function.
(cls, func, skip_bound_arg=True,
globals=None, locals=None, eval_str=False)
| 2329 | |
| 2330 | |
| 2331 | def _signature_from_function(cls, func, skip_bound_arg=True, |
| 2332 | globals=None, locals=None, eval_str=False): |
| 2333 | """Private helper: constructs Signature for the given python function.""" |
| 2334 | |
| 2335 | is_duck_function = False |
| 2336 | if not isfunction(func): |
| 2337 | if _signature_is_functionlike(func): |
| 2338 | is_duck_function = True |
| 2339 | else: |
| 2340 | # If it's not a pure Python function, and not a duck type |
| 2341 | # of pure function: |
| 2342 | raise TypeError('{!r} is not a Python function'.format(func)) |
| 2343 | |
| 2344 | s = getattr(func, "__text_signature__", None) |
| 2345 | if s: |
| 2346 | return _signature_fromstr(cls, func, s, skip_bound_arg) |
| 2347 | |
| 2348 | Parameter = cls._parameter_cls |
| 2349 | |
| 2350 | # Parameter information. |
| 2351 | func_code = func.__code__ |
| 2352 | pos_count = func_code.co_argcount |
| 2353 | arg_names = func_code.co_varnames |
| 2354 | posonly_count = func_code.co_posonlyargcount |
| 2355 | positional = arg_names[:pos_count] |
| 2356 | keyword_only_count = func_code.co_kwonlyargcount |
| 2357 | keyword_only = arg_names[pos_count:pos_count + keyword_only_count] |
| 2358 | annotations = get_annotations(func, globals=globals, locals=locals, eval_str=eval_str) |
| 2359 | defaults = func.__defaults__ |
| 2360 | kwdefaults = func.__kwdefaults__ |
| 2361 | |
| 2362 | if defaults: |
| 2363 | pos_default_count = len(defaults) |
| 2364 | else: |
| 2365 | pos_default_count = 0 |
| 2366 | |
| 2367 | parameters = [] |
| 2368 | |
| 2369 | non_default_count = pos_count - pos_default_count |
| 2370 | posonly_left = posonly_count |
| 2371 | |
| 2372 | # Non-keyword-only parameters w/o defaults. |
| 2373 | for name in positional[:non_default_count]: |
| 2374 | kind = _POSITIONAL_ONLY if posonly_left else _POSITIONAL_OR_KEYWORD |
| 2375 | annotation = annotations.get(name, _empty) |
| 2376 | parameters.append(Parameter(name, annotation=annotation, |
| 2377 | kind=kind)) |
| 2378 | if posonly_left: |
| 2379 | posonly_left -= 1 |
| 2380 | |
| 2381 | # ... w/ defaults. |
| 2382 | for offset, name in enumerate(positional[non_default_count:]): |
| 2383 | kind = _POSITIONAL_ONLY if posonly_left else _POSITIONAL_OR_KEYWORD |
| 2384 | annotation = annotations.get(name, _empty) |
| 2385 | parameters.append(Parameter(name, annotation=annotation, |
| 2386 | kind=kind, |
| 2387 | default=defaults[offset])) |
| 2388 | if posonly_left: |
no test coverage detected