Coverage for tests/test_wrapped_method_forward_reference.py: 100%

18 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2025-12-04 08:29 +0000

1import functools 1hijklmn

2 

3from fastapi import FastAPI 1hijklmn

4from fastapi.testclient import TestClient 1hijklmn

5 

6from .forward_reference_type import forwardref_method 1hijklmn

7 

8 

9def passthrough(f): 1hijklmn

10 @functools.wraps(f) 1abcdefg

11 def method(*args, **kwargs): 1abcdefg

12 return f(*args, **kwargs) 1abcdefg

13 

14 return method 1abcdefg

15 

16 

17def test_wrapped_method_type_inference(): 1hijklmn

18 """ 

19 Regression test ensuring that when a method imported from another module 

20 is decorated with something that sets the __wrapped__ attribute (functools.wraps), 

21 then the types are still processed correctly, including dereferencing of forward 

22 references. 

23 """ 

24 app = FastAPI() 1abcdefg

25 client = TestClient(app) 1abcdefg

26 app.post("/endpoint")(passthrough(forwardref_method)) 1abcdefg

27 app.post("/endpoint2")(passthrough(passthrough(forwardref_method))) 1abcdefg

28 with client: 1abcdefg

29 response = client.post("/endpoint", json={"input": {"x": 0}}) 1abcdefg

30 response2 = client.post("/endpoint2", json={"input": {"x": 0}}) 1abcdefg

31 assert response.json() == response2.json() == {"x": 1} 1abcdefg