-
-
Notifications
You must be signed in to change notification settings - Fork 387
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(DI): Support externally typed classes as dependency providers (#…
…3066) * Support injecting externally typed classes
- Loading branch information
1 parent
1966c4d
commit e6eb9f2
Showing
16 changed files
with
326 additions
and
32 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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from inspect import Parameter, Signature | ||
from typing import Any, Dict, Tuple | ||
|
||
from litestar import Litestar, get | ||
from litestar.di import Provide | ||
from litestar.plugins import DIPlugin | ||
|
||
|
||
class MyBaseType: | ||
def __init__(self, param): | ||
self.param = param | ||
|
||
|
||
class MyDIPlugin(DIPlugin): | ||
def has_typed_init(self, type_: Any) -> bool: | ||
return issubclass(type_, MyBaseType) | ||
|
||
def get_typed_init(self, type_: Any) -> Tuple[Signature, Dict[str, Any]]: | ||
signature = Signature([Parameter(name="param", kind=Parameter.POSITIONAL_OR_KEYWORD)]) | ||
annotations = {"param": str} | ||
return signature, annotations | ||
|
||
|
||
@get("/", dependencies={"injected": Provide(MyBaseType, sync_to_thread=False)}) | ||
async def handler(injected: MyBaseType) -> str: | ||
return injected.param | ||
|
||
|
||
app = Litestar(route_handlers=[handler], plugins=[MyDIPlugin()]) | ||
|
||
# run: /?param=hello |
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
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,26 @@ | ||
from __future__ import annotations | ||
|
||
import inspect | ||
from inspect import Signature | ||
from typing import Any | ||
|
||
from litestar.contrib.pydantic.utils import is_pydantic_model_class | ||
from litestar.plugins import DIPlugin | ||
|
||
|
||
class PydanticDIPlugin(DIPlugin): | ||
def has_typed_init(self, type_: Any) -> bool: | ||
return is_pydantic_model_class(type_) | ||
|
||
def get_typed_init(self, type_: Any) -> tuple[Signature, dict[str, Any]]: | ||
try: | ||
model_fields = dict(type_.model_fields) | ||
except AttributeError: | ||
model_fields = {k: model_field.field_info for k, model_field in type_.__fields__.items()} | ||
|
||
parameters = [ | ||
inspect.Parameter(name=field_name, kind=inspect.Parameter.KEYWORD_ONLY, annotation=Any) | ||
for field_name in model_fields | ||
] | ||
type_hints = {field_name: Any for field_name in model_fields} | ||
return Signature(parameters), type_hints |
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
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,31 @@ | ||
from __future__ import annotations | ||
|
||
import inspect | ||
from inspect import Signature | ||
from typing import Any | ||
|
||
import msgspec | ||
|
||
from litestar.plugins import DIPlugin | ||
|
||
__all__ = ("MsgspecDIPlugin",) | ||
|
||
|
||
class MsgspecDIPlugin(DIPlugin): | ||
def has_typed_init(self, type_: Any) -> bool: | ||
return type(type_) is type(msgspec.Struct) # noqa: E721 | ||
|
||
def get_typed_init(self, type_: Any) -> tuple[Signature, dict[str, Any]]: | ||
parameters = [] | ||
type_hints = {} | ||
for field_info in msgspec.structs.fields(type_): | ||
type_hints[field_info.name] = field_info.type | ||
parameters.append( | ||
inspect.Parameter( | ||
name=field_info.name, | ||
kind=inspect.Parameter.KEYWORD_ONLY, | ||
annotation=field_info.type, | ||
default=field_info.default, | ||
) | ||
) | ||
return inspect.Signature(parameters), type_hints |
Oops, something went wrong.