raise exception instead of asserting

This commit is contained in:
Your Name 2022-09-12 18:45:58 +01:00
parent a4f93df9c7
commit 89794fca6a
2 changed files with 9 additions and 12 deletions

View File

@ -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)

View File

@ -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")