Skip to content

Commit 6fb69a7

Browse files
committed
Improve typing
1 parent d0406f0 commit 6fb69a7

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

src/common/core/utils.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import random
66
from functools import lru_cache
77
from itertools import cycle
8-
from typing import Iterator, NotRequired, TypedDict, TypeVar
8+
from typing import Iterator, Literal, NotRequired, TypedDict, TypeVar
99

1010
from django.conf import settings
1111
from django.contrib.auth import get_user_model
@@ -21,7 +21,8 @@
2121

2222
ModelType = TypeVar("ModelType", bound=Model)
2323

24-
_replica_sequential_names_by_prefix: dict[str, Iterator[str]] = {}
24+
ReplicaNamePrefix = Literal["replica_", "cross_region_replica_"]
25+
_replica_sequential_names_by_prefix: dict[ReplicaNamePrefix, Iterator[str]] = {}
2526

2627

2728
class ReplicaReadStrategy(enum.StrEnum):
@@ -135,7 +136,7 @@ def get_file_contents(file_path: str) -> str | None:
135136

136137
def using_database_replica(
137138
manager: Manager[ModelType],
138-
replica_prefix: str = "replica_",
139+
replica_prefix: ReplicaNamePrefix = "replica_",
139140
) -> Manager[ModelType]:
140141
"""Attempts to bind a manager to a healthy database replica"""
141142
local_replicas = [name for name in connections if name.startswith(replica_prefix)]

tests/unit/common/core/test_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def clear_sequential_replica_manager() -> None:
4343
@pytest.fixture()
4444
def bad_replica(mocker: MockerFixture) -> MockType:
4545
"""An unhealthy replica"""
46-
replica: MockType = mocker.Mock()
46+
replica: MockType = mocker.Mock(spec=connections["default"])
4747
replica.ensure_connection.side_effect = OperationalError("Connection failed")
4848
return replica
4949

0 commit comments

Comments
 (0)