Add full type annotations and black formatting across all modules
Annotated all functions with mypy --strict-compatible types (-> None, dict[str, Any], Generator types, etc.), added # type: ignore for untyped third-party libs (lxml), and reformatted with black. All 18 source files now pass mypy --strict with zero errors. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
453d6244ec
commit
13c4341f3a
14 changed files with 1802 additions and 974 deletions
|
|
@ -1,49 +1,53 @@
|
|||
import os
|
||||
import time
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
import pytest
|
||||
from cache import get_cached, set_cached
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def tmp_cache(tmp_path, monkeypatch):
|
||||
def tmp_cache(tmp_path: Path, monkeypatch: Any) -> Path:
|
||||
import cache as cache_module
|
||||
monkeypatch.setattr(cache_module, 'CACHE_DIR', str(tmp_path))
|
||||
|
||||
monkeypatch.setattr(cache_module, "CACHE_DIR", str(tmp_path))
|
||||
return tmp_path
|
||||
|
||||
|
||||
def test_get_cached_returns_none_for_missing_key(tmp_cache):
|
||||
assert get_cached('no_such_key') is None
|
||||
def test_get_cached_returns_none_for_missing_key(tmp_cache: Path) -> None:
|
||||
assert get_cached("no_such_key") is None
|
||||
|
||||
|
||||
def test_set_and_get_cached_roundtrip(tmp_cache):
|
||||
set_cached('my_key', {'a': 1})
|
||||
assert get_cached('my_key') == {'a': 1}
|
||||
def test_set_and_get_cached_roundtrip(tmp_cache: Path) -> None:
|
||||
set_cached("my_key", {"a": 1})
|
||||
assert get_cached("my_key") == {"a": 1}
|
||||
|
||||
|
||||
def test_get_cached_no_ttl_never_expires(tmp_cache):
|
||||
set_cached('k', [1, 2, 3])
|
||||
def test_get_cached_no_ttl_never_expires(tmp_cache: Path) -> None:
|
||||
set_cached("k", [1, 2, 3])
|
||||
# Backdate the file by 2 days
|
||||
path = tmp_cache / 'k.json'
|
||||
path = tmp_cache / "k.json"
|
||||
old = time.time() - 2 * 86400
|
||||
os.utime(path, (old, old))
|
||||
assert get_cached('k') == [1, 2, 3]
|
||||
assert get_cached("k") == [1, 2, 3]
|
||||
|
||||
|
||||
def test_get_cached_within_ttl(tmp_cache):
|
||||
set_cached('k', 'fresh')
|
||||
assert get_cached('k', ttl=3600) == 'fresh'
|
||||
def test_get_cached_within_ttl(tmp_cache: Path) -> None:
|
||||
set_cached("k", "fresh")
|
||||
assert get_cached("k", ttl=3600) == "fresh"
|
||||
|
||||
|
||||
def test_get_cached_expired_returns_none(tmp_cache):
|
||||
set_cached('k', 'stale')
|
||||
path = tmp_cache / 'k.json'
|
||||
def test_get_cached_expired_returns_none(tmp_cache: Path) -> None:
|
||||
set_cached("k", "stale")
|
||||
path = tmp_cache / "k.json"
|
||||
old = time.time() - 25 * 3600 # 25 hours ago
|
||||
os.utime(path, (old, old))
|
||||
assert get_cached('k', ttl=24 * 3600) is None
|
||||
assert get_cached("k", ttl=24 * 3600) is None
|
||||
|
||||
|
||||
def test_get_cached_invalid_json_returns_none(tmp_cache):
|
||||
path = tmp_cache / 'broken.json'
|
||||
def test_get_cached_invalid_json_returns_none(tmp_cache: Path) -> None:
|
||||
path = tmp_cache / "broken.json"
|
||||
path.write_text('{"not": "finished"')
|
||||
|
||||
assert get_cached('broken') is None
|
||||
assert get_cached("broken") is None
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue