Skip to contentSkip to conversation

Harden local type checking and test mocking

PATCH Applied Tamir Duberstein 2 participants 18 messages Apr 13 – Apr 15

lore
Share & tools
lore
#0 lore raw

v2 cover Harden local type checking and test mocking

This series raises the local quality floor by making the type-checking tooling stricter and by removing test patterns that rely on unsafe or poorly typed mocks.

It starts by wiring the repository into b4 CI checks, then adds ruff, pyright, and ty so the same checks run consistently in one place. After that, it removes suppressions incrementally by making the code and tests more explicit and better typed: typed keyword forwarding in from_git_config, a typed authheaders integration, and test-local subclasses instead of method reassignment.

The goal is to keep each change reviewable on its own while moving the tree toward a stricter baseline with fewer exceptions and less fragile mocking.

It would be nice to reduce or remove the use of patch entirely, but this series stops short of that since it would require significant restructuring.

The final commit adds the minimal b4 configuration needed to send this series. :)

---
Changes in v2:
- Replace b4 CI integration with simple shell script.
- Fix placement of test docstrings.
- Fix from_git_config(cache_dir=None).
- Add missing spaces in README.md.
- Fix b4 send-prefixes.
- Link to v1: https://patch.msgid.link/[email protected]

---
Tamir Duberstein (14):
Add ci script and mypy suppressions
Type make_msg and drop test suppressions
Add ruff import checks to b4 CI
Add ruff format check to CI
Add pyright strict checks to CI
Replace HTTP session mocks with responses
Add ty checks to CI
Drop redundant read-only property test
Type from_git_config keyword arguments
Add authheaders stub and typed callable
Replace batch mocks with subclasses
Use CompletedProcess in git config tests
Update README for uv-based dev checks
Add b4 send configuration

 .b4-config                       |    7 +
 README.md                        |   11 +-
 ci.sh                            |   10 +
 pyproject.toml                   |   25 +-
 src/liblore/__init__.py          |    2 +-
 src/liblore/node.py              |  104 +-
 src/liblore/utils.py             |   89 +-
 tests/conftest.py                |   90 +-
 tests/test_auth_headers.py       |   83 +-
 tests/test_cache.py              |  214 ++--
 tests/test_email_utils.py        |   63 +-
 tests/test_formatting.py         |  324 +++---
 tests/test_mbox.py               |    7 +-
 tests/test_message.py            |   11 +-
 tests/test_node.py               | 1999 +++++++++++++++++++++-----------------
 tests/test_thread.py             |   35 +-
 typings/authheaders/__init__.pyi |   13 +
 17 files changed, 1809 insertions(+), 1278 deletions(-)

---
base-commit: 1c0b653bdb6eeb95625e99419d4b34d31cb55c99
change-id: 20260410-harden-type-checking-e92ed8b4cac1

Best regards,

Signature · 1 line
Tamir Duberstein <[email protected]>
#1 lore raw

PATCH v2 1/14 Add ci script and mypy suppressions

Add a simple shell script that runs mypy and pytest.

Introduce targeted mypy suppressions for current test-only issues so the baseline is green before tightening types further.

Also add pytest-asyncio so pytest recognizes asyncio_default_fixture_loop_scope and stops warning about it.

---
 ci.sh          |  6 ++++++
 pyproject.toml | 14 ++++++++++++++
 2 files changed, 20 insertions(+)
ci.sh new file +6 -0
new file mode 100755
index 0000000..1e8ccb3
@@ -0,0 +1,6 @@
+#!/usr/bin/env sh
+
+set -eu
+
+uv run mypy .
+uv run pytest --durations=0
pyproject.toml +14 -0
index 2353a18..e31a05c 100644
@@ -40,6 +40,7 @@ dev = [
     "build",
     "mypy",
     "pytest",
+    "pytest-asyncio",
     "ruff",
     "types-requests",
 ]
@@ -56,5 +57,18 @@ strict = true
 module = "authheaders"
 ignore_missing_imports = true
 
+[[tool.mypy.overrides]]
+module = [
+    "test_email_utils",
+    "test_formatting",
+    "test_message",
+    "test_thread",
+]
+disable_error_code = ["attr-defined"]
+
+[[tool.mypy.overrides]]
+module = "test_auth_headers"
+disable_error_code = ["unused-ignore"]
+
 [tool.ruff]
 target-version = "py39"
Signature · 1 line
2.53.0
#2 lore raw

PATCH v2 2/14 Type make_msg and drop test suppressions

In reply to Tamir Duberstein

Replace the make_msg class helper with a typed callable fixture and update tests to call it directly. This keeps argument checking in the tests while simplifying the fixture implementation.

Remove the temporary mypy suppressions now that the underlying typing issues are fixed in the test code.

---
 pyproject.toml             | 13 --------
 tests/conftest.py          | 79 ++++++++++++++++++++++++++--------------------
 tests/test_auth_headers.py |  4 +--
 tests/test_email_utils.py  | 60 ++++++++++++++++++-----------------
 tests/test_formatting.py   | 74 ++++++++++++++++++++++---------------------
 tests/test_message.py      | 10 +++---
 tests/test_thread.py       | 34 ++++++++++----------
 7 files changed, 140 insertions(+), 134 deletions(-)
pyproject.toml +0 -13
index e31a05c..7c9e1da 100644
@@ -57,18 +57,5 @@ strict = true
 module = "authheaders"
 ignore_missing_imports = true
 
-[[tool.mypy.overrides]]
-module = [
-    "test_email_utils",
-    "test_formatting",
-    "test_message",
-    "test_thread",
-]
-disable_error_code = ["attr-defined"]
-
-[[tool.mypy.overrides]]
-module = "test_auth_headers"
-disable_error_code = ["unused-ignore"]
-
 [tool.ruff]
 target-version = "py39"
tests/conftest.py +45 -34

92 lines not on this page · raw message

tests/test_auth_headers.py +2 -2
index 6c524b5..9edf4cd 100644
@@ -67,8 +67,8 @@ class TestAuthenticateMsgs:
             assert msg['Authentication-Results'] == (
                 'liblore; dkim=pass header.d=example.com'
             )
-            fake.authenticate_message.assert_called_once()  # type: ignore[attr-defined]
-            call_kwargs = fake.authenticate_message.call_args  # type: ignore[attr-defined]
+            fake.authenticate_message.assert_called_once()
+            call_kwargs = fake.authenticate_message.call_args
             assert call_kwargs[0][1] == 'liblore'
             assert call_kwargs[1]['dkim'] is True
             assert call_kwargs[1]['dmarc'] is True
tests/test_email_utils.py +31 -29

123 lines not on this page · raw message

tests/test_formatting.py +38 -36

217 lines not on this page · raw message

tests/test_message.py +6 -4
index 1a20afb..947e112 100644
@@ -4,6 +4,8 @@ from __future__ import annotations
 
 from email.message import EmailMessage
 
+from conftest import MsgFactory
+
 from liblore.utils import clean_header, get_clean_msgid, parse_message
 
 
@@ -35,16 +37,16 @@ class TestCleanHeader:
 
 
 class TestGetCleanMsgid:
-    def test_extracts_msgid(self, make_msg: type) -> None:
-        msg = make_msg.create(msgid='[email protected]')
+    def test_extracts_msgid(self, make_msg: MsgFactory) -> None:
+        msg = make_msg(msgid='[email protected]')
         assert get_clean_msgid(msg) == '[email protected]'
 
     def test_missing_header(self) -> None:
         msg = EmailMessage()
         assert get_clean_msgid(msg) is None
 
-    def test_custom_header(self, make_msg: type) -> None:
-        msg = make_msg.create(in_reply_to='[email protected]')
+    def test_custom_header(self, make_msg: MsgFactory) -> None:
+        msg = make_msg(in_reply_to='[email protected]')
         assert get_clean_msgid(msg, 'In-Reply-To') == '[email protected]'
 
 
tests/test_thread.py +18 -16
index b404a2a..62a9a1e 100644
@@ -2,35 +2,37 @@
 """Tests for liblore.thread."""
 from __future__ import annotations
 
+from conftest import MsgFactory
+
 from liblore.utils import get_clean_msgid, get_strict_thread
 
 
 class TestGetStrictThread:
