Browse Source
Co-authored-by: Yurii Karabas <[email protected]> Co-authored-by: Sebastián Ramírez <[email protected]>pull/14443/head
committed by
GitHub
3 changed files with 44 additions and 2 deletions
@ -0,0 +1,9 @@ |
|||
from pydantic import BaseModel |
|||
|
|||
|
|||
def forwardref_method(input: "ForwardRefModel") -> "ForwardRefModel": |
|||
return ForwardRefModel(x=input.x + 1) |
|||
|
|||
|
|||
class ForwardRefModel(BaseModel): |
|||
x: int = 0 |
|||
@ -0,0 +1,31 @@ |
|||
import functools |
|||
|
|||
from fastapi import FastAPI |
|||
from fastapi.testclient import TestClient |
|||
|
|||
from .forward_reference_type import forwardref_method |
|||
|
|||
|
|||
def passthrough(f): |
|||
@functools.wraps(f) |
|||
def method(*args, **kwargs): |
|||
return f(*args, **kwargs) |
|||
|
|||
return method |
|||
|
|||
|
|||
def test_wrapped_method_type_inference(): |
|||
""" |
|||
Regression test ensuring that when a method imported from another module |
|||
is decorated with something that sets the __wrapped__ attribute (functools.wraps), |
|||
then the types are still processed correctly, including dereferencing of forward |
|||
references. |
|||
""" |
|||
app = FastAPI() |
|||
client = TestClient(app) |
|||
app.post("/endpoint")(passthrough(forwardref_method)) |
|||
app.post("/endpoint2")(passthrough(passthrough(forwardref_method))) |
|||
with client: |
|||
response = client.post("/endpoint", json={"input": {"x": 0}}) |
|||
response2 = client.post("/endpoint2", json={"input": {"x": 0}}) |
|||
assert response.json() == response2.json() == {"x": 1} |
|||
Loading…
Reference in new issue