Skip to content

Commit

Permalink
fix circular import errors in settings with edgy
Browse files Browse the repository at this point in the history
Recent changes made it more likely to hit circular import errors when
providing settings.
This PR fixes the problem by leveraging the new more lenient monkay 0.2 version and
initialize the settings in cases of import errors later.
  • Loading branch information
devkral committed Jan 7, 2025
1 parent d1021f7 commit ad437a5
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 14 deletions.
8 changes: 8 additions & 0 deletions docs/release-notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ hide:

# Release Notes


## 0.24.2

### Fixed

- Try harder to avoid circular imports when providing settings with edgy references.


## 0.24.1

### Fixed
Expand Down
5 changes: 1 addition & 4 deletions docs_src/tips/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,14 @@ def disable_edgy_settings_load():


def get_application():
"""
This is optional. The function is only used for organisation purposes.
"""
build_path()
# this is optional, for rewiring edgy settings to esmerald settings
disable_edgy_settings_load() # disable any settings load
from edgy import Instance, monkay
from esmerald.conf import settings

monkay.settings = lambda: settings.edgy_settings # rewire
monkay.evaluate_settings()
monkay.evaluate_settings_once(ignore_import_errors=False)

# now the project is in the search path and we can import
from my_project.utils import get_db_connection
Expand Down
4 changes: 3 additions & 1 deletion edgy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

__version__ = "0.24.1"
__version__ = "0.24.2"
from typing import TYPE_CHECKING

from ._monkay import Instance, create_monkay
Expand Down Expand Up @@ -109,6 +109,8 @@

def get_migration_prepared_registry(registry: Registry | None = None) -> Registry:
"""Get registry with applied restrictions, usable for migrations."""
# ensure settings are ready
monkay.evaluate_settings_once(ignore_import_errors=False)
if registry is None:
instance = monkay.instance
assert instance is not None
Expand Down
3 changes: 3 additions & 0 deletions edgy/cli/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ def add_app_module_option(fn: Any) -> Any:

def callback(ctx: click.Context, param: str, value: Optional[str]) -> None:
import edgy
from edgy.conf import evaluate_settings_once_ready

evaluate_settings_once_ready()

if ctx.invoked_subcommand in COMMANDS_WITHOUT_APP:
return
Expand Down
36 changes: 30 additions & 6 deletions edgy/conf/__init__.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,39 @@
from typing import Any
from __future__ import annotations

from .global_settings import EdgySettings
from functools import lru_cache
from typing import TYPE_CHECKING, Any, cast

if TYPE_CHECKING:
from monkay import Monkay

from edgy import EdgySettings, Instance


@lru_cache
def get_edgy_monkay() -> Monkay[Instance, EdgySettings]:
from edgy import monkay

monkay.evaluate_settings_once(on_conflict="error", ignore_import_errors=False)

return monkay


class SettingsForward:
def __getattribute__(self, name: str) -> Any:
from edgy import monkay

monkay = get_edgy_monkay()
return getattr(monkay.settings, name)


settings: EdgySettings = SettingsForward() # type: ignore
settings: EdgySettings = cast("EdgySettings", SettingsForward())


def evaluate_settings_once_ready() -> None:
"""
Call when settings must be ready.
This doesn't prevent the settings being updated later or set before.
"""
get_edgy_monkay()


__all__ = ["settings"]
__all__ = ["settings", "evaluate_settings_once_ready"]
2 changes: 2 additions & 0 deletions edgy/core/connection/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from sqlalchemy.ext.asyncio.engine import AsyncEngine
from sqlalchemy.orm import declarative_base as sa_declarative_base

from edgy.conf import evaluate_settings_once_ready
from edgy.core.connection.database import Database, DatabaseURL
from edgy.core.connection.schemas import Schema
from edgy.core.utils.sync import run_sync
Expand Down Expand Up @@ -115,6 +116,7 @@ def __init__(
extra: Optional[dict[str, Database]] = None,
**kwargs: Any,
) -> None:
evaluate_settings_once_ready()
self.db_schema = schema
extra = extra or {}
self.database: Database = (
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ classifiers = [
"Programming Language :: Python :: 3.12",
]
dependencies = [
"monkay>=0.0.9",
"monkay>=0.2.1",
"orjson",
"alembic>=1.11.3,<2.0.0",
"click>=8.1.3,<9.0.0",
Expand Down
2 changes: 1 addition & 1 deletion tests/settings/multidb.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Union

from edgy.conf.global_settings import EdgySettings
from edgy import EdgySettings


class TestSettings(EdgySettings):
Expand Down
2 changes: 1 addition & 1 deletion tests/settings/multidb_nonidentifier.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Union

from edgy.conf.global_settings import EdgySettings
from edgy import EdgySettings


class TestSettings(EdgySettings):
Expand Down

0 comments on commit ad437a5

Please sign in to comment.