From 3699e17212e1bf0f28627dc4617c5d0714502571 Mon Sep 17 00:00:00 2001 From: Rupsi Kaushik Date: Fri, 12 Jun 2020 19:22:30 -0400 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Implement=20=5F=5Frepr=5F=5F=20meth?= =?UTF-8?q?ods=20for=20path=20parameters=20to=20simplify=20debugging=20(#1?= =?UTF-8?q?560)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * repr description added to Depends class * repr description added to Security subclass * get rid of __repr__ in security since it will inherit from super * make code format consistent with rest * add desc for rest of the classes * Update fastapi/params.py remove trailing whitespace Co-authored-by: Marcelo Trylesinski * Implement __repr__ * fix formatting * formatting again * ran formatting * added basic testing * basic tests added to rest of the classes * added more test coverage and simplified test file Co-authored-by: Marcelo Trylesinski Co-authored-by: Jayati Shrivastava --- fastapi/params.py | 11 ++++++++++ tests/test_params_repr.py | 46 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 tests/test_params_repr.py diff --git a/fastapi/params.py b/fastapi/params.py index 3aa333ac7..c822cbfeb 100644 --- a/fastapi/params.py +++ b/fastapi/params.py @@ -51,6 +51,9 @@ class Param(FieldInfo): **extra, ) + def __repr__(self) -> str: + return f"{self.__class__.__name__}({self.default})" + class Path(Param): in_ = ParamTypes.path @@ -239,6 +242,9 @@ class Body(FieldInfo): **extra, ) + def __repr__(self) -> str: + return f"{self.__class__.__name__}({self.default})" + class Form(Body): def __init__( @@ -316,6 +322,11 @@ class Depends: self.dependency = dependency self.use_cache = use_cache + def __repr__(self) -> str: + attr = getattr(self.dependency, "__name__", type(self.dependency).__name__) + cache = "" if self.use_cache else ", use_cache=False" + return f"{self.__class__.__name__}({attr}{cache})" + class Security(Depends): def __init__( diff --git a/tests/test_params_repr.py b/tests/test_params_repr.py new file mode 100644 index 000000000..e21772aca --- /dev/null +++ b/tests/test_params_repr.py @@ -0,0 +1,46 @@ +import pytest +from fastapi.params import Body, Cookie, Depends, Header, Param, Path, Query + +test_data = ["teststr", None, ..., 1, []] + + +def get_user(): + return {} # pragma: no cover + + +@pytest.fixture(scope="function", params=test_data) +def params(request): + return request.param + + +def test_param_repr(params): + assert repr(Param(params)) == "Param(" + str(params) + ")" + + +def test_path_repr(params): + assert repr(Path(params)) == "Path(Ellipsis)" + + +def test_query_repr(params): + assert repr(Query(params)) == "Query(" + str(params) + ")" + + +def test_header_repr(params): + assert repr(Header(params)) == "Header(" + str(params) + ")" + + +def test_cookie_repr(params): + assert repr(Cookie(params)) == "Cookie(" + str(params) + ")" + + +def test_body_repr(params): + assert repr(Body(params)) == "Body(" + str(params) + ")" + + +def test_depends_repr(): + assert repr(Depends()) == "Depends(NoneType)" + assert repr(Depends(get_user)) == "Depends(get_user)" + assert repr(Depends(use_cache=False)) == "Depends(NoneType, use_cache=False)" + assert ( + repr(Depends(get_user, use_cache=False)) == "Depends(get_user, use_cache=False)" + )