Skip to content

Commit e6797ce

Browse files
authored
fix total for progress bar by counting deprecated docs (#957)
* docstring fix * raise RestError on 400 * fix total by also counting deprecated docs * ruff
1 parent f9936de commit e6797ce

File tree

1 file changed

+40
-27
lines changed

1 file changed

+40
-27
lines changed

mp_api/client/core/client.py

Lines changed: 40 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from __future__ import annotations
77

8+
import inspect
89
import itertools
910
import json
1011
import os
@@ -419,7 +420,7 @@ def _query_resource(
419420
"""Query the endpoint for a Resource containing a list of documents
420421
and meta information about pagination and total document count.
421422
422-
For the end-user, methods .query() and .count() are intended to be
423+
For the end-user, methods .search() and .count() are intended to be
423424
easier to use.
424425
425426
Arguments:
@@ -994,8 +995,9 @@ def _submit_request_and_process(
994995
)
995996

996997
if response.status_code in [400]:
997-
warnings.warn(
998-
f"The server does not support the request made to {response.url}. This may be due to an outdated mp-api package, or a problem with the query."
998+
raise MPRestError(
999+
f"The server does not support the request made to {response.url}. "
1000+
"This may be due to an outdated mp-api package, or a problem with the query."
9991001
)
10001002

10011003
if response.status_code == 200:
@@ -1266,34 +1268,45 @@ def count(self, criteria: dict | None = None) -> int | str:
12661268
"""Return a count of total documents.
12671269
12681270
Args:
1269-
criteria (dict | None): As in .query(). Defaults to None
1271+
criteria (dict | None): As in .search(). Defaults to None
12701272
12711273
Returns:
12721274
(int | str): Count of total results, or string indicating error
12731275
"""
1274-
try:
1275-
criteria = criteria or {}
1276-
user_preferences = (
1277-
self.monty_decode,
1278-
self.use_document_model,
1279-
self.mute_progress_bars,
1280-
)
1281-
self.monty_decode, self.use_document_model, self.mute_progress_bars = (
1282-
False,
1283-
False,
1284-
True,
1285-
) # do not waste cycles decoding
1286-
results = self._query_resource(
1287-
criteria=criteria, num_chunks=1, chunk_size=1
1288-
)
1289-
(
1290-
self.monty_decode,
1291-
self.use_document_model,
1292-
self.mute_progress_bars,
1293-
) = user_preferences
1294-
return results["meta"]["total_doc"]
1295-
except Exception: # pragma: no cover
1296-
return "Problem getting count"
1276+
criteria = criteria or {}
1277+
user_preferences = (
1278+
self.monty_decode,
1279+
self.use_document_model,
1280+
self.mute_progress_bars,
1281+
)
1282+
self.monty_decode, self.use_document_model, self.mute_progress_bars = (
1283+
False,
1284+
False,
1285+
True,
1286+
) # do not waste cycles decoding
1287+
results = self._query_resource(criteria=criteria, num_chunks=1, chunk_size=1)
1288+
cnt = results["meta"]["total_doc"]
1289+
1290+
no_query = not {field for field in criteria if field[0] != "_"}
1291+
if no_query and hasattr(self, "search"):
1292+
allowed_params = inspect.getfullargspec(self.search).args
1293+
if "deprecated" in allowed_params:
1294+
criteria["deprecated"] = True
1295+
results = self._query_resource(
1296+
criteria=criteria, num_chunks=1, chunk_size=1
1297+
)
1298+
cnt += results["meta"]["total_doc"]
1299+
warnings.warn(
1300+
"Omitting a query also includes deprecated documents in the results. "
1301+
"Make sure to post-filter them out."
1302+
)
1303+
1304+
(
1305+
self.monty_decode,
1306+
self.use_document_model,
1307+
self.mute_progress_bars,
1308+
) = user_preferences
1309+
return cnt
12971310

12981311
@property
12991312
def available_fields(self) -> list[str]:

0 commit comments

Comments
 (0)