Get callable signature from inspect.signature in argspec format. inspect.signature is a Python 3 only function that returns the signature of a function. Its advantage over inspect.getfullargspec is that it returns the signature of a decorated function, if the wrapper function itself is
(f: Callable)
| 310 | |
| 311 | |
| 312 | def _get_argspec_from_signature(f: Callable) -> ArgSpec: |
| 313 | """Get callable signature from inspect.signature in argspec format. |
| 314 | |
| 315 | inspect.signature is a Python 3 only function that returns the signature of |
| 316 | a function. Its advantage over inspect.getfullargspec is that it returns |
| 317 | the signature of a decorated function, if the wrapper function itself is |
| 318 | decorated with functools.wraps. |
| 319 | |
| 320 | """ |
| 321 | args = [] |
| 322 | varargs = None |
| 323 | varkwargs = None |
| 324 | defaults = [] |
| 325 | kwonly = [] |
| 326 | kwonly_defaults = {} |
| 327 | annotations = {} |
| 328 | |
| 329 | # We use signature here instead of getfullargspec as the latter also returns |
| 330 | # self and cls (for class methods). |
| 331 | signature = inspect.signature(f) |
| 332 | for parameter in signature.parameters.values(): |
| 333 | if parameter.annotation is not parameter.empty: |
| 334 | annotations[parameter.name] = parameter.annotation |
| 335 | |
| 336 | if parameter.kind == inspect.Parameter.POSITIONAL_OR_KEYWORD: |
| 337 | args.append(parameter.name) |
| 338 | if parameter.default is not parameter.empty: |
| 339 | defaults.append(parameter.default) |
| 340 | elif parameter.kind == inspect.Parameter.POSITIONAL_ONLY: |
| 341 | args.append(parameter.name) |
| 342 | elif parameter.kind == inspect.Parameter.VAR_POSITIONAL: |
| 343 | varargs = parameter.name |
| 344 | elif parameter.kind == inspect.Parameter.KEYWORD_ONLY: |
| 345 | kwonly.append(parameter.name) |
| 346 | kwonly_defaults[parameter.name] = parameter.default |
| 347 | elif parameter.kind == inspect.Parameter.VAR_KEYWORD: |
| 348 | varkwargs = parameter.name |
| 349 | |
| 350 | return ArgSpec( |
| 351 | args, |
| 352 | varargs, |
| 353 | varkwargs, |
| 354 | defaults if defaults else None, |
| 355 | kwonly, |
| 356 | kwonly_defaults if kwonly_defaults else None, |
| 357 | annotations if annotations else None, |
| 358 | ) |
| 359 | |
| 360 | |
| 361 | _get_encoding_line_re = LazyReCompile(r"^.*coding[:=]\s*([-\w.]+).*$") |
no test coverage detected