Skip to content

Commit

Permalink
Fix django integration
Browse files Browse the repository at this point in the history
  • Loading branch information
maldoinc committed Oct 19, 2024
1 parent 29a9755 commit da3092b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
2 changes: 1 addition & 1 deletion test/integration/django/view.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from django.http import HttpRequest, HttpResponse
from django.views import View
from typing_extensions import Annotated
from wireup import Inject, container
from wireup import Inject

from test.integration.django.service.greeter_interface import GreeterService
from test.integration.django.service.random_service import RandomService
Expand Down
28 changes: 18 additions & 10 deletions wireup/integration/django/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@

import wireup
from wireup import DependencyContainer
from wireup.ioc._exit_stack import clean_exit_stack

if TYPE_CHECKING:
from django.http import HttpRequest

from wireup.integration.django import WireupSettings
from wireup.ioc.dependency_container import _InjectionResult


class WireupConfig(AppConfig):
Expand Down Expand Up @@ -62,16 +64,22 @@ def _autowire_class_based_view(self, callback: Any) -> Any:

# This is taken from the django .as_view() method.
def view(request: HttpRequest, *args: Any, **kwargs: Any) -> Any:
autowired_args = self.container._DependencyContainer__callable_get_params_to_inject(callback.view_class) # type: ignore[reportAttributeAccessIssue] # noqa: SLF001

this = callback.view_class(**{**callback.view_initkwargs, **autowired_args})
this.setup(request, *args, **kwargs)
if not hasattr(this, "request"):
raise AttributeError(
"{} instance has no 'request' attribute. Did you override " # noqa: EM103, UP032
"setup() and forget to call super()?".format(callback.view_class.__name__)
)
return this.dispatch(request, *args, **kwargs)
autowired_args: _InjectionResult = self.container._DependencyContainer__callable_get_params_to_inject( # type: ignore[reportAttributeAccessIssue] # noqa: SLF001
callback.view_class
)

this = callback.view_class(**{**callback.view_initkwargs, **autowired_args.args})
try:
this.setup(request, *args, **kwargs)
if not hasattr(this, "request"):
raise AttributeError(
"{} instance has no 'request' attribute. Did you override " # noqa: EM103, UP032
"setup() and forget to call super()?".format(callback.view_class.__name__)
)
return this.dispatch(request, *args, **kwargs)
finally:
if autowired_args.exit_stack:
clean_exit_stack(autowired_args.exit_stack)

return view

Expand Down
2 changes: 1 addition & 1 deletion wireup/ioc/dependency_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def get(self, klass: type[T], qualifier: Qualifier | None = None) -> T:
msg = "Container.get does not support Transient lifetime service generator factories."
raise WireupError(msg)

return res.instance
return res.instance # type: ignore[no-any-return]

raise UnknownServiceRequestedError(klass)

Expand Down

0 comments on commit da3092b

Please sign in to comment.