Skip to main content

A typed `functools.wraps`.

Project description

tightwrap

PyPI Build Coverage Supported Python Versions Ruff

tightwrap (pronounced typed wrap) is a drop-in replacement for functools.wraps that works with static typing. tightwrap is very small, so if you don't want to add a dependency to it just vendor this file.

functools.wraps is very commonly used to adapt runtime function signatures when wrapping functions, but it doesn't work well with static typing tools. tightwrap.wraps has the same interface and you should use it instead:

from tightwrap import wraps

def function(a: int) -> int:
    return a + 1

@wraps(function)
def wrapping(*args, **kwargs) -> int:
    return function(*args, **kwargs)

reveal_type(wrapping)  # Revealed type is "def (a: builtins.int) -> builtins.int"

wrapping("a string")  # error: Argument 1 to "wrapping" has incompatible type "str"; expected "int"

tightwrap applies functools.wraps under the hood so runtime inspection continues to work.

If your wrapper has a different return type than the function you're wrapping, tightwrap.wraps will use the wrapper return type and make the runtime signature return type match.

For comparison, when using functools.wraps the current version of Mypy reports:

from functools import wraps

def function(a: int) -> int:
    return a + 1

@wraps(function)
def wrapping(*args, **kwargs) -> int:
    return function(*args, **kwargs)

reveal_type(wrapping)  # Revealed type is "def (*args: Any, **kwargs: Any) -> builtins.int"

wrapping("a string")  # No type error, blows up at runtime.

Changelog

24.2.0 (2024-05-04)

  • Add support for Python 3.8 and 3.9.

24.1.0 (2024-01-09)

  • Initial version.

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

tightwrap-24.2.0.tar.gz (22.8 kB view details)

Uploaded Source

Built Distribution

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

tightwrap-24.2.0-py3-none-any.whl (7.4 kB view details)

Uploaded Python 3

File details

Details for the file tightwrap-24.2.0.tar.gz.

File metadata

  • Download URL: tightwrap-24.2.0.tar.gz
  • Upload date:
  • Size: 22.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for tightwrap-24.2.0.tar.gz
Algorithm Hash digest
SHA256 4517f98b62a023ae46bfb28cf231a32cbc810a85e08ef2af1b4048515dfe08fa
MD5 01ee09a02bfc02fd7cc4a7a4551844b5
BLAKE2b-256 4b9c40062d31d4e46e3c18199ceb2564ea844015fba1f691c2e5c3f8e1d99d39

See more details on using hashes here.

File details

Details for the file tightwrap-24.2.0-py3-none-any.whl.

File metadata

  • Download URL: tightwrap-24.2.0-py3-none-any.whl
  • Upload date:
  • Size: 7.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for tightwrap-24.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3d06a4231de33157926ba768e8ea488c27438c89f920c6ad361be3ba45b89f0c
MD5 a85c77020d25fcdccd6799f24c17e7fa
BLAKE2b-256 4fe5108cbbb8018ef1a702988fe20869769254196f0341d3daa890b258b577f1

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