Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Injected annotation #17

Merged
merged 5 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion aioinject/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from aioinject.containers import Container
from aioinject.context import InjectionContext, SyncInjectionContext
from aioinject.decorators import inject
from aioinject.markers import Inject
from aioinject.markers import Inject, Injected
from aioinject.providers import (
Object,
Provider,
Expand All @@ -22,6 +22,7 @@
"SyncInjectionContext",
"Transient",
"inject",
"Injected",
]

__version__ = "0.34.0"
13 changes: 13 additions & 0 deletions aioinject/markers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
import dataclasses
from typing import TYPE_CHECKING, Annotated, Generic, TypeAlias, TypeVar


@dataclasses.dataclass(slots=True)
class Inject:
pass


T = TypeVar("T")

if TYPE_CHECKING:
Injected: TypeAlias = Annotated[T, Inject]

else:

class Injected(Generic[T]):
def __class_getitem__(cls, item: object) -> object:
return Annotated[item, Inject]
5 changes: 2 additions & 3 deletions docs/code/integrations/aiogram_.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import asyncio
from typing import Annotated

from aiogram import Bot, Dispatcher, Router
from aiogram.filters import Command
from aiogram.types import Message
from benchmark.container import create_container

from aioinject import Inject, Object
from aioinject import Injected, Object
from aioinject.ext.aiogram import AioInjectMiddleware, inject


Expand All @@ -24,7 +23,7 @@ async def main() -> None:
@inject
async def start(
message: Message,
value: Annotated[int, Inject],
value: Injected[int],
) -> None:
await message.reply(f"Injected value is {value}")

Expand Down
5 changes: 2 additions & 3 deletions docs/code/integrations/fastapi_.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import contextlib
from collections.abc import AsyncIterator
from typing import Annotated

import uvicorn
from fastapi import FastAPI

import aioinject
from aioinject import Inject
from aioinject import Injected
from aioinject.ext.fastapi import AioInjectMiddleware, inject


Expand All @@ -26,7 +25,7 @@ def create_app() -> FastAPI:

@app.get("/")
@inject
async def root(number: Annotated[int, Inject]) -> int:
async def root(number: Injected[int]) -> int:
return number

return app
Expand Down
6 changes: 2 additions & 4 deletions docs/code/integrations/litestar_.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
from typing import Annotated

import uvicorn
from litestar import Litestar, get

import aioinject
from aioinject import Inject
from aioinject import Injected
from aioinject.ext.litestar import AioInjectPlugin, inject


Expand All @@ -15,7 +13,7 @@
@get("/")
@inject
async def function_route(
number: Annotated[int, Inject],
number: Injected[int],
) -> int:
return number

Expand Down
6 changes: 3 additions & 3 deletions docs/code/integrations/strawberry-graphql.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from typing import Annotated, Any
from typing import Any

import strawberry
import uvicorn
from strawberry import Schema
from strawberry.asgi import GraphQL

import aioinject
from aioinject import Inject
from aioinject import Injected
from aioinject.ext.strawberry import AioInjectExtension, inject # (1)!


Expand All @@ -18,7 +18,7 @@
class Query:
@strawberry.field
@inject
async def number(self, number: Annotated[int, Inject]) -> int:
async def number(self, number: Injected[int]) -> int:
return number


Expand Down
14 changes: 14 additions & 0 deletions tests/test_inject.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from aioinject import Container, Inject, Object, Scoped, inject, providers
from aioinject.context import container_var
from aioinject.decorators import InjectMethod
from aioinject.markers import Injected


class _Session:
Expand Down Expand Up @@ -57,6 +58,19 @@ def test_simple_inject(container: Container) -> None:
assert isinstance(session, _Session)


def test_simple_inject_with_injected(container: Container) -> None:
@inject
def injectee(
test: Injected[_Session],
test_no_cache: Injected[_Session],
) -> tuple[_Session, _Session]:
return test, test_no_cache

with container.sync_context():
session, *_ = _injectee() # type: ignore[call-arg]
assert isinstance(session, _Session)


async def test_simple_service(container: Container) -> None:
with container.sync_context() as ctx:
session = ctx.resolve(_Session)
Expand Down
2 changes: 1 addition & 1 deletion tests/validation/test_dependency_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@ def test_err() -> None:
assert len(exc_info.value.errors) == 1
err = exc_info.value.errors[0]
assert isinstance(err, DependencyNotFoundError)
assert err.dependency == int
assert err.dependency is int