14
14
15
15
"""Utilities for choosing which member of a replica set to read from."""
16
16
17
+ from __future__ import annotations
18
+
17
19
from collections import abc
18
- from typing import Any , Dict , Mapping , Optional , Sequence
20
+ from typing import TYPE_CHECKING , Any , Dict , Mapping , Optional , Sequence
19
21
20
22
from pymongo import max_staleness_selectors
21
23
from pymongo .errors import ConfigurationError
24
26
secondary_with_tags_server_selector ,
25
27
)
26
28
29
+ if TYPE_CHECKING :
30
+ from pymongo .server_selectors import Selection
31
+ from pymongo .topology_description import TopologyDescription
32
+
27
33
_PRIMARY = 0
28
34
_PRIMARY_PREFERRED = 1
29
35
_SECONDARY = 2
39
45
"nearest" ,
40
46
)
41
47
48
+ _Hedge = Mapping [str , Any ]
49
+ _TagSets = Sequence [Mapping [str , Any ]]
50
+
42
51
43
- def _validate_tag_sets (tag_sets ) :
52
+ def _validate_tag_sets (tag_sets : Optional [ _TagSets ]) -> Optional [ _TagSets ] :
44
53
"""Validate tag sets for a MongoClient."""
45
54
if tag_sets is None :
46
55
return tag_sets
@@ -63,12 +72,12 @@ def _validate_tag_sets(tag_sets):
63
72
return list (tag_sets )
64
73
65
74
66
- def _invalid_max_staleness_msg (max_staleness ) :
75
+ def _invalid_max_staleness_msg (max_staleness : Any ) -> str :
67
76
return "maxStalenessSeconds must be a positive integer, not %s" % max_staleness
68
77
69
78
70
79
# 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 :
72
81
"""Validate max_staleness."""
73
82
if max_staleness == - 1 :
74
83
return - 1
@@ -82,7 +91,7 @@ def _validate_max_staleness(max_staleness):
82
91
return max_staleness
83
92
84
93
85
- def _validate_hedge (hedge ) :
94
+ def _validate_hedge (hedge : Optional [ _Hedge ]) -> Optional [ _Hedge ] :
86
95
"""Validate hedge."""
87
96
if hedge is None :
88
97
return None
@@ -93,10 +102,6 @@ def _validate_hedge(hedge):
93
102
return hedge
94
103
95
104
96
- _Hedge = Mapping [str , Any ]
97
- _TagSets = Sequence [Mapping [str , Any ]]
98
-
99
-
100
105
class _ServerMode :
101
106
"""Base class for all read preferences."""
102
107
@@ -209,7 +214,7 @@ def min_wire_version(self) -> int:
209
214
"""
210
215
return 0 if self .__max_staleness == - 1 else 5
211
216
212
- def __repr__ (self ):
217
+ def __repr__ (self ) -> str :
213
218
return "{}(tag_sets={!r}, max_staleness={!r}, hedge={!r})" .format (
214
219
self .name ,
215
220
self .__tag_sets ,
@@ -230,7 +235,7 @@ def __eq__(self, other: Any) -> bool:
230
235
def __ne__ (self , other : Any ) -> bool :
231
236
return not self == other
232
237
233
- def __getstate__ (self ):
238
+ def __getstate__ (self ) -> Dict [ str , Any ] :
234
239
"""Return value of object for pickling.
235
240
236
241
Needed explicitly because __slots__() defined.
@@ -242,14 +247,17 @@ def __getstate__(self):
242
247
"hedge" : self .__hedge ,
243
248
}
244
249
245
- def __setstate__ (self , value ) :
250
+ def __setstate__ (self , value : Mapping [ str , Any ]) -> None :
246
251
"""Restore from pickling."""
247
252
self .__mode = value ["mode" ]
248
253
self .__mongos_mode = _MONGOS_MODES [self .__mode ]
249
254
self .__tag_sets = _validate_tag_sets (value ["tag_sets" ])
250
255
self .__max_staleness = _validate_max_staleness (value ["max_staleness" ])
251
256
self .__hedge = _validate_hedge (value ["hedge" ])
252
257
258
+ def __call__ (self , selection : Selection ) -> Selection :
259
+ return selection
260
+
253
261
254
262
class Primary (_ServerMode ):
255
263
"""Primary read preference.
@@ -266,11 +274,11 @@ class Primary(_ServerMode):
266
274
def __init__ (self ) -> None :
267
275
super ().__init__ (_PRIMARY )
268
276
269
- def __call__ (self , selection : Any ) -> Any :
277
+ def __call__ (self , selection : Selection ) -> Selection :
270
278
"""Apply this read preference to a Selection."""
271
279
return selection .primary_selection
272
280
273
- def __repr__ (self ):
281
+ def __repr__ (self ) -> str :
274
282
return "Primary()"
275
283
276
284
def __eq__ (self , other : Any ) -> bool :
@@ -317,7 +325,7 @@ def __init__(
317
325
) -> None :
318
326
super ().__init__ (_PRIMARY_PREFERRED , tag_sets , max_staleness , hedge )
319
327
320
- def __call__ (self , selection : Any ) -> Any :
328
+ def __call__ (self , selection : Selection ) -> Selection :
321
329
"""Apply this read preference to Selection."""
322
330
if selection .primary :
323
331
return selection .primary_selection
@@ -360,7 +368,7 @@ def __init__(
360
368
) -> None :
361
369
super ().__init__ (_SECONDARY , tag_sets , max_staleness , hedge )
362
370
363
- def __call__ (self , selection : Any ) -> Any :
371
+ def __call__ (self , selection : Selection ) -> Selection :
364
372
"""Apply this read preference to Selection."""
365
373
return secondary_with_tags_server_selector (
366
374
self .tag_sets , max_staleness_selectors .select (self .max_staleness , selection )
@@ -404,7 +412,7 @@ def __init__(
404
412
) -> None :
405
413
super ().__init__ (_SECONDARY_PREFERRED , tag_sets , max_staleness , hedge )
406
414
407
- def __call__ (self , selection : Any ) -> Any :
415
+ def __call__ (self , selection : Selection ) -> Selection :
408
416
"""Apply this read preference to Selection."""
409
417
secondaries = secondary_with_tags_server_selector (
410
418
self .tag_sets , max_staleness_selectors .select (self .max_staleness , selection )
@@ -449,7 +457,7 @@ def __init__(
449
457
) -> None :
450
458
super ().__init__ (_NEAREST , tag_sets , max_staleness , hedge )
451
459
452
- def __call__ (self , selection : Any ) -> Any :
460
+ def __call__ (self , selection : Selection ) -> Selection :
453
461
"""Apply this read preference to Selection."""
454
462
return member_with_tags_server_selector (
455
463
self .tag_sets , max_staleness_selectors .select (self .max_staleness , selection )
@@ -469,11 +477,11 @@ class _AggWritePref:
469
477
470
478
__slots__ = ("pref" , "effective_pref" )
471
479
472
- def __init__ (self , pref ):
480
+ def __init__ (self , pref : _ServerMode ):
473
481
self .pref = pref
474
- self .effective_pref = ReadPreference .PRIMARY
482
+ self .effective_pref : _ServerMode = ReadPreference .PRIMARY
475
483
476
- def selection_hook (self , topology_description ) :
484
+ def selection_hook (self , topology_description : TopologyDescription ) -> None :
477
485
common_wv = topology_description .common_wire_version
478
486
if (
479
487
topology_description .has_readable_server (ReadPreference .PRIMARY_PREFERRED )
@@ -484,16 +492,16 @@ def selection_hook(self, topology_description):
484
492
else :
485
493
self .effective_pref = self .pref
486
494
487
- def __call__ (self , selection ) :
495
+ def __call__ (self , selection : Selection ) -> Selection :
488
496
"""Apply this read preference to a Selection."""
489
497
return self .effective_pref (selection )
490
498
491
- def __repr__ (self ):
499
+ def __repr__ (self ) -> str :
492
500
return f"_AggWritePref(pref={ self .pref !r} )"
493
501
494
502
# Proxy other calls to the effective_pref so that _AggWritePref can be
495
503
# used in place of an actual read preference.
496
- def __getattr__ (self , name ) :
504
+ def __getattr__ (self , name : str ) -> Any :
497
505
return getattr (self .effective_pref , name )
498
506
499
507
0 commit comments