Skip to main content

Helps function wrappers/decorators with perfect forwarding

Project description

GitHub Actions Status Latest Version Unlicense

Simple helper for function wrappers or decorators that makes the wrapper function look indistinguishable from the original.

Installation

Using pip:

pip install funcwrap

Alternatively, you can redistribute the funcwrap.py module or even the funcwrap.wrap function by itself as part of your code or program without any license ramifications.

Usage

funcwrap.wrap is similar to the standard functools.wraps function. However, it returns a wrapper that imitates the wrapped function’s signature exactly on the python syntax level.

>>> from funcwrap import wraps

>>> def func(a='Hello,', *, b='World!'):
...     pass

>>> @wraps(func)
... def wrapper(*args, **kwargs):
...     return (args, kwargs)

>>> wrapper()
(('Hello,',), {'b': 'World!'})

>>> wrapper('Bye,', b='Quentin!')
(('Bye,',), {'b': 'Quentin!'})

IMPORTANT: If you’re planning to wrap callables other than python functions or lambdas (e.g. partials, methods, objects), be advised that the results may be surprising. Make absolutely sure that the wrapper function behaves as expected before using funcwrap!

Why (not) use decorator?

funcwrap is a lighter alternative to the decorator module. There are many reasons to stick with decorator and some for trying funcwrap.

Reasons to stick with decorator:

  • more well tested and empirically proven, mature package

  • has a different API that is more directly designed toward writing decorators

  • supports generator and coroutine functions

  • supports python versions below 3.5

  • and probably many more

Reasons to use funcwrap:

  • support for python 3.8’s positional-only parameters

  • simpler, shorter code that is easier to understand and modify if you need to

  • license: you can redistribute this module as part of your code or program without having to retain any license notice

  • has a different API that fits your needs better (@wraps(func) vs @decorator)

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

funcwrap-0.0.1.tar.gz (4.3 kB view hashes)

Uploaded Source

Built Distribution

funcwrap-0.0.1-py3-none-any.whl (3.5 kB view hashes)

Uploaded Python 3

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