Skip to main content

Signature-preserving function delegation

Project description

A tiny utility for preserving signature information (parameter names, annotations, and docstrings) when delegating one function to another.

Works for keyword arguments only; positional arguments are not supported.

Installation

pip install delegatefn

Usage

from delegatefn import delegate
import inspect

def foo(a, b, c):
    """This is the docstring for foo."""

@delegate(foo)
def bar(**kwargs):
    """This is the docstring for bar."""

assert foo.__doc__ == bar.__doc__ == "This is the docstring for foo."
assert inspect.signature(bar).parameters.keys() == {"a", "b", "c"}

Limitations

Unfortunately, there isn't an easy way to combine docstrings from multiple functions. Instead, delegate lets you decide which function's docstring to use.

from delegatefn import delegate

def foo(a, b, c):
    """This is the docstring for foo."""

@delegate(foo, delegate_docstring=False)
def bar(**kwargs):
    """This is the docstring for bar."""

assert foo.__doc__ != bar.__doc__ == "This is the docstring for bar."

Acknowledgements

This approach was inspired by fast.ai.

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

delegatefn-0.2.0.tar.gz (3.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

delegatefn-0.2.0-py3-none-any.whl (4.0 kB view details)

Uploaded Python 3

File details

Details for the file delegatefn-0.2.0.tar.gz.

File metadata

  • Download URL: delegatefn-0.2.0.tar.gz
  • Upload date:
  • Size: 3.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.1 CPython/3.11.1 Darwin/22.1.0

File hashes

Hashes for delegatefn-0.2.0.tar.gz
Algorithm Hash digest
SHA256 e7b34f8d0ccd16a92db834b8739bef0e1e144e4881c33ab0448925ac1fecd5d3
MD5 98f5dc3cec97d266b8b85c21ffd57366
BLAKE2b-256 31cc2f5c3849354334c8d28d6e31a3df1603e708bfc9e84ece83a25c47fff786

See more details on using hashes here.

File details

Details for the file delegatefn-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: delegatefn-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 4.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.1 CPython/3.11.1 Darwin/22.1.0

File hashes

Hashes for delegatefn-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bb417d0191b7f26ef202068eeffa1c7c8251d8e3b864dbefbce7ee50693301cc
MD5 6c5c88792fc32c5e48d2e0b8b25c7fab
BLAKE2b-256 91d227ca7b717060e703b13dcf8a1217f4ed8397dd2d852347c054c1bafd1f9b

See more details on using hashes here.

Supported by

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