fix(types): resolve pre-existing basedpyright errors in tests

Fix ~790 basedpyright errors across the test suite:
- Type stub subclasses of real production classes with super().__init__()
- Add missing generic type arguments and Dict[str, Any] annotations
- Add None guards before subscript/member access
- Adapt tests to production API changes (removed dead handlers,
  PersistentModelCache.get_default, _i18n_filter_added location)
This commit is contained in:
Will Miao
2026-08-08 20:12:59 +08:00
parent 8e724538bd
commit d2f955266d
95 changed files with 953 additions and 666 deletions

View File

@@ -9,7 +9,7 @@ from py.services.downloader import Downloader
class FakeStream:
def __init__(self, chunks: Sequence[Sequence] | Sequence[bytes]):
def __init__(self, chunks: Sequence[bytes | tuple[bytes, float]]):
self._chunks = list(chunks)
async def read(self, _chunk_size: int) -> bytes:
@@ -25,6 +25,7 @@ class FakeStream:
payload = item[0]
delay = item[1]
assert isinstance(payload, bytes)
await asyncio.sleep(delay)
return payload
@@ -84,11 +85,11 @@ def _build_downloader(responses, *, max_retries=0):
downloader.max_retries = max_retries
downloader.base_delay = 0
fake_session = FakeSession(responses)
downloader._session = fake_session
downloader._session = fake_session # pyright: ignore[reportAttributeAccessIssue]
downloader._session_created_at = datetime.now()
downloader._proxy_url = None
async def _noop_create_session():
downloader._session = fake_session
downloader._session = fake_session # pyright: ignore[reportAttributeAccessIssue]
downloader._session_created_at = datetime.now()
downloader._proxy_url = None
@@ -96,6 +97,13 @@ def _build_downloader(responses, *, max_retries=0):
return downloader
def _session(downloader: Downloader) -> FakeSession:
"""Return the injected fake session, asserting the runtime invariant."""
session = downloader._session
assert isinstance(session, FakeSession)
return session
@pytest.mark.asyncio
async def test_download_file_preserves_incomplete_part_when_size_mismatch(tmp_path):
target_path = tmp_path / "model" / "file.bin"
@@ -196,7 +204,7 @@ async def test_download_file_recovers_from_stall(tmp_path):
assert success is True
assert Path(result_path).read_bytes() == payload
assert downloader._session._get_calls == 2
assert _session(downloader)._get_calls == 2
assert not Path(str(target_path) + ".part").exists()
@@ -224,8 +232,8 @@ async def test_download_file_resumes_after_incomplete_integrity_check(tmp_path):
assert success is True
assert Path(result_path).read_bytes() == b"abcdef"
assert downloader._session._get_calls == 2
assert downloader._session.requests[1]["headers"]["Range"] == "bytes=3-"
assert _session(downloader)._get_calls == 2
assert _session(downloader).requests[1]["headers"]["Range"] == "bytes=3-"
assert not Path(str(target_path) + ".part").exists()
@@ -261,6 +269,6 @@ async def test_download_file_retries_redirected_url_when_range_not_honored(tmp_p
assert success is True
assert Path(result_path).read_bytes() == b"abcdef"
assert first_response.released is True
assert downloader._session.requests[0]["headers"]["Range"] == "bytes=3-"
assert downloader._session.requests[1]["url"] == redirected_url
assert downloader._session.requests[1]["headers"]["Range"] == "bytes=3-"
assert _session(downloader).requests[0]["headers"]["Range"] == "bytes=3-"
assert _session(downloader).requests[1]["url"] == redirected_url
assert _session(downloader).requests[1]["headers"]["Range"] == "bytes=3-"