Skip to content

Commit c259dde

Browse files
authored
PYTHON-3860 add types to read_preferences.py (#1320)
1 parent b90765d commit c259dde

File tree

1 file changed

+32
-24
lines changed

1 file changed

+32
-24
lines changed

pymongo/read_preferences.py

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
1414

1515
"""Utilities for choosing which member of a replica set to read from."""
1616

17+
from __future__ import annotations
18+
1719
from collections import abc
18-
from typing import Any, Dict, Mapping, Optional, Sequence
20+
from typing import TYPE_CHECKING, Any, Dict, Mapping, Optional, Sequence
1921

2022
from pymongo import max_staleness_selectors
2123
from pymongo.errors import ConfigurationError
@@ -24,6 +26,10 @@
2426
secondary_with_tags_server_selector,
2527
)
2628

29+
if TYPE_CHECKING:
30+
from pymongo.server_selectors import Selection
31+
from pymongo.topology_description import TopologyDescription
32+
2733
_PRIMARY = 0
2834
_PRIMARY_PREFERRED = 1
2935
_SECONDARY = 2
@@ -39,8 +45,11 @@
3945
"nearest",
4046
)
4147

48+
_Hedge = Mapping[str, Any]
49+
_TagSets = Sequence[Mapping[str, Any]]
50+
4251

43-
def _validate_tag_sets(tag_sets):
52+
def _validate_tag_sets(tag_sets: Optional[_TagSets]) -> Optional[_TagSets]:
4453
"""Validate tag sets for a MongoClient."""
4554
if tag_sets is None:
4655
return tag_sets
@@ -63,12 +72,12 @@ def _validate_tag_sets(tag_sets):
6372
return list(tag_sets)
6473

6574

66-
def _invalid_max_staleness_msg(max_staleness):
75+
def _invalid_max_staleness_msg(max_staleness: Any) -> str:
6776
return "maxStalenessSeconds must be a positive integer, not %s" % max_staleness
6877

6978

7079
# Some duplication with common.py to avoid import cycle.
71-
def _validate_max_staleness(max_staleness):
80+
def _validate_max_staleness(max_staleness: Any) -> int:
7281
"""Validate max_staleness."""
7382
if max_staleness == -1:
7483
return -1
@@ -82,7 +91,7 @@ def _validate_max_staleness(max_staleness):
8291
return max_staleness
8392

8493

85-
def _validate_hedge(hedge):
94+
def _validate_hedge(hedge: Optional[_Hedge]) -> Optional[_Hedge]:
8695
"""Validate hedge."""
8796
if hedge is None:
8897
return None
@@ -93,10 +102,6 @@ def _validate_hedge(hedge):
93102
return hedge
94103

95104

96-
_Hedge = Mapping[str, Any]
97-
_TagSets = Sequence[Mapping[str, Any]]
98-
99-
100105
class _ServerMode:
101106
"""Base class for all read preferences."""
102107

@@ -209,7 +214,7 @@ def min_wire_version(self) -> int:
209214
"""
210215
return 0 if self.__max_staleness == -1 else 5
211216

212-
def __repr__(self):
217+
def __repr__(self) -> str:
213218
return "{}(tag_sets={!r}, max_staleness={!r}, hedge={!r})".format(
214219
self.name,
215220
self.__tag_sets,
@@ -230,7 +235,7 @@ def __eq__(self, other: Any) -> bool:
230235
def __ne__(self, other: Any) -> bool:
231236
return not self == other
232237

233-
def __getstate__(self):
238+
def __getstate__(self) -> Dict[str, Any]:
234239
"""Return value of object for pickling.
235240
236241
Needed explicitly because __slots__() defined.
@@ -242,14 +247,17 @@ def __getstate__(self):
242247
"hedge": self.__hedge,
243248
}
244249

245-
def __setstate__(self, value):
250+
def __setstate__(self, value: Mapping[str, Any]) -> None:
246251
"""Restore from pickling."""
247252
self.__mode = value["mode"]
248253
self.__mongos_mode = _MONGOS_MODES[self.__mode]
249254
self.__tag_sets = _validate_tag_sets(value["tag_sets"])
250255
self.__max_staleness = _validate_max_staleness(value["max_staleness"])
251256
self.__hedge = _validate_hedge(value["hedge"])
252257

