-
Notifications
You must be signed in to change notification settings - Fork 14
feat: Added support for faststream >= 0.6.0 #205
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b1489db
feat: Added support for faststream >= 0.6.0
xelandernt 1c35a2c
ci: Fix faststream installation.
xelandernt af7c92a
test: Fix tests.
xelandernt 6f22f6c
test: Fixed broker init.
xelandernt fde4dfc
Update that_depends/integrations/faststream.py
xelandernt 267e3c5
Revert "Update that_depends/integrations/faststream.py"
xelandernt 691032f
review: Change type hint for message.
xelandernt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -33,6 +33,9 @@ jobs: | |
| - "3.12" | ||
| - "3.13" | ||
| - "3.14" | ||
| faststream-version: | ||
| - "<0.6.0" | ||
| - ">=0.6.0" | ||
| steps: | ||
| - uses: actions/checkout@v5 | ||
| - uses: extractions/setup-just@v3 | ||
|
|
@@ -41,7 +44,7 @@ jobs: | |
| cache-dependency-glob: "**/pyproject.toml" | ||
| - run: uv python install ${{ matrix.python-version }} | ||
| - run: just install | ||
| - run: just test . --cov=. --cov-report xml | ||
| - run: uv run --with "faststream${{ matrix.faststream-version }}" pytest . --cov=. --cov-report xml | ||
| - uses: codecov/[email protected] | ||
| env: | ||
| CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} | ||
|
|
||
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -24,7 +24,7 @@ fastapi = [ | |
| "fastapi", | ||
| ] | ||
| faststream = [ | ||
| "faststream<0.6.0" | ||
| "faststream" | ||
| ] | ||
|
|
||
| [project.urls] | ||
|
|
||
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,58 +1,136 @@ | ||
| import typing | ||
| from importlib.metadata import version | ||
| from types import TracebackType | ||
| from typing import Any, Optional | ||
| from typing import Any, Final, Optional | ||
|
|
||
| from faststream import BaseMiddleware | ||
| from typing_extensions import override | ||
| from packaging.version import Version | ||
xelandernt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| from typing_extensions import deprecated, override | ||
|
|
||
| from that_depends import container_context | ||
| from that_depends.providers.context_resources import ContextScope, SupportsContext | ||
| from that_depends.utils import UNSET, Unset, is_set | ||
|
|
||
|
|
||
| class DIContextMiddleware(BaseMiddleware): | ||
| """Initializes the container context for faststream brokers.""" | ||
|
|
||
| def __init__( | ||
| self, | ||
| *context_items: SupportsContext[Any], | ||
| global_context: dict[str, Any] | Unset = UNSET, | ||
| scope: ContextScope | Unset = UNSET, | ||
| ) -> None: | ||
| """Initialize the container context middleware. | ||
|
|
||
| Args: | ||
| *context_items (SupportsContext[Any]): Context items to initialize. | ||
| global_context (dict[str, Any] | Unset): Global context to initialize the container. | ||
| scope (ContextScope | Unset): Context scope to initialize the container. | ||
|
|
||
| """ | ||
| super().__init__() | ||
| self._context: container_context | None = None | ||
| self._context_items = set(context_items) | ||
| self._global_context = global_context | ||
| self._scope = scope | ||
|
|
||
| @override | ||
| async def on_receive(self) -> None: | ||
| self._context = container_context( | ||
| *self._context_items, | ||
| scope=self._scope if is_set(self._scope) else None, | ||
| global_context=self._global_context if is_set(self._global_context) else None, | ||
| ) | ||
| await self._context.__aenter__() | ||
|
|
||
| @override | ||
| async def after_processed( | ||
| self, | ||
| exc_type: type[BaseException] | None = None, | ||
| exc_val: BaseException | None = None, | ||
| exc_tb: Optional["TracebackType"] = None, | ||
| ) -> bool | None: | ||
| if self._context is not None: | ||
| await self._context.__aexit__(exc_type, exc_val, exc_tb) | ||
| return None | ||
|
|
||
| def __call__(self, *args: typing.Any, **kwargs: typing.Any) -> "DIContextMiddleware": # noqa: ARG002, ANN401 | ||
| """Create an instance of DIContextMiddleware.""" | ||
| return DIContextMiddleware(*self._context_items, scope=self._scope, global_context=self._global_context) | ||
| _FASTSTREAM_MODULE_NAME: Final[str] = "faststream" | ||
| _FASTSTREAM_VERSION: Final[str] = version(_FASTSTREAM_MODULE_NAME) | ||
xelandernt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if Version(_FASTSTREAM_VERSION) >= Version("0.6.0"): # pragma: no cover | ||
| from faststream import BaseMiddleware, ContextRepo | ||
xelandernt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| from faststream._internal.types import AnyMsg | ||
|
|
||
| class DIContextMiddleware(BaseMiddleware): | ||
| """Initializes the container context for faststream brokers.""" | ||
|
|
||
| def __init__( | ||
| self, | ||
| *context_items: SupportsContext[Any], | ||
| msg: AnyMsg | None = None, | ||
| context: Optional["ContextRepo"] = None, | ||
| global_context: dict[str, Any] | Unset = UNSET, | ||
| scope: ContextScope | Unset = UNSET, | ||
| ) -> None: | ||
| """Initialize the container context middleware. | ||
|
|
||
| Args: | ||
| *context_items (SupportsContext[Any]): Context items to initialize. | ||
| msg (Any): Message object. | ||
| context (ContextRepo): Context repository. | ||
| global_context (dict[str, Any] | Unset): Global context to initialize the container. | ||
| scope (ContextScope | Unset): Context scope to initialize the container. | ||
|
|
||
| """ | ||
| super().__init__(msg, context=context) # type: ignore[arg-type] | ||
| self._context: container_context | None = None | ||
| self._context_items = set(context_items) | ||
| self._global_context = global_context | ||
| self._scope = scope | ||
|
|
||
| @override | ||
| async def on_receive(self) -> None: | ||
| self._context = container_context( | ||
| *self._context_items, | ||
| scope=self._scope if is_set(self._scope) else None, | ||
| global_context=self._global_context if is_set(self._global_context) else None, | ||
| ) | ||
| await self._context.__aenter__() | ||
|
|
||
| @override | ||
| async def after_processed( | ||
| self, | ||
| exc_type: type[BaseException] | None = None, | ||
| exc_val: BaseException | None = None, | ||
| exc_tb: Optional["TracebackType"] = None, | ||
| ) -> bool | None: | ||
| if self._context is not None: | ||
| await self._context.__aexit__(exc_type, exc_val, exc_tb) | ||
| return None | ||
|
|
||
| def __call__(self, msg: Any = None, **kwargs: Any) -> "DIContextMiddleware": # noqa: ANN401 | ||
| """Create an instance of DIContextMiddleware. | ||
|
|
||
| Args: | ||
| msg (Any): Message object. | ||
| **kwargs: Additional keyword arguments. | ||
|
|
||
| Returns: | ||
| DIContextMiddleware: A new instance of DIContextMiddleware. | ||
|
|
||
| """ | ||
| context = kwargs.get("context") | ||
|
|
||
| return DIContextMiddleware( | ||
| *self._context_items, | ||
| msg=msg, | ||
| context=context, | ||
| scope=self._scope, | ||
| global_context=self._global_context, | ||
| ) | ||
xelandernt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| else: # pragma: no cover | ||
| from faststream import BaseMiddleware | ||
|
|
||
| @deprecated("Will be removed with faststream v1") | ||
| class DIContextMiddleware(BaseMiddleware): # type: ignore[no-redef] | ||
| """Initializes the container context for faststream brokers.""" | ||
|
|
||
| def __init__( | ||
| self, | ||
| *context_items: SupportsContext[Any], | ||
| global_context: dict[str, Any] | Unset = UNSET, | ||
| scope: ContextScope | Unset = UNSET, | ||
| ) -> None: | ||
| """Initialize the container context middleware. | ||
|
|
||
| Args: | ||
| *context_items (SupportsContext[Any]): Context items to initialize. | ||
| global_context (dict[str, Any] | Unset): Global context to initialize the container. | ||
| scope (ContextScope | Unset): Context scope to initialize the container. | ||
|
|
||
| """ | ||
| super().__init__() # type: ignore[call-arg] | ||
| self._context: container_context | None = None | ||
| self._context_items = set(context_items) | ||
| self._global_context = global_context | ||
| self._scope = scope | ||
|
|
||
| @override | ||
| async def on_receive(self) -> None: | ||
| self._context = container_context( | ||
| *self._context_items, | ||
| scope=self._scope if is_set(self._scope) else None, | ||
| global_context=self._global_context if is_set(self._global_context) else None, | ||
| ) | ||
| await self._context.__aenter__() | ||
|
|
||
| @override | ||
| async def after_processed( | ||
| self, | ||
| exc_type: type[BaseException] | None = None, | ||
| exc_val: BaseException | None = None, | ||
| exc_tb: Optional["TracebackType"] = None, | ||
| ) -> bool | None: | ||
| if self._context is not None: | ||
| await self._context.__aexit__(exc_type, exc_val, exc_tb) | ||
| return None | ||
|
|
||
| def __call__(self, *args: typing.Any, **kwargs: typing.Any) -> "DIContextMiddleware": # noqa: ARG002, ANN401 | ||
| """Create an instance of DIContextMiddleware.""" | ||
| return DIContextMiddleware(*self._context_items, scope=self._scope, global_context=self._global_context) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.