diff --git a/packages/modern-di-faststream/tests_faststream/dependencies.py b/packages/modern-di-faststream/tests_faststream/dependencies.py index ce2ee97..0a78d17 100644 --- a/packages/modern-di-faststream/tests_faststream/dependencies.py +++ b/packages/modern-di-faststream/tests_faststream/dependencies.py @@ -15,8 +15,8 @@ class DependentCreator: dep1: SimpleCreator -def fetch_message_is_processed_from_request(message: StreamMessage[typing.Any]) -> bool: - return message.processed +def fetch_message_is_processed_from_request(message: StreamMessage[typing.Any] | None = None) -> bool: + return message.processed if message else False class Dependencies(Group): diff --git a/packages/modern-di-litestar/tests_litestar/dependencies.py b/packages/modern-di-litestar/tests_litestar/dependencies.py index a503cfd..e40352d 100644 --- a/packages/modern-di-litestar/tests_litestar/dependencies.py +++ b/packages/modern-di-litestar/tests_litestar/dependencies.py @@ -15,12 +15,12 @@ class DependentCreator: dep1: SimpleCreator -def fetch_method_from_request(request: litestar.Request[typing.Any, typing.Any, typing.Any]) -> str: +def fetch_method_from_request(request: litestar.Request[typing.Any, typing.Any, typing.Any] | None = None) -> str: assert isinstance(request, litestar.Request) return request.method -def fetch_url_from_websocket(websocket: litestar.WebSocket[typing.Any, typing.Any, typing.Any]) -> str: +def fetch_url_from_websocket(websocket: litestar.WebSocket[typing.Any, typing.Any, typing.Any] | None = None) -> str: assert isinstance(websocket, litestar.WebSocket) return websocket.url.path diff --git a/packages/modern-di/modern_di/types_parser.py b/packages/modern-di/modern_di/types_parser.py index 83df6c4..9bdfd91 100644 --- a/packages/modern-di/modern_di/types_parser.py +++ b/packages/modern-di/modern_di/types_parser.py @@ -14,29 +14,40 @@ class SignatureItem: default: object = UNSET @classmethod - def from_type(cls, type_: type, default: object = UNSET) -> "SignatureItem": + def from_type(cls, type_: type, default: object = UNSET) -> "SignatureItem": # noqa: C901 if type_ is types.NoneType: return cls() + # typing.Annotated if isinstance(type_, typing._AnnotatedAlias): # type: ignore[attr-defined] # noqa: SLF001 type_ = type_.__args__[0] result: dict[str, typing.Any] = {"default": default} + + # union type if isinstance(type_, (types.UnionType, typing._UnionGenericAlias)): # type: ignore[attr-defined] # noqa: SLF001 args = [x.__origin__ if isinstance(x, types.GenericAlias) else x for x in type_.__args__] if types.NoneType in args: result["is_nullable"] = True args.remove(types.NoneType) + + for i, arg in enumerate(args): + if isinstance(arg, (types.GenericAlias, typing._GenericAlias)): # type: ignore[attr-defined] # noqa: SLF001 + args[i] = arg.__origin__ + if len(args) > 1: result["args"] = args elif args: result["arg_type"] = args[0] + + # generic elif isinstance(type_, (types.GenericAlias, typing._GenericAlias)): # type: ignore[attr-defined] # noqa: SLF001 result["arg_type"] = type_.__origin__ result["args"] = list(type_.__args__) elif isinstance(type_, type): result["arg_type"] = type_ + return cls(**result) diff --git a/packages/modern-di/tests_core/test_types_parser.py b/packages/modern-di/tests_core/test_types_parser.py index a40be7a..733dc44 100644 --- a/packages/modern-di/tests_core/test_types_parser.py +++ b/packages/modern-di/tests_core/test_types_parser.py @@ -22,6 +22,7 @@ class GenericClass(typing.Generic[types.T]): ... (typing.Union[str | int], SignatureItem(args=[str, int])), # noqa: UP007 (list[str] | None, SignatureItem(arg_type=list, is_nullable=True)), (GenericClass[str], SignatureItem(arg_type=GenericClass, args=[str])), + (GenericClass[str] | None, SignatureItem(arg_type=GenericClass, is_nullable=True)), ], ) def test_signature_item_parser(type_: type, result: SignatureItem) -> None: