Add check for ciruclar imports + test

This commit is contained in:
Javier Sánchez 2025-10-30 10:50:23 +01:00
parent 1fc586c3a5
commit 111de89957
2 changed files with 12 additions and 0 deletions

View File

@ -1333,6 +1333,7 @@ class APIRouter(routing.Router):
app.include_router(internal_router) app.include_router(internal_router)
``` ```
""" """
assert self is not router, "Cannot include router into itself"
if prefix: if prefix:
assert prefix.startswith("/"), "A path prefix must start with '/'" assert prefix.startswith("/"), "A path prefix must start with '/'"
assert not prefix.endswith("/"), ( assert not prefix.endswith("/"), (

View File

@ -0,0 +1,11 @@
import pytest
from fastapi import APIRouter, FastAPI
def test_router_circular_import():
app = FastAPI()
router = APIRouter()
app.include_router(router)
with pytest.raises(AssertionError, match="Router cannot be the same as parent"):
router.include_router(router)