MCPcopy Index your code
hub / github.com/mongodb/mongo-python-driver / _index_list

Function _index_list

pymongo/helpers_shared.py:145–172  ·  view source on GitHub ↗

Helper to generate a list of (key, direction) pairs. Takes such a list, or a single key, or a single key and direction.

(
    key_or_list: _Hint, direction: Optional[Union[int, str]] = None
)

Source from the content-addressed store, hash-verified

143
144
145def _index_list(
146 key_or_list: _Hint, direction: Optional[Union[int, str]] = None
147) -> Sequence[tuple[str, Union[int, str, Mapping[str, Any]]]]:
148 """Helper to generate a list of (key, direction) pairs.
149
150 Takes such a list, or a single key, or a single key and direction.
151 """
152 if direction is not None:
153 if not isinstance(key_or_list, str):
154 raise TypeError(f"Expected a string and a direction, not {type(key_or_list)}")
155 return [(key_or_list, direction)]
156 else:
157 if isinstance(key_or_list, str):
158 return [(key_or_list, ASCENDING)]
159 elif isinstance(key_or_list, abc.ItemsView):
160 return list(key_or_list) # type: ignore[arg-type]
161 elif isinstance(key_or_list, abc.Mapping):
162 return list(key_or_list.items())
163 elif not isinstance(key_or_list, (list, tuple)):
164 raise TypeError(
165 f"if no direction is specified, key_or_list must be an instance of list, not {type(key_or_list)}"
166 )
167 values: list[tuple[str, int]] = []
168 for item in key_or_list:
169 if isinstance(item, str):
170 item = (item, ASCENDING) # noqa: PLW2901
171 values.append(item)
172 return values
173
174
175def _index_document(index_list: _IndexList) -> dict[str, Any]:

Callers 1

__init__Method · 0.90

Calls 1

itemsMethod · 0.80

Tested by

no test coverage detected