Skip to content

Commit 18e5d05

Browse files
paradoxxxzeroTisho99
authored andcommitted
[FIX] fastapi: Change circular import fix to avoid crash with fastapi>=0.123.7
Remove TYPE_CHECKING import condition and delay dependencies import to solve the circular import
1 parent 2dc6ba5 commit 18e5d05

2 files changed

Lines changed: 11 additions & 8 deletions

File tree

fastapi/dependencies.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Copyright 2022 ACSONE SA/NV
22
# License LGPL-3.0 or later (http://www.gnu.org/licenses/LGPL).
33

4-
from typing import TYPE_CHECKING, Annotated
4+
from typing import Annotated
55

66
from odoo.api import Environment
77
from odoo.exceptions import AccessDenied
@@ -13,11 +13,9 @@
1313
from fastapi.security import HTTPBasic, HTTPBasicCredentials
1414

1515
from .context import odoo_env_ctx
16+
from .models.fastapi_endpoint import FastapiEndpoint
1617
from .schemas import Paging
1718

18-
if TYPE_CHECKING:
19-
from .models.fastapi_endpoint import FastapiEndpoint
20-
2119

2220
def company_id() -> int | None:
2321
"""This method may be overriden by the FastAPI app to set the allowed company

fastapi/models/fastapi_endpoint.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
from fastapi import APIRouter, Depends, FastAPI
1616

17-
from .. import dependencies
1817
from ..middleware import ASGIMiddleware
1918

2019
_logger = logging.getLogger(__name__)
@@ -309,9 +308,12 @@ def _get_app(self) -> FastAPI:
309308
return app
310309

311310
def _get_app_dependencies_overrides(self) -> dict[Callable, Callable]:
311+
# Import here to avoid circular import while waiting for lazy imports
312+
from ..dependencies import company_id, fastapi_endpoint_id
313+
312314
return {
313-
dependencies.fastapi_endpoint_id: partial(lambda a: a, self.id),
314-
dependencies.company_id: partial(lambda a: a, self.company_id.id),
315+
fastapi_endpoint_id: partial(lambda a: a, self.id),
316+
company_id: partial(lambda a: a, self.company_id.id),
315317
}
316318

317319
def _prepare_fastapi_app_params(self) -> dict[str, Any]:
@@ -336,4 +338,7 @@ def _get_fastapi_app_middlewares(self) -> list[Middleware]:
336338

337339
def _get_fastapi_app_dependencies(self) -> list[Depends]:
338340
"""Return the dependencies to use for the fastapi app."""
339-
return [Depends(dependencies.accept_language)]
341+
# Import here to avoid circular import too
342+
from ..dependencies import accept_language
343+
344+
return [Depends(accept_language)]

0 commit comments

Comments
 (0)