-
Notifications
You must be signed in to change notification settings - Fork 173
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: FastAPI 0.112.3 compatibility (#1763)
* fix: FastAPI 0.112.3 compatibility * chore: bump FastAPI * lint: fix mypy for latests FastAPI * chore: bump version
- Loading branch information
Showing
5 changed files
with
131 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
"""Simple and fast framework to create message brokers based microservices.""" | ||
|
||
__version__ = "0.5.20" | ||
__version__ = "0.5.21" | ||
|
||
SERVICE_NAME = f"faststream-{__version__}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
from dataclasses import dataclass | ||
from typing import TYPE_CHECKING, Any, List, Optional | ||
|
||
from fastapi import __version__ as FASTAPI_VERSION # noqa: N812 | ||
from fastapi.dependencies.utils import solve_dependencies | ||
from starlette.background import BackgroundTasks | ||
from typing_extensions import Never | ||
|
||
from faststream.types import AnyDict | ||
|
||
if TYPE_CHECKING: | ||
from fastapi.dependencies.models import Dependant | ||
from fastapi.requests import Request | ||
|
||
major, minor, patch, *_ = map(int, FASTAPI_VERSION.split(".")) | ||
FASTAPI_V2 = major > 0 or minor > 100 | ||
FASTAPI_V106 = major > 0 or minor >= 106 | ||
FASTAPI_v102_3 = major > 0 or minor > 112 or (minor == 112 and patch > 2) | ||
|
||
__all__ = ( | ||
"create_response_field", | ||
"solve_faststream_dependency", | ||
"raise_fastapi_validation_error", | ||
"RequestValidationError", | ||
) | ||
|
||
|
||
@dataclass | ||
class SolvedDependency: | ||
values: AnyDict | ||
errors: List[Any] | ||
background_tasks: Optional[BackgroundTasks] | ||
|
||
|
||
if FASTAPI_V2: | ||
from fastapi._compat import _normalize_errors | ||
from fastapi.exceptions import RequestValidationError | ||
|
||
def raise_fastapi_validation_error(errors: List[Any], body: AnyDict) -> Never: | ||
raise RequestValidationError(_normalize_errors(errors), body=body) | ||
|
||
else: | ||
from pydantic import ( # type: ignore[assignment] | ||
ValidationError as RequestValidationError, | ||
) | ||
from pydantic import create_model | ||
|
||
ROUTER_VALIDATION_ERROR_MODEL = create_model("StreamRoute") | ||
|
||
def raise_fastapi_validation_error(errors: List[Any], body: AnyDict) -> Never: | ||
raise RequestValidationError(errors, ROUTER_VALIDATION_ERROR_MODEL) # type: ignore[misc] | ||
|
||
|
||
if FASTAPI_v102_3: | ||
from fastapi.utils import ( | ||
create_model_field as create_response_field, | ||
) | ||
|
||
async def solve_faststream_dependency( | ||
request: "Request", | ||
dependant: "Dependant", | ||
dependency_overrides_provider: Optional[Any], | ||
**kwargs: Any, | ||
) -> SolvedDependency: | ||
solved_result = await solve_dependencies( | ||
request=request, | ||
body=request._body, # type: ignore[arg-type] | ||
dependant=dependant, | ||
dependency_overrides_provider=dependency_overrides_provider, | ||
**kwargs, | ||
) | ||
values, errors, background = ( | ||
solved_result.values, | ||
solved_result.errors, | ||
solved_result.background_tasks, | ||
) | ||
|
||
return SolvedDependency( | ||
values=values, | ||
errors=errors, | ||
background_tasks=background, | ||
) | ||
|
||
else: | ||
from fastapi.utils import ( # type: ignore[attr-defined,no-redef] | ||
create_response_field as create_response_field, | ||
) | ||
|
||
async def solve_faststream_dependency( | ||
request: "Request", | ||
dependant: "Dependant", | ||
dependency_overrides_provider: Optional[Any], | ||
**kwargs: Any, | ||
) -> SolvedDependency: | ||
solved_result = await solve_dependencies( | ||
request=request, | ||
body=request._body, # type: ignore[arg-type] | ||
dependant=dependant, | ||
dependency_overrides_provider=dependency_overrides_provider, | ||
**kwargs, | ||
) | ||
|
||
( | ||
values, | ||
errors, | ||
background, | ||
_response, | ||
_dependency_cache, | ||
) = solved_result # type: ignore[misc] | ||
|
||
return SolvedDependency( | ||
values=values, # type: ignore[has-type] | ||
errors=errors, # type: ignore[has-type] | ||
background_tasks=background, # type: ignore[has-type] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters