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

Fix type hinting in PyCharm #20

Merged
merged 2 commits into from
Nov 12, 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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Fixed
- Compatibility with PyCharm for FastAPI `Provide` type hints

### Removed
- Removed `Provider` class

## [0.2.1] - 2024-10-01
### Fixed
Expand Down
4 changes: 2 additions & 2 deletions src/magic_di/fastapi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
"""

from ._app import inject_app
from ._provide import Provide, Provider # type: ignore[attr-defined]
from ._provide import Provide # type: ignore[attr-defined]

__all__ = ("inject_app", "Provide", "Provider")
__all__ = ("inject_app", "Provide")
37 changes: 18 additions & 19 deletions src/magic_di/fastapi/_provide.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,25 @@
class FastAPIInjectionError(Exception): ...


class Provider:
def __getitem__(self, obj: type[T]) -> type[T]:
@lru_cache(maxsize=1)
def get_dependency(injector: DependencyInjector) -> T:
return injector.inject(obj)()

def inject(request: Request) -> T:
if not hasattr(request.app.state, "dependency_injector"):
error_msg = (
"FastAPI application is not injected. Did you forget to add `inject_app(app)`?"
)
raise FastAPIInjectionError(error_msg)
if TYPE_CHECKING:
from typing import Union as Provide # hack for mypy # noqa: FIX004
else:

injector = request.app.state.dependency_injector
return get_dependency(injector)
class Provide:
def __class_getitem__(cls, obj: T) -> T:
@lru_cache(maxsize=1)
def get_dependency(injector: DependencyInjector) -> T:
return injector.inject(obj)()

return Annotated[obj, Depends(inject), Injectable] # type: ignore[return-value]
def inject(request: Request) -> T:
if not hasattr(request.app.state, "dependency_injector"):
error_msg = (
"FastAPI application is not injected. "
"Did you forget to add `inject_app(app)`?"
)
raise FastAPIInjectionError(error_msg)

injector = request.app.state.dependency_injector
return get_dependency(injector)

if TYPE_CHECKING:
from typing import Union as Provide # hack for mypy # noqa: FIX004
else:
Provide = Provider()
return Annotated[obj, Depends(inject), Injectable] # type: ignore[return-value]
Loading