Skip to content
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
27 changes: 5 additions & 22 deletions anydi_django/apps.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
from __future__ import annotations

import types
from collections.abc import Callable
from typing import cast

import anydi
from anydi._container import import_container
from django.apps import AppConfig
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
Expand All @@ -25,33 +24,17 @@ def __init__(self, app_name: str, app_module: types.ModuleType | None) -> None:

def _make_container(self) -> anydi.Container:
"""Create the AnyDI container configured for this app."""
container_factory_path = self.settings["CONTAINER_FACTORY"]
if not container_factory_path:
container_path = self.settings["CONTAINER_FACTORY"]
if not container_path:
return anydi.Container()

try:
factory = import_string(container_factory_path)
return import_container(container_path)
except ImportError as exc:
raise ImproperlyConfigured(
f"Cannot import container factory '{container_factory_path}'."
f"Cannot import container factory '{container_path}'."
) from exc

if isinstance(factory, anydi.Container):
return factory

if callable(factory):
container = cast(Callable[[], anydi.Container], factory)()
if not isinstance(container, anydi.Container):
raise ImproperlyConfigured(
f"Container factory '{container_factory_path}' must return an "
"anydi.Container instance."
)
return container

raise ImproperlyConfigured(
f"Cannot import container factory '{container_factory_path}'."
)

def ready(self) -> None: # noqa: C901
# Register Django settings
if self.settings["REGISTER_SETTINGS"]:
Expand Down
1 change: 0 additions & 1 deletion anydi_django/ninja/_signature.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from collections.abc import Callable
from typing import Any

from anydi._types import is_inject_marker # noqa
from django.http import HttpResponse
from ninja.signature.details import (
FuncParam, # noqa
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ description = "Add your description here"
readme = "README.md"
requires-python = ">=3.10"
dependencies = [
"anydi>=0.56.0",
"anydi>=0.57.0",
"django>=4.2",
]

Expand Down
15 changes: 0 additions & 15 deletions tests/test_apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
import importlib

import anydi
import pytest
from django.conf import settings as django_settings
from django.core.exceptions import ImproperlyConfigured
from django.test import override_settings

from anydi_django.apps import ContainerConfig
Expand Down Expand Up @@ -48,16 +46,3 @@ def test_container_factory_callable_returns_container() -> None:
config = _make_config()

assert config.container is factories.CALLABLE_CONTAINER


def test_container_factory_callable_must_return_container() -> None:
with override_settings(
ANYDI={
**django_settings.ANYDI,
"CONTAINER_FACTORY": "tests.factories.bad_container_factory",
}
):
with pytest.raises(
ImproperlyConfigured, match="must return an anydi.Container"
):
_make_config()
Loading