-    def test_simple_thread(self, make_msg: type) -> None:
-        root = make_msg.create(msgid='[email protected]', subject='Root')
-        reply = make_msg.create(
+    def test_simple_thread(self, make_msg: MsgFactory) -> None:
+        root = make_msg(msgid='[email protected]', subject='Root')
+        reply = make_msg(
             msgid='[email protected]',
             subject='Re: Root',
             in_reply_to='[email protected]',
         )
-        unrelated = make_msg.create(msgid='[email protected]', subject='Other')
+        unrelated = make_msg(msgid='[email protected]', subject='Other')
         result = get_strict_thread([root, reply, unrelated], '[email protected]')
         assert result is not None
         ids = {get_clean_msgid(m) for m in result}
         assert ids == {'[email protected]', '[email protected]'}
 
-    def test_returns_none_for_missing_msgid(self, make_msg: type) -> None:
-        msg = make_msg.create(msgid='[email protected]')
+    def test_returns_none_for_missing_msgid(self, make_msg: MsgFactory) -> None:
+        msg = make_msg(msgid='[email protected]')
         result = get_strict_thread([msg], '[email protected]')
         assert result is None
 
-    def test_noparent(self, make_msg: type) -> None:
-        parent = make_msg.create(msgid='[email protected]')
-        child = make_msg.create(
+    def test_noparent(self, make_msg: MsgFactory) -> None:
+        parent = make_msg(msgid='[email protected]')
+        child = make_msg(
             msgid='[email protected]',
             in_reply_to='[email protected]',
         )
-        grandchild = make_msg.create(
+        grandchild = make_msg(
             msgid='[email protected]',
             in_reply_to='[email protected]',
         )
@@ -44,10 +46,10 @@ class TestGetStrictThread:
         assert '[email protected]' in ids
         assert '[email protected]' in ids
 
-    def test_references_chain(self, make_msg: type) -> None:
-        msg1 = make_msg.create(msgid='[email protected]')
-        msg2 = make_msg.create(msgid='[email protected]', references=['[email protected]'])
-        msg3 = make_msg.create(
+    def test_references_chain(self, make_msg: MsgFactory) -> None:
+        msg1 = make_msg(msgid='[email protected]')
+        msg2 = make_msg(msgid='[email protected]', references=['[email protected]'])
+        msg3 = make_msg(
             msgid='[email protected]',
             references=['[email protected]', '[email protected]'],
         )
@@ -55,8 +57,8 @@ class TestGetStrictThread:
         assert result is not None
         assert len(result) == 3
 
-    def test_single_message(self, make_msg: type) -> None:
-        msg = make_msg.create(msgid='[email protected]')
+    def test_single_message(self, make_msg: MsgFactory) -> None:
+        msg = make_msg(msgid='[email protected]')
         result = get_strict_thread([msg], '[email protected]')
         assert result is not None
         assert len(result) == 1
Signature · 1 line
2.53.0
#3 lore raw

PATCH v2 3/14 Add ruff import checks to b4 CI

In reply to Tamir Duberstein

Run ruff as part of the b4 CI check script and enable import sorting in Ruff so import ordering regressions show up in the review UI.

Keep Ruff as a subprocess and document the related upstream API limitation.

---
 ci.sh                      | 1 +
 pyproject.toml             | 4 ++--
 src/liblore/__init__.py    | 1 -
 src/liblore/node.py        | 3 +--
 src/liblore/utils.py       | 3 +--
 tests/conftest.py          | 3 +--
 tests/test_auth_headers.py | 1 -
 tests/test_email_utils.py  | 1 -
 tests/test_formatting.py   | 3 +--
 tests/test_mbox.py         | 4 +---
 tests/test_node.py         | 1 -
 11 files changed, 8 insertions(+), 17 deletions(-)
ci.sh +1 -0
index 1e8ccb3..3001db2 100755
@@ -2,5 +2,6 @@
 
 set -eu
 
+uv run ruff check
 uv run mypy .
 uv run pytest --durations=0
pyproject.toml +2 -2
index 7c9e1da..38be519 100644
@@ -57,5 +57,5 @@ strict = true
 module = "authheaders"
 ignore_missing_imports = true
 
-[tool.ruff]
-target-version = "py39"
+[tool.ruff.lint]
+extend-select = ["I"]
src/liblore/__init__.py +0 -1
index ff4314a..1ffaaeb 100644
@@ -3,7 +3,6 @@
 """liblore — shared library for public-inbox / lore.kernel.org access."""
 import email.charset
 import email.policy
-
 from email.message import EmailMessage
 
 __version__ = '0.7.1'
src/liblore/node.py +1 -2
index 5fb6343..66da1d7 100644
@@ -15,11 +15,10 @@ import time
 import types
 import urllib.parse
 from datetime import datetime, timezone
+from email.message import EmailMessage
 
 import requests
 
-from email.message import EmailMessage
-
 from liblore import LibloreError, RemoteError
 from liblore.utils import (
     get_strict_thread,
src/liblore/utils.py +1 -2
index 8d485da..e8926c6 100644
@@ -3,7 +3,6 @@
 """Message parsing, email utilities, threading, and mbox splitting."""
 from __future__ import annotations
 
-from collections.abc import Sequence
 import datetime
 import email.header
 import email.parser
@@ -14,7 +13,7 @@ import logging
 import re
 import textwrap
 import urllib.parse
-
+from collections.abc import Sequence
 from email.message import EmailMessage
 
 from liblore import emlpolicy
tests/conftest.py +1 -2
index b84fc35..ec8ec80 100644
@@ -4,12 +4,11 @@ from __future__ import annotations
 
 import email.utils
 import textwrap
+from email.message import EmailMessage
 from typing import Protocol
 
 import pytest
 
-from email.message import EmailMessage
-
 from liblore import emlpolicy
 
 
tests/test_auth_headers.py +0 -1
index 9edf4cd..9097048 100644
@@ -13,7 +13,6 @@ import pytest
 from liblore import LibloreError
 from liblore.node import LoreNode
 
-
 # =====================================================================
 # Import-time validation
 # =====================================================================
tests/test_email_utils.py +0 -1
index ad8775f..74216d4 100644
@@ -16,7 +16,6 @@ from liblore.utils import (
 )
 
 
-
 class TestMsgGetSubject:
     def test_plain_subject(self, make_msg: MsgFactory) -> None:
         msg = make_msg(subject='Just a plain subject')
tests/test_formatting.py +1 -2
index a13593e..cb3cf80 100644
@@ -2,10 +2,9 @@
 """Tests for email formatting and thread minimization."""
 from __future__ import annotations
 
-import pytest
-
 from email.message import EmailMessage
 
+import pytest
 from conftest import MsgFactory
 
 from liblore.utils import (
tests/test_mbox.py +1 -3
index 3815e3f..9bc2271 100644
@@ -3,9 +3,9 @@
 from __future__ import annotations
 
 import textwrap
-
 from email.message import EmailMessage
 
+from liblore import emlpolicy
 from liblore.utils import (
     get_clean_msgid,
     get_preferred_duplicate,
@@ -15,8 +15,6 @@ from liblore.utils import (
     split_mbox_as_bytes,
 )
 
-from liblore import emlpolicy
-
 
 class TestSplitMbox:
     def test_splits_two_messages(self, sample_mbox: bytes) -> None:
tests/test_node.py +0 -1
index c2e7bf3..54d76d8 100644
@@ -14,7 +14,6 @@ import requests
 from liblore import RemoteError
 from liblore.node import LoreNode
 
-
 # =====================================================================
 # Session management
 # =====================================================================
Signature · 1 line
2.53.0
#4 lore raw

PATCH v2 4/14 Add ruff format check to CI

In reply to Tamir Duberstein

Enable ruff format checking in the b4 CI script and configure Ruff's formatter in pyproject.toml.

Apply a one-time repo-wide format pass so the new check enforces the current style without leaving the branch permanently red.

---
 ci.sh                      |   1 +
 pyproject.toml             |   3 +
 src/liblore/__init__.py    |   1 +
 src/liblore/node.py        |  61 ++++++-----
 src/liblore/utils.py       |  86 +++++++++++-----
 tests/conftest.py          |   1 +
 tests/test_auth_headers.py |   4 +
 tests/test_cache.py        |  23 +++--
 tests/test_email_utils.py  |   2 +-
 tests/test_formatting.py   | 251 ++++++++++++++++++++++++++++-----------------
 tests/test_mbox.py         |   3 +
 tests/test_message.py      |   1 +
 tests/test_node.py         | 152 +++++++++++++++++++--------
 tests/test_thread.py       |   1 +
 14 files changed, 395 insertions(+), 195 deletions(-)
ci.sh +1 -0
index 3001db2..ec0baf8 100755
@@ -2,6 +2,7 @@
 
 set -eu
 
+uv run ruff format --check
 uv run ruff check
 uv run mypy .
 uv run pytest --durations=0
pyproject.toml +3 -0
index 38be519..9bfadbe 100644
@@ -59,3 +59,6 @@ ignore_missing_imports = true
 
 [tool.ruff.lint]
 extend-select = ["I"]
+
+[tool.ruff.format]
+quote-style = "single"
src/liblore/__init__.py +1 -0
index 1ffaaeb..2ba94e7 100644
@@ -1,6 +1,7 @@
 # SPDX-License-Identifier: GPL-2.0-or-later
 # Copyright (C) 2025-2026 The Linux Foundation
 """liblore — shared library for public-inbox / lore.kernel.org access."""
+
 import email.charset
 import email.policy
 from email.message import EmailMessage
src/liblore/node.py +38 -23

167 lines not on this page · raw message

src/liblore/utils.py +58 -28

184 lines not on this page · raw message

tests/conftest.py +1 -0
index ec8ec80..28fedaf 100644
@@ -1,5 +1,6 @@
 # SPDX-License-Identifier: GPL-2.0-or-later
 """Shared fixtures for liblore tests."""
+
 from __future__ import annotations
 
 import email.utils
tests/test_auth_headers.py +4 -0
index 9097048..8cec624 100644
@@ -1,5 +1,6 @@
 # SPDX-License-Identifier: GPL-2.0-or-later
 """Tests for optional authheaders integration in LoreNode."""
+
 from __future__ import annotations
 
 import gzip
@@ -17,6 +18,7 @@ from liblore.node import LoreNode
 # Import-time validation
 # =====================================================================
 
+
 class TestAuthHeadersImport:
     def test_raises_when_authheaders_missing(self) -> None:
         with patch.dict(sys.modules, {'authheaders': None}):
@@ -41,6 +43,7 @@ class TestAuthHeadersImport:
 # _authenticate_msgs
 # =====================================================================
 
+
 class TestAuthenticateMsgs:
     def test_noop_when_disabled(self) -> None:
         node = LoreNode()
@@ -116,6 +119,7 @@ class TestAuthenticateMsgs:
 # Integration with fetch methods
 # =====================================================================
 
+
 class TestAuthInFetchMethods:
     @pytest.fixture()
     def auth_node(self, sample_mbox: bytes) -> LoreNode:
tests/test_cache.py +16 -7
index 9e7933b..b822e7c 100644
@@ -1,5 +1,6 @@
 # SPDX-License-Identifier: GPL-2.0-or-later
 """Tests for LoreNode filesystem caching."""
+
 from __future__ import annotations
 
 import gzip
@@ -82,7 +83,6 @@ class TestCacheReadWrite:
 
 
 class TestClearCache:
-
     def test_clears_cache_files(self, tmp_path: Path) -> None:
         node = LoreNode(cache_dir=str(tmp_path))
         node._cache_write(node._cache_key('a', '1'), b'data1')
@@ -104,7 +104,6 @@ class TestClearCache:
 
 
 class TestProperties:
-
     def test_url_property(self) -> None:
         node = LoreNode('https://lore.kernel.org/all/')
         # Trailing slash is stripped by constructor
@@ -122,8 +121,12 @@ class TestProperties:
 class TestCachedMethods:
     """Integration tests: verify cache hit avoids network, cache miss hits network."""
 
-    def _make_node(self, tmp_path: Path, sample_mbox: bytes) -> tuple[LoreNode, MagicMock]:
-        node = LoreNode('https://lore.kernel.org/all', cache_dir=str(tmp_path), cache_ttl=60)
+    def _make_node(
+        self, tmp_path: Path, sample_mbox: bytes
+    ) -> tuple[LoreNode, MagicMock]:
+        node = LoreNode(
+            'https://lore.kernel.org/all', cache_dir=str(tmp_path), cache_ttl=60
+        )
         mock_session = MagicMock()
         mock_resp = MagicMock()
         mock_resp.status_code = 200
@@ -158,7 +161,9 @@ class TestCachedMethods:
         assert mock_session.post.call_count == 2
 
     def test_get_message_by_msgid_caches(self, tmp_path: Path) -> None:
-        node = LoreNode('https://lore.kernel.org/all', cache_dir=str(tmp_path), cache_ttl=60)
+        node = LoreNode(
+            'https://lore.kernel.org/all', cache_dir=str(tmp_path), cache_ttl=60
+        )
         mock_session = MagicMock()
         mock_resp = MagicMock()
         mock_resp.status_code = 200
@@ -197,7 +202,9 @@ class TestCachedMethods:
         # tmp_path should be empty since we didn't set cache_dir to it
         assert list(tmp_path.iterdir()) == []
 
-    def test_fetch_thread_since_not_cached(self, tmp_path: Path, sample_mbox: bytes) -> None:
+    def test_fetch_thread_since_not_cached(
+        self, tmp_path: Path, sample_mbox: bytes
+    ) -> None:
         """_fetch_thread_since should NOT be cached."""
         node, mock_session = self._make_node(tmp_path, sample_mbox)
         node._fetch_thread_since('[email protected]', 'dt:20240101..')
@@ -207,7 +214,9 @@ class TestCachedMethods:
 
     def test_error_not_cached(self, tmp_path: Path) -> None:
         """Network errors should not be cached."""
-        node = LoreNode('https://lore.kernel.org/all', cache_dir=str(tmp_path), cache_ttl=60)
+        node = LoreNode(
+            'https://lore.kernel.org/all', cache_dir=str(tmp_path), cache_ttl=60
+        )
         mock_session = MagicMock()
         mock_resp = MagicMock()
         mock_resp.status_code = 404
tests/test_email_utils.py +1 -1
index 74216d4..9276010 100644
@@ -1,5 +1,6 @@
 # SPDX-License-Identifier: GPL-2.0-or-later
 """Tests for liblore.email_utils."""
+
 from __future__ import annotations
 
 from email.message import EmailMessage
@@ -100,7 +101,6 @@ class TestMsgGetPayload:
         assert msg_get_payload(msg) == ''
 
 
-
 class TestMsgGetRecipients:
     def test_to_cc_from(self) -> None:
         msg = EmailMessage(policy=emlpolicy)
tests/test_formatting.py +156 -95

289 lines not on this page · raw message

tests/test_mbox.py +3 -0
index 9bc2271..7269192 100644
@@ -1,5 +1,6 @@
 # SPDX-License-Identifier: GPL-2.0-or-later
 """Tests for liblore.mbox."""
+
 from __future__ import annotations
 
 import textwrap
@@ -176,6 +177,7 @@ class TestSplitMboxAsBytes:
     def test_roundtrip_with_split_mbox(self, sample_mbox: bytes) -> None:
         """split_mbox_as_bytes + parse_message should match split_mbox."""
         from liblore.utils import parse_message
+
         raw_chunks = split_mbox_as_bytes(sample_mbox)
         parsed = split_mbox(sample_mbox)
         assert len(raw_chunks) == len(parsed)
@@ -265,6 +267,7 @@ class TestSplitAndDedupeAsBytes:
     def test_roundtrip_with_split_and_dedupe(self, sample_mbox: bytes) -> None:
         """Parsing the bytes output should match split_and_dedupe."""
         from liblore.utils import parse_message
+
         doubled = sample_mbox + sample_mbox
         chunks = split_and_dedupe_as_bytes(doubled)
         msgs = split_and_dedupe(doubled)
tests/test_message.py +1 -0
index 947e112..77c4cd4 100644
@@ -1,5 +1,6 @@
 # SPDX-License-Identifier: GPL-2.0-or-later
 """Tests for liblore.message."""
+
 from __future__ import annotations
 
 from email.message import EmailMessage
tests/test_node.py +111 -41

430 lines not on this page · raw message

tests/test_thread.py +1 -0
index 62a9a1e..d835373 100644
@@ -1,5 +1,6 @@
 # SPDX-License-Identifier: GPL-2.0-or-later
 """Tests for liblore.thread."""
+
 from __future__ import annotations
 
 from conftest import MsgFactory
Signature · 1 line
2.53.0
#5 lore raw

PATCH v2 5/14 Add pyright strict checks to CI

In reply to Tamir Duberstein

Configure pyright in strict mode and run it from the b4 CI checker.

Allow private-usage checks to be disabled only for tests via a pyright execution environment override, and silence missing type stubs for the optional authheaders dependency.

Add a small number of casts in tests to satisfy pyright. These casts should be removed in a follow-up commit by tightening the mocked types further.

---
 ci.sh               |  1 +
 pyproject.toml      | 11 ++++++++---
 src/liblore/node.py |  2 +-
 tests/test_node.py  | 11 ++++++++---
 4 files changed, 18 insertions(+), 7 deletions(-)
ci.sh +1 -0
index ec0baf8..4b07fa2 100755
@@ -5,4 +5,5 @@ set -eu
 uv run ruff format --check
 uv run ruff check
 uv run mypy .
+uv run pyright
 uv run pytest --durations=0
pyproject.toml +8 -3
index 9bfadbe..7a96d23 100644
@@ -39,6 +39,7 @@ Repository = "https://git.kernel.org/pub/scm/utils/liblore/liblore.git"
 dev = [
     "build",
     "mypy",
+    "pyright",
     "pytest",
     "pytest-asyncio",
     "ruff",
@@ -53,9 +54,13 @@ asyncio_default_fixture_loop_scope = "function"
 [tool.mypy]
 strict = true
 
-[[tool.mypy.overrides]]
-module = "authheaders"
-ignore_missing_imports = true
+[tool.pyright]
+typeCheckingMode = "strict"
+
+executionEnvironments = [
+    # We're testing private APIs quite a bit.
+    { root = "tests", reportPrivateUsage = false },
+]
 
 [tool.ruff.lint]
 extend-select = ["I"]
src/liblore/node.py +1 -1
index 503428b..095ba4f 100644
@@ -195,7 +195,7 @@ class LoreNode:
         self._authheaders: types.ModuleType | None = None
         if add_auth_headers:
             try:
-                import authheaders
+                import authheaders  # type: ignore[import-untyped]
 
                 self._authheaders = authheaders
             except ImportError:
tests/test_node.py +8 -3
index 8f4ccac..af140db 100644
@@ -7,6 +7,7 @@ import gzip
 import os
 from datetime import datetime, timezone
 from email.message import EmailMessage
+from typing import cast
 from unittest.mock import MagicMock, call, patch
 
 import pytest
@@ -25,7 +26,9 @@ class TestSessionManagement:
         node = LoreNode()
         s = node._get_session()
         assert s is not None
-        assert 'liblore/' in s.headers['User-Agent']
+        user_agent = s.headers['User-Agent']
+        assert isinstance(user_agent, str)
+        assert 'liblore/' in user_agent
         node.close()
 
     def test_returns_same_session(self) -> None:
@@ -74,7 +77,9 @@ class TestSessionManagement:
     def test_default_no_plus(self) -> None:
         node = LoreNode()
         s = node._get_session()
-        assert '+' not in s.headers['User-Agent']
+        user_agent = s.headers['User-Agent']
+        assert isinstance(user_agent, str)
+        assert '+' not in user_agent
         node.close()
 
     def test_set_requests_session(self) -> None:
@@ -1008,7 +1013,7 @@ class TestProbeOrigins:
         def fake_head(url: str, **kwargs: object) -> MagicMock:
             headers = kwargs.get('headers', {})
             assert isinstance(headers, dict)
-            captured_headers.append(headers)
+            captured_headers.append(cast(dict[str, str], headers))
             resp = MagicMock()
             resp.status_code = 200
             return resp
Signature · 1 line
2.53.0
#6 lore raw

PATCH v2 6/14 Replace HTTP session mocks with responses

In reply to Tamir Duberstein

Convert networking tests from patched requests sessions to responses mocks. Each test now registers requests on a local RequestsMock so expected HTTP traffic is declared inline.

Add an autouse responses fixture to block real network access outside explicit mock blocks.

---
 pyproject.toml             |    3 +-
 tests/conftest.py          |    9 +-
 tests/test_auth_headers.py |   45 +-
 tests/test_cache.py        |  207 +++---
 tests/test_node.py         | 1708 ++++++++++++++++++++++++--------------------
 5 files changed, 1097 insertions(+), 875 deletions(-)
pyproject.toml +2 -1
index 7a96d23..a160edd 100644
@@ -42,6 +42,7 @@ dev = [
     "pyright",
     "pytest",
     "pytest-asyncio",
+    "responses",
     "ruff",
     "types-requests",
 ]
@@ -63,7 +64,7 @@ executionEnvironments = [
 ]
 
 [tool.ruff.lint]
-extend-select = ["I"]
+extend-select = ["ARG", "I"]
 
 [tool.ruff.format]
 quote-style = "single"
tests/conftest.py +8 -1
index 28fedaf..ea52d95 100644
@@ -6,13 +6,20 @@ from __future__ import annotations
 import email.utils
 import textwrap
 from email.message import EmailMessage
-from typing import Protocol
+from typing import Iterator, Protocol
 
 import pytest
+import responses as responses_
 
 from liblore import emlpolicy
 
 
[email protected](autouse=True)
+def _block_network() -> Iterator[None]:  # pyright: ignore[reportUnusedFunction]
+    with responses_.RequestsMock():
+        yield
+
+
 class MsgFactory(Protocol):
     def __call__(
         self,
tests/test_auth_headers.py +31 -14

73 lines not on this page · raw message

tests/test_cache.py +126 -81

244 lines not on this page · raw message

tests/test_node.py +930 -778

1,956 lines not on this page · raw message

Signature · 1 line
2.53.0
#7 lore raw

PATCH v2 7/14 Add ty checks to CI

In reply to Tamir Duberstein

Add ty to the development dependencies and enable all ty rules. Run ty in the b4 CI check script so its diagnostics are surfaced with the other checks.

Use ty check --add-ignore to suppress existing errors.

---
 ci.sh                      |  1 +
 pyproject.toml             |  4 ++++
 src/liblore/node.py        |  2 +-
 tests/test_auth_headers.py | 10 +++++-----
 tests/test_node.py         | 30 +++++++++++++++---------------
 5 files changed, 26 insertions(+), 21 deletions(-)
ci.sh +1 -0
index 4b07fa2..c97c853 100755
@@ -4,6 +4,7 @@ set -eu
 
 uv run ruff format --check
 uv run ruff check
+uv run ty check
 uv run mypy .
 uv run pyright
 uv run pytest --durations=0
pyproject.toml +4 -0
index a160edd..37c85a7 100644
@@ -44,6 +44,7 @@ dev = [
     "pytest-asyncio",
     "responses",
     "ruff",
+    "ty",
     "types-requests",
 ]
 
@@ -63,6 +64,9 @@ executionEnvironments = [
     { root = "tests", reportPrivateUsage = false },
 ]
 
+[tool.ty.rules]
+all = "error"
+
 [tool.ruff.lint]
 extend-select = ["ARG", "I"]
 
src/liblore/node.py +1 -1
index 095ba4f..1b0463e 100644
@@ -319,7 +319,7 @@ class LoreNode:
                 except ValueError:
                     pass
 
-        node = cls(url, **kwargs)  # type: ignore[arg-type]
+        node = cls(url, **kwargs)  # type: ignore[arg-type]  # ty:ignore[invalid-argument-type]
 
         val = gitcfg.get('useragentplus')
         if isinstance(val, str) and val:
tests/test_auth_headers.py +5 -5
index f1c240a..b51050c 100644
@@ -29,7 +29,7 @@ class TestAuthHeadersImport:
 
     def test_ok_when_authheaders_installed(self) -> None:
         fake = ModuleType('authheaders')
-        fake.authenticate_message = MagicMock()  # type: ignore[attr-defined]
+        fake.authenticate_message = MagicMock()  # type: ignore[attr-defined]  # ty:ignore[unresolved-attribute]
         with patch.dict(sys.modules, {'authheaders': fake}):
             node = LoreNode(add_auth_headers=True)
             assert node._authheaders is not None
@@ -56,7 +56,7 @@ class TestAuthenticateMsgs:
 
     def test_adds_header_when_enabled(self) -> None:
         fake = ModuleType('authheaders')
-        fake.authenticate_message = MagicMock(  # type: ignore[attr-defined]
+        fake.authenticate_message = MagicMock(  # type: ignore[attr-defined]  # ty:ignore[unresolved-attribute]
             return_value='Authentication-Results: liblore; dkim=pass header.d=example.com',
         )
         with patch.dict(sys.modules, {'authheaders': fake}):
@@ -82,7 +82,7 @@ class TestAuthenticateMsgs:
 
     def test_skips_empty_result(self) -> None:
         fake = ModuleType('authheaders')
-        fake.authenticate_message = MagicMock(return_value='')  # type: ignore[attr-defined]
+        fake.authenticate_message = MagicMock(return_value='')  # type: ignore[attr-defined]  # ty:ignore[unresolved-attribute]
         with patch.dict(sys.modules, {'authheaders': fake}):
             node = LoreNode(add_auth_headers=True)
             msg = EmailMessage()
@@ -95,7 +95,7 @@ class TestAuthenticateMsgs:
 
     def test_multiple_messages(self) -> None:
         fake = ModuleType('authheaders')
-        fake.authenticate_message = MagicMock(  # type: ignore[attr-defined]
+        fake.authenticate_message = MagicMock(  # type: ignore[attr-defined]  # ty:ignore[unresolved-attribute]
             side_effect=[
                 'liblore; dkim=pass',
                 'Authentication-Results: liblore; dkim=fail',
@@ -126,7 +126,7 @@ class TestAuthInFetchMethods:
     @pytest.fixture()
     def auth_node(self) -> Iterator[tuple[LoreNode, responses.RequestsMock]]:
         fake = ModuleType('authheaders')
-        fake.authenticate_message = MagicMock(  # type: ignore[attr-defined]
+        fake.authenticate_message = MagicMock(  # type: ignore[attr-defined]  # ty:ignore[unresolved-attribute]
             return_value='Authentication-Results: liblore; dkim=pass',
         )
         with patch.dict(sys.modules, {'authheaders': fake}):
tests/test_node.py +15 -15
index fc94d9f..f4a3495 100644
@@ -500,19 +500,19 @@ class TestBatchGetThreadByMsgid:
         node = LoreNode()
         thread_a = [EmailMessage()]
         thread_b = [EmailMessage(), EmailMessage()]
-        node.get_thread_by_msgid = MagicMock(side_effect=[thread_a, thread_b])  # type: ignore[method-assign]
+        node.get_thread_by_msgid = MagicMock(side_effect=[thread_a, thread_b])  # type: ignore[method-assign]  # ty:ignore[invalid-assignment]
 
         with patch('liblore.node.time.sleep') as mock_sleep:
             results = node.batch_get_thread_by_msgid(['a@x', 'b@x'])
 
         assert results == [thread_a, thread_b]
-        assert node.get_thread_by_msgid.call_count == 2
+        assert node.get_thread_by_msgid.call_count == 2  # ty:ignore[unresolved-attribute]
         mock_sleep.assert_called_once_with(0.1)
 
     def test_no_sleep_for_single_msgid(self) -> None:
         node = LoreNode()
         thread = [EmailMessage()]
-        node.get_thread_by_msgid = MagicMock(return_value=thread)  # type: ignore[method-assign]
+        node.get_thread_by_msgid = MagicMock(return_value=thread)  # type: ignore[method-assign]  # ty:ignore[invalid-assignment]
 
         with patch('liblore.node.time.sleep') as mock_sleep:
             results = node.batch_get_thread_by_msgid(['only@x'])
@@ -522,7 +522,7 @@ class TestBatchGetThreadByMsgid:
 
     def test_passes_kwargs(self) -> None:
         node = LoreNode()
-        node.get_thread_by_msgid = MagicMock(return_value=[EmailMessage()])  # type: ignore[method-assign]
+        node.get_thread_by_msgid = MagicMock(return_value=[EmailMessage()])  # type: ignore[method-assign]  # ty:ignore[invalid-assignment]
 
         with patch('liblore.node.time.sleep'):
             node.batch_get_thread_by_msgid(
@@ -532,7 +532,7 @@ class TestBatchGetThreadByMsgid:
                 since='20240101',
             )
 
-        node.get_thread_by_msgid.assert_called_once_with(
+        node.get_thread_by_msgid.assert_called_once_with(  # ty:ignore[unresolved-attribute]
             'a@x',
             strict=False,
             sort=True,
@@ -541,7 +541,7 @@ class TestBatchGetThreadByMsgid:
 
     def test_sleep_count_matches_gaps(self) -> None:
         node = LoreNode()
-        node.get_thread_by_msgid = MagicMock(return_value=[EmailMessage()])  # type: ignore[method-assign]
+        node.get_thread_by_msgid = MagicMock(return_value=[EmailMessage()])  # type: ignore[method-assign]  # ty:ignore[invalid-assignment]
 
         with patch('liblore.node.time.sleep') as mock_sleep:
             node.batch_get_thread_by_msgid(['a@x', 'b@x', 'c@x'])
@@ -550,14 +550,14 @@ class TestBatchGetThreadByMsgid:
 
     def test_empty_list(self) -> None:
         node = LoreNode()
-        node.get_thread_by_msgid = MagicMock()  # type: ignore[method-assign]
+        node.get_thread_by_msgid = MagicMock()  # type: ignore[method-assign]  # ty:ignore[invalid-assignment]
 
         with patch('liblore.node.time.sleep') as mock_sleep:
             results = node.batch_get_thread_by_msgid([])
 
         assert results == []
         mock_sleep.assert_not_called()
-        node.get_thread_by_msgid.assert_not_called()
+        node.get_thread_by_msgid.assert_not_called()  # ty:ignore[unresolved-attribute]
 
 
 # =====================================================================
@@ -570,19 +570,19 @@ class TestBatchGetThreadByQuery:
         node = LoreNode()
         result_a = [EmailMessage()]
         result_b = [EmailMessage(), EmailMessage()]
-        node.get_thread_by_query = MagicMock(side_effect=[result_a, result_b])  # type: ignore[method-assign]
+        node.get_thread_by_query = MagicMock(side_effect=[result_a, result_b])  # type: ignore[method-assign]  # ty:ignore[invalid-assignment]
 
         with patch('liblore.node.time.sleep') as mock_sleep:
             results = node.batch_get_thread_by_query(['q1', 'q2'])
 
         assert results == [result_a, result_b]
-        assert node.get_thread_by_query.call_count == 2
+        assert node.get_thread_by_query.call_count == 2  # ty:ignore[unresolved-attribute]
         mock_sleep.assert_called_once_with(0.1)
 
     def test_no_sleep_for_single_query(self) -> None:
         node = LoreNode()
         result = [EmailMessage()]
-        node.get_thread_by_query = MagicMock(return_value=result)  # type: ignore[method-assign]
+        node.get_thread_by_query = MagicMock(return_value=result)  # type: ignore[method-assign]  # ty:ignore[invalid-assignment]
 
         with patch('liblore.node.time.sleep') as mock_sleep:
             results = node.batch_get_thread_by_query(['only_query'])
@@ -592,7 +592,7 @@ class TestBatchGetThreadByQuery:
 
     def test_sleep_count_matches_gaps(self) -> None:
         node = LoreNode()
-        node.get_thread_by_query = MagicMock(return_value=[EmailMessage()])  # type: ignore[method-assign]
+        node.get_thread_by_query = MagicMock(return_value=[EmailMessage()])  # type: ignore[method-assign]  # ty:ignore[invalid-assignment]
 
         with patch('liblore.node.time.sleep') as mock_sleep:
             node.batch_get_thread_by_query(['q1', 'q2', 'q3', 'q4'])
@@ -601,14 +601,14 @@ class TestBatchGetThreadByQuery:
 
     def test_empty_list(self) -> None:
         node = LoreNode()
-        node.get_thread_by_query = MagicMock()  # type: ignore[method-assign]
+        node.get_thread_by_query = MagicMock()  # type: ignore[method-assign]  # ty:ignore[invalid-assignment]
 
         with patch('liblore.node.time.sleep') as mock_sleep:
             results = node.batch_get_thread_by_query([])
 
         assert results == []
         mock_sleep.assert_not_called()
-        node.get_thread_by_query.assert_not_called()
+        node.get_thread_by_query.assert_not_called()  # ty:ignore[unresolved-attribute]
 
 
 # =====================================================================
@@ -1928,7 +1928,7 @@ class TestUserAgentPlusProperty:
         """Property has no setter — assignment raises AttributeError."""
         node = LoreNode()
         with pytest.raises(AttributeError):
-            node.user_agent_plus = 'nope'  # type: ignore[misc]
+            node.user_agent_plus = 'nope'  # type: ignore[misc]  # ty:ignore[invalid-assignment]
 
 
 # =====================================================================
Signature · 1 line
2.53.0
#8 lore raw

PATCH v2 8/14 Drop redundant read-only property test

In reply to Tamir Duberstein

Remove the user_agent_plus assignment test. It's not clear to me what the point of this test is, and doing this already produces type checking errors.

---
 tests/test_node.py | 6 ------
 1 file changed, 6 deletions(-)
tests/test_node.py +0 -6
index f4a3495..4abb4f4 100644
@@ -1924,12 +1924,6 @@ class TestUserAgentPlusProperty:
 
         assert node.user_agent_plus == 'my-tracking-uuid'
 
-    def test_read_only(self) -> None:
-        """Property has no setter — assignment raises AttributeError."""
-        node = LoreNode()
-        with pytest.raises(AttributeError):
-            node.user_agent_plus = 'nope'  # type: ignore[misc]  # ty:ignore[invalid-assignment]
-
 
 # =====================================================================
 # Public API: origins property
Signature · 1 line
2.53.0
#9 lore raw

PATCH v2 9/14 Type from_git_config keyword arguments

In reply to Tamir Duberstein

Define a TypedDict for LoreNode constructor keyword arguments and annotate from_git_config with an unpacked kwargs signature.

This removes the constructor-call type suppressions while keeping git-config defaults and caller overrides unchanged.

---
 pyproject.toml      |  1 +
 src/liblore/node.py | 18 ++++++++++++++++--
 2 files changed, 17 insertions(+), 2 deletions(-)
pyproject.toml +1 -0
index 37c85a7..387757c 100644
@@ -46,6 +46,7 @@ dev = [
     "ruff",
     "ty",
     "types-requests",
+    "typing-extensions",
 ]
 
 [tool.pytest.ini_options]
src/liblore/node.py +16 -2
index 1b0463e..f9817c1 100644
@@ -17,6 +17,7 @@ import types
 import urllib.parse
 from datetime import datetime, timezone
 from email.message import EmailMessage
+from typing import TYPE_CHECKING, TypedDict
 
 import requests
 
@@ -29,6 +30,19 @@ from liblore.utils import (
 
 logger = logging.getLogger(__name__)
 
+if TYPE_CHECKING:
+    from typing_extensions import Unpack
+
+
+class _LoreNodeInitKwargs(TypedDict, total=False):
+    fallback_urls: list[str] | None
+    auto_probe: bool
+    probe_timeout: float
+    probe_ttl: int
+    add_auth_headers: bool
+    cache_dir: str | None
+    cache_ttl: int
+
 
 def _get_config_from_git(
     regexp: str,
@@ -212,7 +226,7 @@ class LoreNode:
     def from_git_config(
         cls,
         url: str = 'https://lore.kernel.org/all',
-        **kwargs: object,
+        **kwargs: Unpack[_LoreNodeInitKwargs],
     ) -> LoreNode:
         """Create a :class:`LoreNode` using settings from git config.
 
@@ -319,7 +333,7 @@ class LoreNode:
                 except ValueError:
                     pass
 
-        node = cls(url, **kwargs)  # type: ignore[arg-type]  # ty:ignore[invalid-argument-type]
+        node = cls(url, **kwargs)
 
         val = gitcfg.get('useragentplus')
         if isinstance(val, str) and val:
Signature · 1 line
2.53.0
#10 lore raw

PATCH v2 10/14 Add authheaders stub and typed callable

In reply to Tamir Duberstein

Add a generated authheaders stub under typings so the type checkers can resolve the package. Then store authenticate_message directly on LoreNode as a typed callable and update the authheaders tests to use that shape.

---
 src/liblore/node.py              | 26 +++++++++++++++++++-------
 tests/test_auth_headers.py       | 31 ++++++++++++++++++-------------
 typings/authheaders/__init__.pyi | 13 +++++++++++++
 3 files changed, 50 insertions(+), 20 deletions(-)
src/liblore/node.py +19 -7
index f9817c1..8935dbf 100644
@@ -13,11 +13,10 @@ import os
 import re
 import subprocess
 import time
-import types
 import urllib.parse
 from datetime import datetime, timezone
 from email.message import EmailMessage
-from typing import TYPE_CHECKING, TypedDict
+from typing import TYPE_CHECKING, Protocol, TypedDict
 
 import requests
 
@@ -44,6 +43,19 @@ class _LoreNodeInitKwargs(TypedDict, total=False):
     cache_ttl: int
 
 
+class _AuthenticateMessage(Protocol):
+    def __call__(
+        self,
+        msg: bytes,
+        authserv_id: str,
+        *,
+        dkim: bool = ...,
+        dmarc: bool = ...,
+        arc: bool = ...,
+        spf: bool = ...,
+    ) -> str: ...
+
+
 def _get_config_from_git(
     regexp: str,
     multivals: list[str] | None = None,
@@ -206,12 +218,12 @@ class LoreNode:
 
         if cache_dir is not None:
             os.makedirs(cache_dir, exist_ok=True)
-        self._authheaders: types.ModuleType | None = None
+        self._authenticate_message: _AuthenticateMessage | None = None
         if add_auth_headers:
             try:
-                import authheaders  # type: ignore[import-untyped]
+                import authheaders
 
-                self._authheaders = authheaders
+                self._authenticate_message = authheaders.authenticate_message
             except ImportError:
                 raise LibloreError(
                     'authheaders library is required for add_auth_headers. '
@@ -721,11 +733,11 @@ class LoreNode:
 
     def _authenticate_msgs(self, msgs: list[EmailMessage]) -> None:
         """Add Authentication-Results headers via authheaders."""
-        if self._authheaders is None:
+        if self._authenticate_message is None:
             return
         for msg in msgs:
             msg_bytes = msg.as_bytes()
-            auth_result = self._authheaders.authenticate_message(
+            auth_result = self._authenticate_message(
                 msg_bytes,
                 'liblore',
                 dkim=True,
tests/test_auth_headers.py +18 -13
index b51050c..e149863 100644
@@ -14,7 +14,12 @@ import pytest
 import responses
 
 from liblore import LibloreError
-from liblore.node import LoreNode
+from liblore.node import LoreNode, _AuthenticateMessage
+
+
+class _FakeAuthHeaders(ModuleType):
+    authenticate_message: _AuthenticateMessage
+
 
 # =====================================================================
 # Import-time validation
@@ -28,16 +33,16 @@ class TestAuthHeadersImport:
                 LoreNode(add_auth_headers=True)
 
     def test_ok_when_authheaders_installed(self) -> None:
-        fake = ModuleType('authheaders')
-        fake.authenticate_message = MagicMock()  # type: ignore[attr-defined]  # ty:ignore[unresolved-attribute]
+        fake = _FakeAuthHeaders('authheaders')
+        fake.authenticate_message = MagicMock()
         with patch.dict(sys.modules, {'authheaders': fake}):
             node = LoreNode(add_auth_headers=True)
-            assert node._authheaders is not None
+            assert node._authenticate_message is not None
             node.close()
 
     def test_default_is_disabled(self) -> None:
         node = LoreNode()
-        assert node._authheaders is None
+        assert node._authenticate_message is None
         node.close()
 
 
@@ -55,8 +60,8 @@ class TestAuthenticateMsgs:
         assert 'Authentication-Results' not in msg
 
     def test_adds_header_when_enabled(self) -> None:
-        fake = ModuleType('authheaders')
-        fake.authenticate_message = MagicMock(  # type: ignore[attr-defined]  # ty:ignore[unresolved-attribute]
+        fake = _FakeAuthHeaders('authheaders')
+        fake.authenticate_message = MagicMock(
             return_value='Authentication-Results: liblore; dkim=pass header.d=example.com',
         )
         with patch.dict(sys.modules, {'authheaders': fake}):
@@ -81,8 +86,8 @@ class TestAuthenticateMsgs:
             node.close()
 
     def test_skips_empty_result(self) -> None:
-        fake = ModuleType('authheaders')
-        fake.authenticate_message = MagicMock(return_value='')  # type: ignore[attr-defined]  # ty:ignore[unresolved-attribute]
+        fake = _FakeAuthHeaders('authheaders')
+        fake.authenticate_message = MagicMock(return_value='')
         with patch.dict(sys.modules, {'authheaders': fake}):
             node = LoreNode(add_auth_headers=True)
             msg = EmailMessage()
@@ -94,8 +99,8 @@ class TestAuthenticateMsgs:
             node.close()
 
     def test_multiple_messages(self) -> None:
-        fake = ModuleType('authheaders')
-        fake.authenticate_message = MagicMock(  # type: ignore[attr-defined]  # ty:ignore[unresolved-attribute]
+        fake = _FakeAuthHeaders('authheaders')
+        fake.authenticate_message = MagicMock(
             side_effect=[
                 'liblore; dkim=pass',
                 'Authentication-Results: liblore; dkim=fail',
@@ -125,8 +130,8 @@ class TestAuthenticateMsgs:
 class TestAuthInFetchMethods:
     @pytest.fixture()
     def auth_node(self) -> Iterator[tuple[LoreNode, responses.RequestsMock]]:
-        fake = ModuleType('authheaders')
-        fake.authenticate_message = MagicMock(  # type: ignore[attr-defined]  # ty:ignore[unresolved-attribute]
+        fake = _FakeAuthHeaders('authheaders')
+        fake.authenticate_message = MagicMock(
             return_value='Authentication-Results: liblore; dkim=pass',
         )
         with patch.dict(sys.modules, {'authheaders': fake}):
typings/authheaders/__init__.pyi new file +13 -0
new file mode 100644
index 0000000..ed4f61e
@@ -0,0 +1,13 @@
+"""
+This type stub file was generated by pyright.
+"""
+
+def authenticate_message(
+    msg: bytes,
+    authserv_id: str,
+    *,
+    dkim: bool = ...,
+    dmarc: bool = ...,
+    arc: bool = ...,
+    spf: bool = ...,
+) -> str: ...
Signature · 1 line
2.53.0
#11 lore raw

PATCH v2 11/14 Replace batch mocks with subclasses

In reply to Tamir Duberstein

Replace method reassignment in the batch helper tests with small LoreNode subclasses that override the fetch methods and record calls. This removes the type-checking suppressions without adding more patch-based mocking.

---
 tests/test_node.py | 88 +++++++++++++++++++++++++++++++-----------------------
 1 file changed, 51 insertions(+), 37 deletions(-)
tests/test_node.py +51 -37
index 4abb4f4..73c79a3 100644
@@ -12,6 +12,7 @@ from unittest.mock import MagicMock, call, patch
 import pytest
 import requests
 import responses
+from typing_extensions import override
 
 from liblore import RemoteError
 from liblore.node import LoreNode
@@ -496,24 +497,38 @@ class TestGetMessageByMsgid:
 
 
 class TestBatchGetThreadByMsgid:
+    class _Node(LoreNode):
+        def __init__(self, results: list[list[EmailMessage]]) -> None:
+            super().__init__()
+            self.calls: list[tuple[str, bool, bool, str | None]] = []
+            self._results = results
+
+        @override
+        def get_thread_by_msgid(
+            self,
+            msgid: str,
+            *,
+            strict: bool = True,
+            sort: bool = False,
+            since: str | None = None,
+        ) -> list[EmailMessage]:
+            self.calls.append((msgid, strict, sort, since))
+            return self._results.pop(0)
+
     def test_returns_ordered_results(self) -> None:
-        node = LoreNode()
         thread_a = [EmailMessage()]
         thread_b = [EmailMessage(), EmailMessage()]
-        node.get_thread_by_msgid = MagicMock(side_effect=[thread_a, thread_b])  # type: ignore[method-assign]  # ty:ignore[invalid-assignment]
-
+        node = self._Node([thread_a, thread_b])
         with patch('liblore.node.time.sleep') as mock_sleep:
             results = node.batch_get_thread_by_msgid(['a@x', 'b@x'])
 
         assert results == [thread_a, thread_b]
-        assert node.get_thread_by_msgid.call_count == 2  # ty:ignore[unresolved-attribute]
+        assert len(node.calls) == 2
         mock_sleep.assert_called_once_with(0.1)
 
     def test_no_sleep_for_single_msgid(self) -> None:
-        node = LoreNode()
         thread = [EmailMessage()]
-        node.get_thread_by_msgid = MagicMock(return_value=thread)  # type: ignore[method-assign]  # ty:ignore[invalid-assignment]
-
+        node = self._Node([thread])
         with patch('liblore.node.time.sleep') as mock_sleep:
             results = node.batch_get_thread_by_msgid(['only@x'])
 
@@ -521,9 +536,7 @@ class TestBatchGetThreadByMsgid:
         mock_sleep.assert_not_called()
 
     def test_passes_kwargs(self) -> None:
-        node = LoreNode()
-        node.get_thread_by_msgid = MagicMock(return_value=[EmailMessage()])  # type: ignore[method-assign]  # ty:ignore[invalid-assignment]
-
+        node = self._Node([[EmailMessage()]])
         with patch('liblore.node.time.sleep'):
             node.batch_get_thread_by_msgid(
                 ['a@x'],
@@ -532,32 +545,23 @@ class TestBatchGetThreadByMsgid:
                 since='20240101',
             )
 
-        node.get_thread_by_msgid.assert_called_once_with(  # ty:ignore[unresolved-attribute]
-            'a@x',
-            strict=False,
-            sort=True,
-            since='20240101',
-        )
+        assert node.calls == [('a@x', False, True, '20240101')]
 
     def test_sleep_count_matches_gaps(self) -> None:
-        node = LoreNode()
-        node.get_thread_by_msgid = MagicMock(return_value=[EmailMessage()])  # type: ignore[method-assign]  # ty:ignore[invalid-assignment]
-
+        node = self._Node([[EmailMessage()], [EmailMessage()], [EmailMessage()]])
         with patch('liblore.node.time.sleep') as mock_sleep:
             node.batch_get_thread_by_msgid(['a@x', 'b@x', 'c@x'])
 
         assert mock_sleep.call_args_list == [call(0.1), call(0.1)]
 
     def test_empty_list(self) -> None:
-        node = LoreNode()
-        node.get_thread_by_msgid = MagicMock()  # type: ignore[method-assign]  # ty:ignore[invalid-assignment]
-
+        node = self._Node([])
         with patch('liblore.node.time.sleep') as mock_sleep:
             results = node.batch_get_thread_by_msgid([])
 
         assert results == []
         mock_sleep.assert_not_called()
-        node.get_thread_by_msgid.assert_not_called()  # ty:ignore[unresolved-attribute]
+        assert node.calls == []
 
 
 # =====================================================================
@@ -566,24 +570,36 @@ class TestBatchGetThreadByMsgid:
 
 
 class TestBatchGetThreadByQuery:
+    class _Node(LoreNode):
+        def __init__(self, results: list[list[EmailMessage]]) -> None:
+            super().__init__()
+            self.calls: list[tuple[str, bool]] = []
+            self._results = results
+
+        @override
+        def get_thread_by_query(
+            self,
+            query: str,
+            *,
+            full_threads: bool = False,
+        ) -> list[EmailMessage]:
+            self.calls.append((query, full_threads))
+            return self._results.pop(0)
+
     def test_returns_ordered_results(self) -> None:
-        node = LoreNode()
         result_a = [EmailMessage()]
         result_b = [EmailMessage(), EmailMessage()]
-        node.get_thread_by_query = MagicMock(side_effect=[result_a, result_b])  # type: ignore[method-assign]  # ty:ignore[invalid-assignment]
-
+        node = self._Node([result_a, result_b])
         with patch('liblore.node.time.sleep') as mock_sleep:
             results = node.batch_get_thread_by_query(['q1', 'q2'])
 
         assert results == [result_a, result_b]
-        assert node.get_thread_by_query.call_count == 2  # ty:ignore[unresolved-attribute]
+        assert len(node.calls) == 2
         mock_sleep.assert_called_once_with(0.1)
 
     def test_no_sleep_for_single_query(self) -> None:
-        node = LoreNode()
         result = [EmailMessage()]
-        node.get_thread_by_query = MagicMock(return_value=result)  # type: ignore[method-assign]  # ty:ignore[invalid-assignment]
-
+        node = self._Node([result])
         with patch('liblore.node.time.sleep') as mock_sleep:
             results = node.batch_get_thread_by_query(['only_query'])
 
@@ -591,24 +607,22 @@ class TestBatchGetThreadByQuery:
         mock_sleep.assert_not_called()
 
     def test_sleep_count_matches_gaps(self) -> None:
-        node = LoreNode()
-        node.get_thread_by_query = MagicMock(return_value=[EmailMessage()])  # type: ignore[method-assign]  # ty:ignore[invalid-assignment]
-
+        node = self._Node(
+            [[EmailMessage()], [EmailMessage()], [EmailMessage()], [EmailMessage()]]
+        )
         with patch('liblore.node.time.sleep') as mock_sleep:
             node.batch_get_thread_by_query(['q1', 'q2', 'q3', 'q4'])
 
         assert mock_sleep.call_args_list == [call(0.1), call(0.1), call(0.1)]
 
     def test_empty_list(self) -> None:
-        node = LoreNode()
-        node.get_thread_by_query = MagicMock()  # type: ignore[method-assign]  # ty:ignore[invalid-assignment]
-
+        node = self._Node([])
         with patch('liblore.node.time.sleep') as mock_sleep:
             results = node.batch_get_thread_by_query([])
 
         assert results == []
         mock_sleep.assert_not_called()
-        node.get_thread_by_query.assert_not_called()  # ty:ignore[unresolved-attribute]
+        assert node.calls == []
 
 
 # =====================================================================
Signature · 1 line
2.53.0
#12 lore raw

PATCH v2 12/14 Use CompletedProcess in git config tests

In reply to Tamir Duberstein

Replace MagicMock subprocess results with real subprocess.CompletedProcess values in the git config helper tests. This keeps the behavior the same while giving the mocks a concrete, type-checked shape.

---
 tests/test_node.py | 77 +++++++++++++++++++++++++++++++-----------------------
 1 file changed, 45 insertions(+), 32 deletions(-)
tests/test_node.py +45 -32
index 73c79a3..89be909 100644
@@ -5,6 +5,7 @@ from __future__ import annotations
 
 import gzip
 import os
+import subprocess
 from datetime import datetime, timezone
 from email.message import EmailMessage
 from unittest.mock import MagicMock, call, patch
@@ -1557,15 +1558,17 @@ class TestGetConfigFromGit:
         """Parses git config -z output correctly."""
         from liblore.node import _get_config_from_git
 
-        mock_result = MagicMock()
-        mock_result.returncode = 0
-        mock_result.stdout = (
-            'lore.fallback\nhttps://tor.lore.kernel.org\x00'
-            'lore.fallback\nhttps://sea.lore.kernel.org\x00'
-            'lore.autoprobe\ntrue\x00'
-            'lore.probettl\n7200\x00'
+        result = subprocess.CompletedProcess[str](
+            args=['git'],
+            returncode=0,
+            stdout=(
+                'lore.fallback\nhttps://tor.lore.kernel.org\x00'
+                'lore.fallback\nhttps://sea.lore.kernel.org\x00'
+                'lore.autoprobe\ntrue\x00'
+                'lore.probettl\n7200\x00'
+            ),
         )
-        with patch('liblore.node.subprocess.run', return_value=mock_result):
+        with patch('liblore.node.subprocess.run', return_value=result):
             cfg = _get_config_from_git(r'^lore\.', multivals=['fallback'])
 
         assert cfg == {
@@ -1593,10 +1596,12 @@ class TestGetConfigFromGit:
         """Returns empty dict when no keys match the regexp."""
         from liblore.node import _get_config_from_git
 
-        mock_result = MagicMock()
-        mock_result.returncode = 1
-        mock_result.stdout = ''
-        with patch('liblore.node.subprocess.run', return_value=mock_result):
+        result = subprocess.CompletedProcess[str](
+            args=['git'],
+            returncode=1,
+            stdout='',
+        )
+        with patch('liblore.node.subprocess.run', return_value=result):
             cfg = _get_config_from_git(r'^lore\.')
 
         assert cfg == {}
@@ -1605,10 +1610,12 @@ class TestGetConfigFromGit:
         """A key without a value (no newline) defaults to 'true'."""
         from liblore.node import _get_config_from_git
 
-        mock_result = MagicMock()
-        mock_result.returncode = 0
-        mock_result.stdout = 'lore.autoprobe\x00'
-        with patch('liblore.node.subprocess.run', return_value=mock_result):
+        result = subprocess.CompletedProcess[str](
+            args=['git'],
+            returncode=0,
+            stdout='lore.autoprobe\x00',
+        )
+        with patch('liblore.node.subprocess.run', return_value=result):
             cfg = _get_config_from_git(r'^lore\.')
 
         assert cfg == {'autoprobe': 'true'}
@@ -1621,15 +1628,17 @@ class TestGetSubsectionConfig:
         """Parses keys from [liblore "https://lore.kernel.org"]."""
         from liblore.node import _get_subsection_config
 
-        mock_result = MagicMock()
-        mock_result.returncode = 0
-        mock_result.stdout = (
-            'liblore.https://lore.kernel.org.fallback\nhttps://tor.lore.kernel.org\x00'
-            'liblore.https://lore.kernel.org.fallback\nhttps://sea.lore.kernel.org\x00'
-            'liblore.https://lore.kernel.org.autoprobe\ntrue\x00'
-            'liblore.https://lore.kernel.org.useragentplus\nmyuuid\x00'
+        result = subprocess.CompletedProcess[str](
+            args=['git'],
+            returncode=0,
+            stdout=(
+                'liblore.https://lore.kernel.org.fallback\nhttps://tor.lore.kernel.org\x00'
+                'liblore.https://lore.kernel.org.fallback\nhttps://sea.lore.kernel.org\x00'
+                'liblore.https://lore.kernel.org.autoprobe\ntrue\x00'
+                'liblore.https://lore.kernel.org.useragentplus\nmyuuid\x00'
+            ),
         )
-        with patch('liblore.node.subprocess.run', return_value=mock_result):
+        with patch('liblore.node.subprocess.run', return_value=result):
             cfg = _get_subsection_config(
                 'liblore',
                 'https://lore.kernel.org',
@@ -1649,10 +1658,12 @@ class TestGetSubsectionConfig:
         """Subsection names with dots (URLs) are parsed correctly."""
         from liblore.node import _get_subsection_config
 
-        mock_result = MagicMock()
-        mock_result.returncode = 0
-        mock_result.stdout = 'liblore.https://subspace.kernel.org.fallback\nhttps://mirror.example.com\x00'
-        with patch('liblore.node.subprocess.run', return_value=mock_result):
+        result = subprocess.CompletedProcess[str](
+            args=['git'],
+            returncode=0,
+            stdout='liblore.https://subspace.kernel.org.fallback\nhttps://mirror.example.com\x00',
+        )
+        with patch('liblore.node.subprocess.run', return_value=result):
             cfg = _get_subsection_config(
                 'liblore',
                 'https://subspace.kernel.org',
@@ -1667,10 +1678,12 @@ class TestGetSubsectionConfig:
         """Returns empty dict when no keys match the subsection."""
         from liblore.node import _get_subsection_config
 
-        mock_result = MagicMock()
-        mock_result.returncode = 1
-        mock_result.stdout = ''
-        with patch('liblore.node.subprocess.run', return_value=mock_result):
+        result = subprocess.CompletedProcess[str](
+            args=['git'],
+            returncode=1,
+            stdout='',
+        )
+        with patch('liblore.node.subprocess.run', return_value=result):
             cfg = _get_subsection_config(
                 'liblore',
                 'https://nonexistent.example.com',
Signature · 1 line
2.53.0
#13 lore raw

PATCH v2 13/14 Update README for uv-based dev checks

In reply to Tamir Duberstein

Document the development workflow in terms of uv and mention pyright, ty check, and ruff format.

---
 README.md | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
README.md +7 -4
index 3709d7e..1c3863e 100644
@@ -613,25 +613,28 @@ except LibloreError:
 Install with development dependencies:
 
 ```shell
-pip install -e ".[dev]"
+uv sync --all-extras --all-groups
 ```
 
 Run the test suite:
 
 ```shell
-pytest
+uv run --all-extras --all-groups pytest
 ```
 
 Type checking:
 
 ```shell
-mypy src/liblore/ --strict
+uv run --all-extras --all-groups ty check
+uv run --all-extras --all-groups mypy .
+uv run --all-extras --all-groups pyright
 ```
 
 Linting:
 
 ```shell
-ruff check src/liblore/
+uv run --all-extras --all-groups ruff format --check
+uv run --all-extras --all-groups ruff check
 ```
 
 ## Bug Reports
Signature · 1 line
2.53.0
#14 lore raw

PATCH v2 14/14 Add b4 send configuration

In reply to Tamir Duberstein

Populate the repository b4 config with the lore masks and default send settings used for kernel.org submissions.

---
 .b4-config | 7 +++++++
 1 file changed, 7 insertions(+)
.b4-config new file +7 -0
new file mode 100644
index 0000000..5084661
@@ -0,0 +1,7 @@
+[b4]
+  midmask = https://lore.kernel.org/%s
+  linkmask = https://patch.msgid.link/%s
+  send-series-to = Kernel.org Tools <[email protected]>
+  send-series-cc = Konstantin Ryabitsev <[email protected]>
+  send-endpoint-web = https://lkml.kernel.org/_b4_submit
+  send-prefixes = liblore
Signature · 1 line
2.53.0
#15 lore raw
In reply to Tamir Duberstein
Quoted 6 lines · Tamir Duberstein

Harden local type checking and test mocking

This series raises the local quality floor by making the type-checking tooling stricter and by removing test patterns that rely on unsafe or poorly typed mocks.

Quoted 8 lines

It starts by wiring the repository into b4 CI checks, then adds ruff, pyright, and ty so the same checks run consistently in one place. After that, it removes suppressions incrementally by making the code and tests more explicit and better typed: typed keyword forwarding in from_git_config, a typed authheaders integration, and test-local subclasses instead of method reassignment.

[...]

Applied, thanks!

[01/14] Add ci script and mypy suppressions

        https://git.kernel.org/pub/scm/utils/liblore/liblore.git/commit/?id=828e718aa0a4
[02/14] Type make_msg and drop test suppressions
        https://git.kernel.org/pub/scm/utils/liblore/liblore.git/commit/?id=b2981347f362
[03/14] Add ruff import checks to b4 CI
        https://git.kernel.org/pub/scm/utils/liblore/liblore.git/commit/?id=ac6e2df890e8
[04/14] Add ruff format check to CI
        https://git.kernel.org/pub/scm/utils/liblore/liblore.git/commit/?id=100116fefeef
[05/14] Add pyright strict checks to CI
        https://git.kernel.org/pub/scm/utils/liblore/liblore.git/commit/?id=6109a737d052
[06/14] Replace HTTP session mocks with responses
        https://git.kernel.org/pub/scm/utils/liblore/liblore.git/commit/?id=5445b53cd074
[07/14] Add ty checks to CI
        https://git.kernel.org/pub/scm/utils/liblore/liblore.git/commit/?id=7cd032b465fc
[08/14] Drop redundant read-only property test
        https://git.kernel.org/pub/scm/utils/liblore/liblore.git/commit/?id=53041a41e451
[09/14] Type from_git_config keyword arguments
        https://git.kernel.org/pub/scm/utils/liblore/liblore.git/commit/?id=18603217b450
[10/14] Add authheaders stub and typed callable
        https://git.kernel.org/pub/scm/utils/liblore/liblore.git/commit/?id=c9e8cf46e133
[11/14] Replace batch mocks with subclasses
        https://git.kernel.org/pub/scm/utils/liblore/liblore.git/commit/?id=bdca5ed2752e
[12/14] Use CompletedProcess in git config tests
        https://git.kernel.org/pub/scm/utils/liblore/liblore.git/commit/?id=f993dde25931
[13/14] Update README for uv-based dev checks
        https://git.kernel.org/pub/scm/utils/liblore/liblore.git/commit/?id=204122fd6ad6

[14/14] Add b4 send configuration
https://git.kernel.org/pub/scm/utils/liblore/liblore.git/commit/?id=9ba4ddb7e4aa

Best regards,

Signature · 1 line
KR
#16 lore raw
Quoted 16 lines · Konstantin Ryabitsev
Quoted 13 lines · Tamir Duberstein

Harden local type checking and test mocking

This series raises the local quality floor by making the type-checking tooling stricter and by removing test patterns that rely on unsafe or poorly typed mocks.

It starts by wiring the repository into b4 CI checks, then adds ruff, pyright, and ty so the same checks run consistently in one place. After that, it removes suppressions incrementally by making the code and tests more explicit and better typed: typed keyword forwarding in from_git_config, a typed authheaders integration, and test-local subclasses instead of method reassignment.

Quoted 3 lines
Quoted 1 line

[...]

Applied, thanks!

Thanks! Should I revive the b4 series, or wait until you've bumped the liblore pointer there?

#17 lore raw
Quoted 2 lines · Tamir Duberstein

Thanks! Should I revive the b4 series, or wait until you've bumped the liblore pointer there?

You don't have to wait on that -- the master should be quiet for the next few days as I'm focusing more on infra work, so now is a good time to do a big linter change.

Thanks!

Signature · 1 line
KR