Skip to main content

Backport of `inspect` module from Python3.13

Project description

inspect313

test Python Version

Backport of inspect module from Python3.13, supports Python 3.8+

Features

  • Fully typed with annotations and checked with mypy, PEP561 compatible
  • Has backported skip_bound_arg argument to signature and Signature.from_callable

Installation

pip install inspect313

Examples

Replace inspect.getfullargspec with inspect.signature

getfullargspec is an old way of getting the function signature. It is deprecated since Python 3.13, and new skip_bound_arg: bool = True argument was introduced in the same version.

However, this change was not backported, so users of older Python versions need this package to have the same functionality.

Here's how getfullargspec is different from regular signature call:

  • the self / cls parameter is always reported, even for bound methods
>>> import inspect

>>> class A:
...    def method(self, arg: int) -> None: ...

>>> inspect.getfullargspec(A().method)
FullArgSpec(args=['self', 'arg'], varargs=None, varkw=None, defaults=None, kwonlyargs=[], kwonlydefaults=None, annotations={'return': None, 'arg': <class 'int'>})

>>> # signature() produces a different result:
>>> inspect.signature(A().method)
<Signature (arg: int) -> None>
  • wrapper chains defined by __wrapped__ not unwrapped automatically
>>> import functools

>>> def some_decorator(f):
...     @functools.wraps(f)
...     def wrapper(*args, **kwargs):
...         return f(*args, **kwargs)
...     return wrapper

>>> @some_decorator
... def func(a: int, /, b: str) -> None: ...

>>> inspect.getfullargspec(func)
FullArgSpec(args=[], varargs='args', varkw='kwargs', defaults=None, kwonlyargs=[], kwonlydefaults=None, annotations={'return': None})

>>> # signature() produces a different result:
>>> inspect.signature(func)
<Signature (a: int, /, b: str) -> None>

Here's how you can migrate, these results will be in line with getfullargspec:

>>> import inspect313

>>> inspect313.signature(
...    A().method,
...    skip_bound_arg=False,
...    follow_wrapped=False,
... )
<Signature (self, arg: int) -> None>

>>> inspect313.signature(
...    func,
...    skip_bound_arg=False,
...    follow_wrapped=False,
... )
<Signature (*args, **kwargs) -> None>

However, consider migrating to just using inspect.signature, since it produces more correct results in a general case.

Replace inspect.getargvalues with inspect.Signature.from_frame

getargvalues was used to create signature-like objects from frame objects. But, it didn't really support all features of modern functions.

This is why Python 3.13 introduced inspect.Signature.from_frame public constructor and getargvalues and formatargvalues were deprecated.

Here's how it worked before:

>>> import inspect

>>> def func(a: int = 0, /, b: int = 1, *, c: int = 2):
...     return inspect.currentframe()

>>> frame = func()
>>> # notice that pos-only and kw-only args are not supported properly:
>>> inspect.formatargvalues(*inspect.getargvalues(frame))
'(a=0, b=1, c=2)'

Here's how to replace it with modern API:

>>> from inspect313 import Signature

>>> str(Signature.from_frame(frame))
'(a=0, /, b=1, *, c=2)'

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

inspect313-0.1.0.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

inspect313-0.1.0-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file inspect313-0.1.0.tar.gz.

File metadata

  • Download URL: inspect313-0.1.0.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.1 CPython/3.11.5 Darwin/23.0.0

File hashes

Hashes for inspect313-0.1.0.tar.gz
Algorithm Hash digest
SHA256 9dcedd40bc006043da1ff3fa720d3c25c0dc2e28c5f543e539df3a1b5064c1e6
MD5 92f3941f0a5516ff8b5a5c28c2f35a7f
BLAKE2b-256 e72ac0fc7c51490ecc067ab230f996f7998f5ab13276f00bec273ae87c193e67

See more details on using hashes here.

File details

Details for the file inspect313-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: inspect313-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.1 CPython/3.11.5 Darwin/23.0.0

File hashes

Hashes for inspect313-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ad21191a2a715a0d5c231c66848829d3e00ae754c7cc3f6416e258eb2bf85207
MD5 90e900074665458906b1cb0f3224e2ad
BLAKE2b-256 e03b6f4ccbcf83977713f866b5fcd8ac23c58b25d93629ba78e715b255648694

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page