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.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.1.0.tar.gz (21.4 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.1.0-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: tightwrap-24.1.0.tar.gz
  • Upload date:
  • Size: 21.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.7

File hashes

Hashes for tightwrap-24.1.0.tar.gz
Algorithm Hash digest
SHA256 5ca4c18bc8220c4864e1bd37b90644c81dacd7754c27d068aac2d9f8e7635b0e
MD5 fcabad001662508152323a82805cff4d
BLAKE2b-256 a0718d8285de133b959eb033425de775e5195a200f603f8443e21f0ce5bd2600

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tightwrap-24.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.7

File hashes

Hashes for tightwrap-24.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 88a89d4e55edbad2b8849745a93316eceb570bb0fe6b36adbb00e455b50b0c11
MD5 5d282c84b06c21ea3223db871676e87a
BLAKE2b-256 a3a685af018cec29a984969b2a62a8bc9e9e245365e6c4456f5c77a2ea37928a

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