8000 Fix wrapping unannotated functions by Tinche · Pull Request #3 · Tinche/tightwrap · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fix wrapping unannotated functions #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,15 @@ wrapping("a string") # No type error, blows up at runtime.

## Changelog

### 24.3.0 (UNRELEASED)

- Fix wrapping unannotated functions.
([#2](https://github.com/Tinche/tightwrap/issues/2) [#3](https://github.com/Tinche/tightwrap/pull/3))

### 24.2.0 (2024-05-04)

- Add support for Python 3.8 and 3.9.
([#1](https://github.com/Tinche/tightwrap/pull/1))

### 24.1.0 (2024-01-09)

Expand Down
40 changes: 20 additions & 20 deletions pdm.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions src/tightwrap/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import sys
from functools import wraps as functools_wraps
from inspect import Signature
from inspect import Signature, _empty
from typing import Any, Callable, TypeVar, cast

from ._backported import eval_if_necessary, get_annotations
Expand All @@ -20,12 +20,12 @@ def _get_resolved_signature(fn: Callable[..., Any]) -> Signature:
evaluated_annotations, fn_globals, fn_locals = get_annotations(fn)

for name, parameter in signature.parameters.items():
setattr(parameter, "_annotation", evaluated_annotations[name])
parameter._annotation = evaluated_annotations.get(name, _empty) # type: ignore

new_return_annotation = eval_if_necessary(
signature.return_annotation, fn_globals, fn_locals
)
setattr(signature, "_return_annotation", new_return_annotation)
signature._return_annotation = new_return_annotation # type: ignore

return signature

Expand All @@ -42,8 +42,8 @@ def wrapper(fn: Callable[..., R]) -> Callable[P, R]:
if orig_sig.return_annotation != wrapper_return:
# We do a little rewriting.
new_sig = Signature(None, return_annotation=wrapper_return)
setattr(new_sig, "_parameters", orig_sig.parameters)
setattr(res, "__signature__", new_sig)
new_sig._parameters = orig_sig.parameters # type: ignore
res.__signature__ = new_sig # type: ignore

return cast(Callable[P, R], res)

Expand Down
19 changes: 17 additions & 2 deletions tests/test_wraps.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

from typing import Any, Callable

from tightwrap import _get_resolved_signature # pyright: ignore[reportPrivateUsage]
from tightwrap import wraps
from tightwrap import (
_get_resolved_signature, # pyright: ignore[reportPrivateUsage]
wraps,
)


def test_wraps() -> None:
Expand Down Expand Up @@ -39,3 +41,16 @@ def wrapper(*args: Any, **kwargs: Any) -> str:
assert wrapped_signature.return_annotation is int

_: Callable[[int], str] = wrapper


def test_wrap_unannotated() -> None:
"""Unannotated functions can be wrapped."""

def inner(a):
return a + 1

@wraps(inner)
def outer(*args: Any, **kwargs: Any) -> int:
return inner(*args, **kwargs)

assert outer(3) == 4
Loading
0