-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use globals of original(wrapped) function (#97)
- Loading branch information
1 parent
a936df9
commit 5585390
Showing
4 changed files
with
50 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,43 @@ | ||
from __future__ import annotations | ||
|
||
from fast_depends.core import build_call_model | ||
from fast_depends.use import inject | ||
|
||
from .wrapper import noop_wrap | ||
|
||
from pydantic import BaseModel | ||
|
||
|
||
class Model(BaseModel): | ||
a: str | ||
|
||
|
||
def base_func(a: int) -> str: | ||
return "success" | ||
|
||
|
||
def model_func(m: Model) -> str: | ||
return m.a | ||
|
||
|
||
def test_prebuild(): | ||
model = build_call_model(base_func) | ||
inject()(None, model)(1) | ||
|
||
|
||
def test_prebuild_with_wrapper(): | ||
func = noop_wrap(model_func) | ||
assert func(Model(a="Hi!")) == "Hi!" | ||
|
||
# build_call_model should work even if function is wrapped with a | ||
# wrapper that is imported from different module | ||
call_model = build_call_model(func) | ||
|
||
assert call_model.model | ||
# Fails if function unwrapping is not done at type introspection | ||
|
||
if hasattr(call_model.model, "model_rebuild"): | ||
call_model.model.model_rebuild() | ||
else: | ||
# pydantic v1 | ||
call_model.model.update_forward_refs() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from __future__ import annotations | ||
|
||
from functools import wraps | ||
|
||
|
||
def noop_wrap(func): | ||
@wraps(func) | ||
def wrapper(*args, **kwargs): | ||
return func(*args, **kwargs) | ||
|
||
return wrapper |