1515"""Criteria to select some ServerDescriptions from a TopologyDescription."""
1616from __future__ import annotations
1717
18- from typing import TYPE_CHECKING , List , Optional , TypeVar
18+ from typing import TYPE_CHECKING , Any , List , Mapping , Optional , Sequence , TypeVar , cast
1919
2020from pymongo .server_type import SERVER_TYPE
2121
2525
2626
2727T = TypeVar ("T" )
28+ TagSet = Mapping [str , Any ]
29+ TagSets = Sequence [TagSet ]
2830
2931
3032class Selection :
@@ -66,7 +68,9 @@ def with_server_descriptions(self, server_descriptions: List[ServerDescription])
6668 def secondary_with_max_last_write_date (self ) -> Optional [ServerDescription ]:
6769 secondaries = secondary_server_selector (self )
6870 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+ )
7074 return None
7175
7276 @property
@@ -105,24 +109,24 @@ def writable_server_selector(selection: Selection) -> Selection:
105109 )
106110
107111
108- def secondary_server_selector (selection ) :
112+ def secondary_server_selector (selection : Selection ) -> Selection :
109113 return selection .with_server_descriptions (
110114 [s for s in selection .server_descriptions if s .server_type == SERVER_TYPE .RSSecondary ]
111115 )
112116
113117
114- def arbiter_server_selector (selection ) :
118+ def arbiter_server_selector (selection : Selection ) -> Selection :
115119 return selection .with_server_descriptions (
116120 [s for s in selection .server_descriptions if s .server_type == SERVER_TYPE .RSArbiter ]
117121 )
118122
119123
120- def writable_preferred_server_selector (selection ) :
124+ def writable_preferred_server_selector (selection : Selection ) -> Selection :
121125 """Like PrimaryPreferred but doesn't use tags or latency."""
122126 return writable_server_selector (selection ) or secondary_server_selector (selection )
123127
124128
125- def apply_single_tag_set (tag_set , selection ) :
129+ def apply_single_tag_set (tag_set : TagSet , selection : Selection ) -> Selection :
126130 """All servers matching one tag set.
127131
128132 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):
131135 The empty tag set {} matches any server.
132136 """
133137
134- def tags_match (server_tags ) :
138+ def tags_match (server_tags : Mapping [ str , Any ]) -> bool :
135139 for key , value in tag_set .items ():
136140 if key not in server_tags or server_tags [key ] != value :
137141 return False
@@ -143,7 +147,7 @@ def tags_match(server_tags):
143147 )
144148
145149
146- def apply_tag_sets (tag_sets , selection ) :
150+ def apply_tag_sets (tag_sets : TagSets , selection : Selection ) -> Selection :
147151 """All servers match a list of tag sets.
148152
149153 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):
160164 return selection .with_server_descriptions ([])
161165
162166
163- def secondary_with_tags_server_selector (tag_sets , selection ) :
167+ def secondary_with_tags_server_selector (tag_sets : TagSets , selection : Selection ) -> Selection :
164168 """All near-enough secondaries matching the tag sets."""
165169 return apply_tag_sets (tag_sets , secondary_server_selector (selection ))
166170
167171
168- def member_with_tags_server_selector (tag_sets , selection ) :
172+ def member_with_tags_server_selector (tag_sets : TagSets , selection : Selection ) -> Selection :
169173 """All near-enough members matching the tag sets."""
170174 return apply_tag_sets (tag_sets , readable_server_selector (selection ))
0 commit comments