Allow callables (as functools.partial) in path operations (#977)

This commit is contained in:
Sebastián Ramírez 2020-02-12 21:36:14 +01:00 committed by GitHub
parent d91b2b3ee8
commit e0c3519b94
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 3 deletions

View File

@ -335,9 +335,7 @@ class APIRoute(routing.Route):
self.include_in_schema = include_in_schema self.include_in_schema = include_in_schema
self.response_class = response_class self.response_class = response_class
assert inspect.isfunction(endpoint) or inspect.ismethod( assert callable(endpoint), f"An endpoint must be a callable"
endpoint
), f"An endpoint must be a function or method"
self.dependant = get_dependant(path=self.path_format, call=self.endpoint) self.dependant = get_dependant(path=self.path_format, call=self.endpoint)
for depends in self.dependencies[::-1]: for depends in self.dependencies[::-1]:
self.dependant.dependencies.insert( self.dependant.dependencies.insert(

View File

@ -0,0 +1,24 @@
from functools import partial
from fastapi import FastAPI
from starlette.testclient import TestClient
def main(some_arg, q: str = None):
return {"some_arg": some_arg, "q": q}
endpoint = partial(main, "foo")
app = FastAPI()
app.get("/")(endpoint)
client = TestClient(app)
def test_partial():
response = client.get("/?q=bar")
data = response.json()
assert data == {"some_arg": "foo", "q": "bar"}