Merge branch 'release/4.4.1' into master

This commit is contained in:
Roman Mogylatov 2020-11-17 23:59:44 -05:00
commit ee965f9782
7 changed files with 58 additions and 9 deletions

View File

@ -7,6 +7,12 @@ that were made in every particular version.
From version 0.7.6 *Dependency Injector* framework strictly
follows `Semantic versioning`_
4.4.1
-----
- Improve ``FastAPI`` integration: handle ``Depends(Provide[...])``.
- Update ``FastAPI`` example.
- Remove a typo from the ``Flask`` tutorial.
4.4.0
-----
- Add ``@inject`` decorator. It helps to fix a number of wiring bugs and make wiring be more resilient.

View File

@ -708,7 +708,6 @@ Edit ``views.py``:
.. code-block:: python
:emphasize-lines: 4,6-7,10-11,15
:emphasize-lines: 4,6-7,10-11,15
"""Views module."""

View File

@ -113,9 +113,9 @@ The output should be something like:
giphynavigator/__init__.py 0 0 100%
giphynavigator/application.py 13 0 100%
giphynavigator/containers.py 6 0 100%
giphynavigator/endpoints.py 6 0 100%
giphynavigator/endpoints.py 20 0 100%
giphynavigator/giphy.py 14 9 36%
giphynavigator/services.py 9 1 89%
giphynavigator/tests.py 38 0 100%
---------------------------------------------------
TOTAL 86 10 88%
TOTAL 100 10 90%

View File

@ -14,7 +14,7 @@ def create_app() -> FastAPI:
app = FastAPI()
app.container = container
app.add_api_route('/', endpoints.index)
app.include_router(endpoints.router)
return app

View File

@ -1,17 +1,42 @@
"""Endpoints module."""
from typing import Optional, List
from fastapi import APIRouter, Depends
from pydantic import BaseModel
from dependency_injector.wiring import inject, Provide
from .services import SearchService
from .containers import Container
class Gif(BaseModel):
url: str
class Response(BaseModel):
query: str
limit: int
gifs: List[Gif]
router = APIRouter()
@router.get('/', response_model=Response)
@inject
async def index(
query: str = Provide[Container.config.default.query],
limit: int = Provide[Container.config.default.limit.as_int()],
search_service=Provide[Container.search_service],
query: Optional[str] = None,
limit: Optional[str] = None,
default_query: str = Depends(Provide[Container.config.default.query]),
default_limit: int = Depends(Provide[Container.config.default.limit.as_int()]),
search_service: SearchService = Depends(Provide[Container.search_service]),
):
query = query or default_query
limit = limit or default_limit
gifs = await search_service.search(query, limit)
return {
'query': query,
'limit': limit,

View File

@ -1,6 +1,6 @@
"""Top-level package."""
__version__ = '4.4.0'
__version__ = '4.4.1'
"""Version number.
:type: str

View File

@ -27,6 +27,13 @@ else:
...
try:
from fastapi.params import Depends as FastAPIDepends
fastapi_installed = True
except ImportError:
fastapi_installed = False
from . import providers
@ -320,10 +327,15 @@ def _fetch_reference_injections(
injections = {}
closing = {}
for parameter_name, parameter in signature.parameters.items():
if not isinstance(parameter.default, _Marker):
if not isinstance(parameter.default, _Marker) \
and not _is_fastapi_depends(parameter.default):
continue
marker = parameter.default
if _is_fastapi_depends(marker):
marker = marker.dependency
if isinstance(marker, Closing):
marker = marker.provider
closing[parameter_name] = marker
@ -435,6 +447,10 @@ def _is_fastapi_default_arg_injection(injection, kwargs):
return injection in kwargs and isinstance(kwargs[injection], _Marker)
def _is_fastapi_depends(param: Any) -> bool:
return fastapi_installed and isinstance(param, FastAPIDepends)
def _is_patched(fn):
return getattr(fn, '__wired__', False) is True
@ -459,6 +475,9 @@ class _Marker(Generic[T], metaclass=ClassGetItemMeta):
def __class_getitem__(cls, item) -> T:
return cls(item)
def __call__(self) -> T:
return self
class Provide(_Marker):
...