Skip to content

Tinche/tightwrap

Repository files navigation

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.