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

26.1.0 (2026-03-04)

  • Add support for wrapping methods with correct typing. (#9)
  • Add support for Python 3.14. (#8)
  • Add support for Python 3.13. (#4)
  • Drop support for Python 3.8 and 3.9. (#8)

24.3.0 (2024-05-24)

  • Fix wrapping unannotated functions. (#2 #3)

24.2.0 (2024-05-04)

  • Add support for Python 3.8 and 3.9. (#1)

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-26.1.0.tar.gz (139.1 kB view details)

Uploaded Source

Built Distribution

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

tightwrap-26.1.0-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: tightwrap-26.1.0.tar.gz
  • Upload date:
  • Size: 139.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for tightwrap-26.1.0.tar.gz
Algorithm Hash digest
SHA256 da963813dae54bd4754f4c50017923c4d88403efe4aa89b5df85577d51cc2a21
MD5 2e3d07049f7fc1085e0732330d14d29f
BLAKE2b-256 70d7a657bf4f55da1a6e48ceb13e991dd4074a3dbd03777a0b228c3a611d43c9

See more details on using hashes here.

Provenance

The following attestation bundles were made for tightwrap-26.1.0.tar.gz:

Publisher: pypi-package.yml on Tinche/tightwrap

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

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

File metadata

  • Download URL: tightwrap-26.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for tightwrap-26.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a76a4659f9f27d94259a86ee87a5a85a36b6b3a531fe5e43425658b789cbbb33
MD5 0138a819dc08f8ed0c86f1de9dee6b19
BLAKE2b-256 7c57cb916e28bbb7a27d53a22c332a9077ecbed163a132835acd5d76b937a797

See more details on using hashes here.

Provenance

The following attestation bundles were made for tightwrap-26.1.0-py3-none-any.whl:

Publisher: pypi-package.yml on Tinche/tightwrap

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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