Skip to content

Commit 6e94187

Browse files
committed
fix(search): treat a SearchParam limit of None as no limit
- 'limit=None' appended the literal '|limit=None' and then raised TypeError on the truncation check - None now sends no limit parameter and leaves the wiki to apply its own - no truncation warning in that case, since no limit is in force
1 parent 0c75699 commit 6e94187

2 files changed

Lines changed: 50 additions & 7 deletions

File tree

‎src/osw/wiki_tools.py‎

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,9 @@ class SearchParam(OswBaseModel):
138138
parallel: Optional[bool] = None # is set to true if query is a list longer than 5
139139
debug: Optional[bool] = False
140140
limit: Optional[int] = 1000
141-
"""the result limit to apply. Ignored by semantic_search for a query that
142-
sets 'limit=' itself, since SMW honours the last limit in the query string"""
141+
"""the result limit to apply, None to apply none and leave it to the wiki.
142+
Ignored by semantic_search for a query that sets 'limit=' itself, since SMW
143+
honours the last limit in the query string"""
143144
return_json: Optional[bool] = False
144145

145146
def __init__(self, **data):
@@ -289,7 +290,7 @@ def semantic_search(
289290
query :
290291
(List of) query text(s) or instance of SearchParam. A query that sets
291292
``limit=`` itself keeps that limit; ``SearchParam.limit`` is only
292-
appended to a query that does not.
293+
appended to a query that does not, and only when it is not None.
293294
294295
Returns
295296
-------
@@ -304,9 +305,11 @@ def semantic_search(
304305
def semantic_search_(single_query):
305306
page_list = list()
306307
# SMW honours the last limit in the query string, so appending the
307-
# default would silently override a limit the caller wrote themselves
308+
# default would silently override a limit the caller wrote themselves.
309+
# A SearchParam limit of None asks for no limit at all, leaving the
310+
# wiki to apply its own default
308311
limit = get_query_limit(single_query)
309-
if limit is None:
312+
if limit is None and query.limit is not None:
310313
limit = query.limit
311314
single_query += f"|limit={limit}"
312315
result = site.api("ask", query=single_query, format="json")
@@ -317,8 +320,9 @@ def semantic_search_(single_query):
317320
print(f"Query '{single_query}' returned no results")
318321
else:
319322
print(f"Query '{single_query}' returned {n} results")
320-
# 'limit=0' asks for no results at all, e.g. with a count format, so
321-
# meeting it says nothing about truncation
323+
# No limit in force, or 'limit=0' asking for no results at all as a
324+
# count format does, means the result count says nothing about
325+
# truncation
322326
if limit and n >= limit:
323327
warnings.warn(
324328
f"Query '{single_query}' returned {n} results, which meets the "

‎tests/test_wiki_tools.py‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,45 @@ def test_semantic_search_truncation_warning_uses_the_query_limit():
276276
assert sorted(out) == sorted(titles)
277277

278278

279+
def test_semantic_search_limit_none_sends_no_limit():
280+
"""None asks for no limit at all, leaving the wiki to apply its own."""
281+
site = MagicMock()
282+
site.api.return_value = _ask_result("Item:OSW1")
283+
284+
wt.semantic_search(
285+
site, wt.SearchParam(query="[[HasType::Category:Item]]", limit=None)
286+
)
287+
288+
assert site.api.call_args.kwargs["query"] == "[[HasType::Category:Item]]"
289+
290+
291+
def test_semantic_search_limit_none_keeps_a_limit_the_caller_wrote():
292+
site = MagicMock()
293+
site.api.return_value = _ask_result("Item:OSW1")
294+
295+
wt.semantic_search(
296+
site, wt.SearchParam(query="[[HasType::Category:Item]]|limit=2", limit=None)
297+
)
298+
299+
assert site.api.call_args.kwargs["query"] == "[[HasType::Category:Item]]|limit=2"
300+
301+
302+
def test_semantic_search_limit_none_does_not_warn_about_truncation():
303+
"""With no limit in force the result count says nothing about truncation."""
304+
titles = [f"Item:OSW{i}" for i in range(5)]
305+
site = MagicMock()
306+
site.api.return_value = _ask_result(*titles)
307+
308+
with warnings.catch_warnings(record=True) as caught:
309+
warnings.simplefilter("always")
310+
out = wt.semantic_search(
311+
site, wt.SearchParam(query="[[HasType::Category:Item]]", limit=None)
312+
)
313+
314+
assert not any("truncated" in str(w.message) for w in caught)
315+
assert sorted(out) == sorted(titles)
316+
317+
279318
def test_semantic_search_no_truncation_warning_for_a_zero_limit():
280319
"""'limit=0' asks for no results, so meeting it is not truncation."""
281320
site = MagicMock()

0 commit comments

Comments
 (0)