Skip to content

Commit 7310b51

Browse files
committed
fix: common.core.utils.TemporaryDirectory dependent on Django settings
1 parent 965b336 commit 7310b51

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

src/common/core/utils.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from functools import lru_cache
88
from itertools import cycle
99
from typing import (
10+
TYPE_CHECKING,
1011
Iterator,
1112
Literal,
1213
NotRequired,
@@ -17,19 +18,23 @@
1718

1819
from django.conf import settings
1920
from django.contrib.auth import get_user_model
20-
from django.contrib.auth.models import AbstractBaseUser
2121
from django.db import connections
22-
from django.db.models import Manager, Model
2322
from django.db.utils import OperationalError
2423

2524
from common.core import ReplicaReadStrategy
2625

26+
if TYPE_CHECKING:
27+
from django.contrib.auth.models import AbstractBaseUser
28+
from django.db.models.base import Model
29+
from django.db.models.manager import Manager
30+
31+
2732
logger = logging.getLogger(__name__)
2833

2934
UNKNOWN = "unknown"
3035
VERSIONS_INFO_FILE_LOCATION = ".versions.json"
3136

32-
ManagerType = TypeVar("ManagerType", bound=Manager[Model])
37+
ManagerType = TypeVar("ManagerType", bound="Manager[Model]")
3338

3439
ReplicaNamePrefix = Literal["replica_", "cross_region_replica_"]
3540
_replica_sequential_names_by_prefix: dict[ReplicaNamePrefix, Iterator[str]] = {}
@@ -102,7 +107,7 @@ def get_version_info() -> VersionInfo:
102107
version_json["image_tag"] = manifest_versions["."]
103108

104109
if not _is_saas:
105-
user_objects: Manager[AbstractBaseUser] = getattr(get_user_model(), "objects")
110+
user_objects: "Manager[AbstractBaseUser]" = getattr(get_user_model(), "objects")
106111

107112
version_json["self_hosted_data"] = {
108113
"has_users": user_objects.exists(),

tests/integration/core/test_main.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import subprocess
23
from pathlib import Path
34

45
import django
@@ -110,9 +111,11 @@ def test_main__healthcheck_http__server_invalid_response__runs_expected(
110111
main(argv)
111112

112113

113-
def test_main__prometheus_multiproc_remove_dir_on_exit_default__expected() -> None:
114+
def test_main__prometheus_multiproc_remove_dir_on_exit_default__expected(
115+
monkeypatch: pytest.MonkeyPatch,
116+
) -> None:
114117
# Given
115-
os.environ.pop("PROMETHEUS_MULTIPROC_DIR_KEEP", None)
118+
monkeypatch.delenv("PROMETHEUS_MULTIPROC_DIR_KEEP")
116119

117120
# When
118121
main(["flagsmith"])
@@ -122,14 +125,28 @@ def test_main__prometheus_multiproc_remove_dir_on_exit_default__expected() -> No
122125

123126

124127
def test_main__prometheus_multiproc_remove_dir_on_exit_true__expected(
128+
monkeypatch: pytest.MonkeyPatch,
125129
fs: FakeFilesystem,
126130
) -> None:
127131
# Given
128-
os.environ.pop("PROMETHEUS_MULTIPROC_DIR", None)
129-
os.environ["PROMETHEUS_MULTIPROC_DIR_KEEP"] = "true"
132+
monkeypatch.delenv("PROMETHEUS_MULTIPROC_DIR")
133+
monkeypatch.setenv("PROMETHEUS_MULTIPROC_DIR_KEEP", "true")
130134

131135
# When
132136
main(["flagsmith"])
133137

134138
# Then
135139
assert Path(os.environ["PROMETHEUS_MULTIPROC_DIR"]).exists()
140+
141+
142+
def test_main__no_django_configured__expected_0(
143+
monkeypatch: pytest.MonkeyPatch,
144+
) -> None:
145+
# Given
146+
monkeypatch.delenv("DJANGO_SETTINGS_MODULE")
147+
148+
# When
149+
output = subprocess.run("flagsmith")
150+
151+
# Then
152+
assert output.returncode == 0

0 commit comments

Comments
 (0)