An open source, non-profit web search engine
Fix the three bugs the type checker found
Daoud Clarkef0bb313e5f43
diff --git a/mwmbl/indexer/update_urls.py b/mwmbl/indexer/update_urls.pyindex e583a06..932fad8 100644--- a/mwmbl/indexer/update_urls.py+++ b/mwmbl/indexer/update_urls.py@@ -107,7 +107,7 @@ def _add_found_urls_to_db_and_queue(found_urls, new_item_queue):def add_hn_links(new_item_queue: RedisURLQueue):hn_count = new_item_queue.get_domain_count("news.ycombinator.com")- max_count = get_domain_max_urls("news.ycombinator.com")+ max_count = get_domain_max_urls("news.ycombinator.com", new_item_queue.get_curated_domains_function())max_item = requests.get("https://hacker-news.firebaseio.com/v0/maxitem.json").json()logger.info(f"Adding HN links, current count: {hn_count}, max count: {max_count}, max item ID: {max_item}")diff --git a/mwmbl/tinysearchengine/super_search.py b/mwmbl/tinysearchengine/super_search.pyindex 5cd5e65..39d5538 100644--- a/mwmbl/tinysearchengine/super_search.py+++ b/mwmbl/tinysearchengine/super_search.py@@ -45,6 +45,7 @@ from mwmbl.tinysearchengine.indexer import Documentfrom mwmbl.tinysearchengine.ltr_rank import score_documentsfrom mwmbl.tinysearchengine.mmr_rank import mmr_rerankfrom mwmbl.tinysearchengine.rank import find_blacklisted_urls, score_result_whole+from mwmbl.tinysearchengine.super_search_select.rewards import SelectionContextfrom mwmbl.tinysearchengine.super_search_sources import SOURCESfrom mwmbl.tokenizer import tokenize@@ -505,7 +506,14 @@ async def _index_results(query: str, docs: list[Document]) -> int:return 0-async def _run_pipeline(query: str, emit, all_docs: list[Document], last_results_key: list, lock: asyncio.Lock) -> None:+async def _run_pipeline(+ query: str,+ emit,+ all_docs: list[Document],+ last_results_key: list,+ lock: asyncio.Lock,+ ctx: SelectionContext | None = None,+) -> None:per_source_limit = settings.SUPER_SEARCH_RESULTS_PER_SOURCEtop_k = getattr(settings, "SUPER_SEARCH_TOP_K", 10)limits = httpx.Limits(max_connections=20, max_keepalive_connections=10)@@ -542,6 +550,10 @@ async def _run_pipeline(query: str, emit, all_docs: list[Document], last_resultsfor name, fn in SOURCES.items():await emit("source_started", SourceStartedEvent(source=name))source_tasks.append(asyncio.create_task(_call_source(name, fn, client, query, per_source_limit)))+ if ctx is not None:+ ctx.candidates = list(SOURCES)+ ctx.selected = list(SOURCES)+ ctx.per_source_limit = per_source_limitsecondary: list[asyncio.Task] = []@@ -552,6 +564,8 @@ async def _run_pipeline(query: str, emit, all_docs: list[Document], last_resultscontinuedocs = _drop_blacklisted(docs)await emit("source_returned", SourceReturnedEvent(source=name, count=len(docs)))+ if ctx is not None:+ ctx.record_results(name, [doc.url for doc in docs])if not docs:continuediff --git a/pyproject.toml b/pyproject.tomlindex adbf5ba..a333f51 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -169,11 +169,10 @@ no-matching-overload = "warn"invalid-return-type = "warn"# 2.invalid-type-form = "warn"-# 2 - both real: eval callers pass a 6th `ctx` argument to `_run_pipeline`, which takes 5.-too-many-positional-arguments = "warn"-# 1 - real: mwmbl/indexer/update_urls.py calls get_domain_max_urls() without-# `curated_domains`, so add_hn_links() raises TypeError.-missing-argument = "warn"+# 0 - clean, and kept blocking. Calling something with the wrong number of arguments is+# always a bug, and ty catches it across modules where a test never would.+too-many-positional-arguments = "error"+missing-argument = "error"# 1.empty-body = "warn"# 1.diff --git a/test/test_super_search.py b/test/test_super_search.pyindex 72e615b..e6a3d1b 100644--- a/test/test_super_search.py+++ b/test/test_super_search.py@@ -829,3 +829,49 @@ def test_blacklisted_links_are_not_crawled(client, api_key, monkeypatch):assert "https://badsite.test/x" not in crawledassert "https://other.test/y" in crawled+++def test_run_pipeline_attributes_results_to_their_source(monkeypatch):+ """The offline eval callers pass a SelectionContext and read back which source+ produced each URL. Nothing in the streaming endpoint exercises that path, so the+ attribution is only covered here."""+ import mwmbl.tinysearchengine.super_search as ss+ from mwmbl.tinysearchengine.super_search_select.rewards import SelectionContext++ _stub_sources(+ monkeypatch,+ {+ "hn": [Document(title="From HN", url="https://hn.example/a", extract="x")],+ "github": [Document(title="From GH", url="https://gh.example/b", extract="x")],+ },+ )+ _stub_scoring(monkeypatch, [0.0] * 20)++ async def _noop_emit(event_type, data):+ return None++ ctx = SelectionContext()+ all_docs: list[Document] = []+ async_to_sync(ss._run_pipeline)("q", _noop_emit, all_docs, [None], asyncio.Lock(), ctx)++ assert ctx.source_by_url == {+ "https://hn.example/a": "hn",+ "https://gh.example/b": "github",+ }+ assert sorted(ctx.selected) == ["github", "hn"]+ assert ctx.per_source_limit > 0+++def test_run_pipeline_works_without_a_selection_context(monkeypatch):+ """The streaming endpoint passes no context; the pipeline must not require one."""+ import mwmbl.tinysearchengine.super_search as ss++ _stub_sources(monkeypatch, {"hn": [Document(title="From HN", url="https://hn.example/a", extract="x")]})+ _stub_scoring(monkeypatch, [0.0] * 20)++ async def _noop_emit(event_type, data):+ return None++ all_docs: list[Document] = []+ async_to_sync(ss._run_pipeline)("q", _noop_emit, all_docs, [None], asyncio.Lock())+ assert [d.url for d in all_docs] == ["https://hn.example/a"]diff --git a/test/test_update_urls.py b/test/test_update_urls.pyindex a94bcb2..547d351 100644--- a/test/test_update_urls.py+++ b/test/test_update_urls.py@@ -26,7 +26,6 @@ def test_process_link_normal():def test_process_link_excludes_porn():- url_scores = {}url_timestamps = {}url_users = {}domain_links = {}@@ -44,7 +43,6 @@ def test_process_link_excludes_porn():domain_links=domain_links,)- assert url_scores == {}assert url_timestamps == {}assert url_users == {}assert domain_links == {}