258+
def __call__(self, selection: Selection) -> Selection:
259+
return selection
260+
253261

254262
class Primary(_ServerMode):
255263
"""Primary read preference.
@@ -266,11 +274,11 @@ class Primary(_ServerMode):
266274
def __init__(self) -> None:
267275
super().__init__(_PRIMARY)
268276

269-
def __call__(self, selection: Any) -> Any:
277+
def __call__(self, selection: Selection) -> Selection:
270278
"""Apply this read preference to a Selection."""
271279
return selection.primary_selection
272280

273-
def __repr__(self):
281+
def __repr__(self) -> str:
274282
return "Primary()"
275283

276284
def __eq__(self, other: Any) -> bool:
@@ -317,7 +325,7 @@ def __init__(
317325
) -> None:
318326
super().__init__(_PRIMARY_PREFERRED, tag_sets, max_staleness, hedge)
319327

320-
def __call__(self, selection: Any) -> Any:
328+
def __call__(self, selection: Selection) -> Selection:
321329
"""Apply this read preference to Selection."""
322330
if selection.primary:
323331
return selection.primary_selection
@@ -360,7 +368,7 @@ def __init__(
360368
) -> None:
361369
super().__init__(_SECONDARY, tag_sets, max_staleness, hedge)
362370

363-
def __call__(self, selection: Any) -> Any:
371+
def __call__(self, selection: Selection) -> Selection:
364372
"""Apply this read preference to Selection."""
365373
return secondary_with_tags_server_selector(
366374
self.tag_sets, max_staleness_selectors.select(self.max_staleness, selection)
@@ -404,7 +412,7 @@ def __init__(
404412
) -> None:
405413
super().__init__(_SECONDARY_PREFERRED, tag_sets, max_staleness, hedge)
406414

407-
def __call__(self, selection: Any) -> Any:
415+
def __call__(self, selection: Selection) -> Selection:
408416
"""Apply this read preference to Selection."""
409417
secondaries = secondary_with_tags_server_selector(
410418
self.tag_sets, max_staleness_selectors.select(self.max_staleness, selection)
@@ -449,7 +457,7 @@ def __init__(
449457
) -> None:
450458
super().__init__(_NEAREST, tag_sets, max_staleness, hedge)
451459

452-
def __call__(self, selection: Any) -> Any:
460+
def __call__(self, selection: Selection) -> Selection:
453461
"""Apply this read preference to Selection."""
454462
return member_with_tags_server_selector(
455463
self.tag_sets, max_staleness_selectors.select(self.max_staleness, selection)
@@ -469,11 +477,11 @@ class _AggWritePref:
469477

470478
__slots__ = ("pref", "effective_pref")
471479

472-
def __init__(self, pref):
480+
def __init__(self, pref: _ServerMode):
473481
self.pref = pref
474-
self.effective_pref = ReadPreference.PRIMARY
482+
self.effective_pref: _ServerMode = ReadPreference.PRIMARY
475483

476-
def selection_hook(self, topology_description):
484+
def selection_hook(self, topology_description: TopologyDescription) -> None:
477485
common_wv = topology_description.common_wire_version
478486
if (
479487
topology_description.has_readable_server(ReadPreference.PRIMARY_PREFERRED)
@@ -484,16 +492,16 @@ def selection_hook(self, topology_description):
484492
else:
485493
self.effective_pref = self.pref
486494

487-
def __call__(self, selection):
495+
def __call__(self, selection: Selection) -> Selection:
488496
"""Apply this read preference to a Selection."""
489497
return self.effective_pref(selection)
490498

491-
def __repr__(self):
499+
def __repr__(self) -> str:
492500
return f"_AggWritePref(pref={self.pref!r})"
493501

494502
# Proxy other calls to the effective_pref so that _AggWritePref can be
495503
# used in place of an actual read preference.
496-
def __getattr__(self, name):
504+
def __getattr__(self, name: str) -> Any:
497505
return getattr(self.effective_pref, name)
498506

499507

0 commit comments

Comments
 (0)