15
15
"""Criteria to select some ServerDescriptions from a TopologyDescription."""
16
16
from __future__ import annotations
17
17
18
- from typing import TYPE_CHECKING , List , Optional , TypeVar
18
+ from typing import TYPE_CHECKING , Any , List , Mapping , Optional , Sequence , TypeVar , cast
19
19
20
20
from pymongo .server_type import SERVER_TYPE
21
21
25
25
26
26
27
27
T = TypeVar ("T" )
28
+ TagSet = Mapping [str , Any ]
29
+ TagSets = Sequence [TagSet ]
28
30
29
31
30
32
class Selection :
@@ -66,7 +68,9 @@ def with_server_descriptions(self, server_descriptions: List[ServerDescription])
66
68
def secondary_with_max_last_write_date (self ) -> Optional [ServerDescription ]:
67
69
secondaries = secondary_server_selector (self )
68
70
if secondaries .server_descriptions :
69
- return max (secondaries .server_descriptions , key = lambda sd : sd .last_write_date )
71
+ return max (
72
+ secondaries .server_descriptions , key = lambda sd : cast (float , sd .last_write_date )
73
+ )
70
74
return None
71
75
72
76
@property
@@ -105,24 +109,24 @@ def writable_server_selector(selection: Selection) -> Selection:
105
109
)
106
110
107
111
108
- def secondary_server_selector (selection ) :
112
+ def secondary_server_selector (selection : Selection ) -> Selection :
109
113
return selection .with_server_descriptions (
110
114
[s for s in selection .server_descriptions if s .server_type == SERVER_TYPE .RSSecondary ]
111
115
)
112
116
113
117
114
- def arbiter_server_selector (selection ) :
118
+ def arbiter_server_selector (selection : Selection ) -> Selection :
115
119
return selection .with_server_descriptions (
116
120
[s for s in selection .server_descriptions if s .server_type == SERVER_TYPE .RSArbiter ]
117
121
)
118
122
119
123
120
- def writable_preferred_server_selector (selection ) :
124
+ def writable_preferred_server_selector (selection : Selection ) -> Selection :
121
125
"""Like PrimaryPreferred but doesn't use tags or latency."""
122
126
return writable_server_selector (selection ) or secondary_server_selector (selection )
123
127
124
128
125
- def apply_single_tag_set (tag_set , selection ) :
129
+ def apply_single_tag_set (tag_set : TagSet , selection : Selection ) -> Selection :
126
130
"""All servers matching one tag set.
127
131
128
132
A tag set is a dict. A server matches if its tags are a superset:
@@ -131,7 +135,7 @@ def apply_single_tag_set(tag_set, selection):
131
135
The empty tag set {} matches any server.
132
136
"""
133
137
134
- def tags_match (server_tags ) :
138
+ def tags_match (server_tags : Mapping [ str , Any ]) -> bool :
135
139
for key , value in tag_set .items ():
136
140
if key not in server_tags or server_tags [key ] != value :
137
141
return False
@@ -143,7 +147,7 @@ def tags_match(server_tags):
143
147
)
144
148
145
149
146
- def apply_tag_sets (tag_sets , selection ) :
150
+ def apply_tag_sets (tag_sets : TagSets , selection : Selection ) -> Selection :
147
151
"""All servers match a list of tag sets.
148
152
149
153
tag_sets is a list of dicts. The empty tag set {} matches any server,
@@ -160,11 +164,11 @@ def apply_tag_sets(tag_sets, selection):
160
164
return selection .with_server_descriptions ([])
161
165
162
166
163
- def secondary_with_tags_server_selector (tag_sets , selection ) :
167
+ def secondary_with_tags_server_selector (tag_sets : TagSets , selection : Selection ) -> Selection :
164
168
"""All near-enough secondaries matching the tag sets."""
165
169
return apply_tag_sets (tag_sets , secondary_server_selector (selection ))
166
170
167
171
168
- def member_with_tags_server_selector (tag_sets , selection ) :
172
+ def member_with_tags_server_selector (tag_sets : TagSets , selection : Selection ) -> Selection :
169
173
"""All near-enough members matching the tag sets."""
170
174
return apply_tag_sets (tag_sets , readable_server_selector (selection ))
0 commit comments