A typed `functools.wraps`.
Project description
tightwrap
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.3.0 (2024-05-24)
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file tightwrap-24.3.0.tar.gz
.
File metadata
- Download URL: tightwrap-24.3.0.tar.gz
- Upload date:
- Size: 22.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.0.0 CPython/3.12.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5af24989c3b02616b71083cbdc29b0c6b78a6eb98e17827d1c92bcec3f342d8f |
|
MD5 | 8d668b93908dcd8bc493edad52956bb2 |
|
BLAKE2b-256 | d5385a5190bf7632a5e8444e197eb8073ebf7f67b7475ba6598b3b61624a1048 |
File details
Details for the file tightwrap-24.3.0-py3-none-any.whl
.
File metadata
- Download URL: tightwrap-24.3.0-py3-none-any.whl
- Upload date:
- Size: 7.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.0.0 CPython/3.12.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4cccafa883af2206e1a6660250d5c633296c130b3ac6db88ff3b82f453743671 |
|
MD5 | 0e0ca45e0c6f65ce34d9266bd9d3f87b |
|
BLAKE2b-256 | fd2a5a492e2935bf6d4660b8134be6a024a3d03f23832c91007bcf9750f928ea |