From 89794fca6ab0cb7f4b6928479067a89413108eb9 Mon Sep 17 00:00:00 2001 From: Your Name Date: Mon, 12 Sep 2022 18:45:58 +0100 Subject: [PATCH] raise exception instead of asserting --- fastapi/routing.py | 15 ++++++--------- tests/test_tags.py | 6 +++--- 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/fastapi/routing.py b/fastapi/routing.py index e8e5ba788..5899af62a 100644 --- a/fastapi/routing.py +++ b/fastapi/routing.py @@ -500,9 +500,8 @@ class APIRouter(routing.Router): ), "A path prefix must not end with '/', as the routes will start with '/'" self.prefix = prefix if tags: - assert not isinstance( - tags, str - ), """tags should not be a string: please use ["tag"] instead of "tag".""" + if isinstance(tags, str): + raise TypeError(f"""tags should not be a string: please use ["{tags}"] instead of "{tags}".""") self.tags: List[Union[str, Enum]] = tags or [] self.dependencies = list(dependencies or []) or [] self.deprecated = deprecated @@ -556,9 +555,8 @@ class APIRouter(routing.Router): ) current_tags = self.tags.copy() if tags: - assert not isinstance( - tags, str - ), """tags should not be a string: please use ["tag"] instead of "tag".""" + if isinstance(tags, str): + raise TypeError(f"""tags should not be a string: please use ["{tags}"] instead of "{tags}".""") current_tags.extend(tags) current_dependencies = self.dependencies.copy() if dependencies: @@ -723,9 +721,8 @@ class APIRouter(routing.Router): ) current_tags = [] if tags: - assert not isinstance( - tags, str - ), """tags should not be a string: please use ["tag"] instead of "tag".""" + if isinstance(tags, str): + raise TypeError(f"""tags should not be a string: please use ["{tags}"] instead of "{tags}".""") current_tags.extend(tags) if route.tags: current_tags.extend(route.tags) diff --git a/tests/test_tags.py b/tests/test_tags.py index 993b81430..3c5993ff3 100644 --- a/tests/test_tags.py +++ b/tests/test_tags.py @@ -3,13 +3,13 @@ from fastapi import APIRouter, FastAPI def test_string_is_invalid_in_router_tags(): - with pytest.raises(AssertionError): + with pytest.raises(TypeError): APIRouter(tags="test") def test_string_is_invalid_in_router_route_tags(): router = APIRouter() - with pytest.raises(AssertionError): + with pytest.raises(TypeError): @router.get("", tags="test") def test(): ... @@ -23,5 +23,5 @@ def test_string_is_invalid_in_include_router_tags(): def test(): ... - with pytest.raises(AssertionError): + with pytest.raises(TypeError): app.include_router(router, prefix="/test", tags="test") \ No newline at end of file