mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2025-02-18 04:20:46 +03:00
Merge branch 'release/4.4.1' into master
This commit is contained in:
commit
ee965f9782
|
@ -7,6 +7,12 @@ that were made in every particular version.
|
||||||
From version 0.7.6 *Dependency Injector* framework strictly
|
From version 0.7.6 *Dependency Injector* framework strictly
|
||||||
follows `Semantic versioning`_
|
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
|
4.4.0
|
||||||
-----
|
-----
|
||||||
- Add ``@inject`` decorator. It helps to fix a number of wiring bugs and make wiring be more resilient.
|
- Add ``@inject`` decorator. It helps to fix a number of wiring bugs and make wiring be more resilient.
|
||||||
|
|
|
@ -708,7 +708,6 @@ Edit ``views.py``:
|
||||||
|
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
:emphasize-lines: 4,6-7,10-11,15
|
:emphasize-lines: 4,6-7,10-11,15
|
||||||
:emphasize-lines: 4,6-7,10-11,15
|
|
||||||
|
|
||||||
"""Views module."""
|
"""Views module."""
|
||||||
|
|
||||||
|
|
|
@ -113,9 +113,9 @@ The output should be something like:
|
||||||
giphynavigator/__init__.py 0 0 100%
|
giphynavigator/__init__.py 0 0 100%
|
||||||
giphynavigator/application.py 13 0 100%
|
giphynavigator/application.py 13 0 100%
|
||||||
giphynavigator/containers.py 6 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/giphy.py 14 9 36%
|
||||||
giphynavigator/services.py 9 1 89%
|
giphynavigator/services.py 9 1 89%
|
||||||
giphynavigator/tests.py 38 0 100%
|
giphynavigator/tests.py 38 0 100%
|
||||||
---------------------------------------------------
|
---------------------------------------------------
|
||||||
TOTAL 86 10 88%
|
TOTAL 100 10 90%
|
||||||
|
|
|
@ -14,7 +14,7 @@ def create_app() -> FastAPI:
|
||||||
|
|
||||||
app = FastAPI()
|
app = FastAPI()
|
||||||
app.container = container
|
app.container = container
|
||||||
app.add_api_route('/', endpoints.index)
|
app.include_router(endpoints.router)
|
||||||
return app
|
return app
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,17 +1,42 @@
|
||||||
"""Endpoints module."""
|
"""Endpoints module."""
|
||||||
|
|
||||||
|
from typing import Optional, List
|
||||||
|
|
||||||
|
from fastapi import APIRouter, Depends
|
||||||
|
from pydantic import BaseModel
|
||||||
from dependency_injector.wiring import inject, Provide
|
from dependency_injector.wiring import inject, Provide
|
||||||
|
|
||||||
|
from .services import SearchService
|
||||||
from .containers import Container
|
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
|
@inject
|
||||||
async def index(
|
async def index(
|
||||||
query: str = Provide[Container.config.default.query],
|
query: Optional[str] = None,
|
||||||
limit: int = Provide[Container.config.default.limit.as_int()],
|
limit: Optional[str] = None,
|
||||||
search_service=Provide[Container.search_service],
|
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)
|
gifs = await search_service.search(query, limit)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
'query': query,
|
'query': query,
|
||||||
'limit': limit,
|
'limit': limit,
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
"""Top-level package."""
|
"""Top-level package."""
|
||||||
|
|
||||||
__version__ = '4.4.0'
|
__version__ = '4.4.1'
|
||||||
"""Version number.
|
"""Version number.
|
||||||
|
|
||||||
:type: str
|
:type: str
|
||||||
|
|
|
@ -27,6 +27,13 @@ else:
|
||||||
...
|
...
|
||||||
|
|
||||||
|
|
||||||
|
try:
|
||||||
|
from fastapi.params import Depends as FastAPIDepends
|
||||||
|
fastapi_installed = True
|
||||||
|
except ImportError:
|
||||||
|
fastapi_installed = False
|
||||||
|
|
||||||
|
|
||||||
from . import providers
|
from . import providers
|
||||||
|
|
||||||
|
|
||||||
|
@ -320,10 +327,15 @@ def _fetch_reference_injections(
|
||||||
injections = {}
|
injections = {}
|
||||||
closing = {}
|
closing = {}
|
||||||
for parameter_name, parameter in signature.parameters.items():
|
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
|
continue
|
||||||
|
|
||||||
marker = parameter.default
|
marker = parameter.default
|
||||||
|
|
||||||
|
if _is_fastapi_depends(marker):
|
||||||
|
marker = marker.dependency
|
||||||
|
|
||||||
if isinstance(marker, Closing):
|
if isinstance(marker, Closing):
|
||||||
marker = marker.provider
|
marker = marker.provider
|
||||||
closing[parameter_name] = marker
|
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)
|
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):
|
def _is_patched(fn):
|
||||||
return getattr(fn, '__wired__', False) is True
|
return getattr(fn, '__wired__', False) is True
|
||||||
|
|
||||||
|
@ -459,6 +475,9 @@ class _Marker(Generic[T], metaclass=ClassGetItemMeta):
|
||||||
def __class_getitem__(cls, item) -> T:
|
def __class_getitem__(cls, item) -> T:
|
||||||
return cls(item)
|
return cls(item)
|
||||||
|
|
||||||
|
def __call__(self) -> T:
|
||||||
|
return self
|
||||||
|
|
||||||
|
|
||||||
class Provide(_Marker):
|
class Provide(_Marker):
|
||||||
...
|
...
|
||||||
|
|
Loading…
Reference in New Issue
Block a user