c12d5b6d82
Phase 6: Eliminate Optional[T] returns (FR5) - BATCH 1 of 7
Before: 8 Optional[T] return types across 4 files
After: 0 (replaced with default-zero return values)
Delta: -8 sites
Per conductor/code_styleguides/error_handling.md "Optional[X] ban":
- "Use Result[T] for any function that can fail at runtime."
- "Use nil-sentinel dataclasses for 'no result'."
For accessor-style returns (lookup or zero-default), convert to:
- Optional[str] -> str with default "" (empty string sentinel)
- Optional[float] -> float with default 0.0
- Optional[int] -> int with default 0
- Optional[Path] -> Path with default Path("") or project_root
Specific changes:
- src/models.py:765-789: Persona.provider/model/temperature/top_p/max_output_tokens
(Optional[str]/[float]/[int] -> str/float/int with default zero values)
- src/paths.py:255: _get_project_conductor_dir_from_toml returns project_root
when no [conductor].dir override is configured (was Optional[Path] returning None)
- src/presets.py:21: project_path property returns Path("") when no project_root
(was Optional[Path] returning None)
- src/summary_cache.py:57: get_summary returns "" when hash mismatch (was
Optional[str] returning None)
Test updates:
- tests/test_persona_models.py:64-69: test_persona_defaults now expects
"" / 0.0 instead of None
- tests/test_summary_cache.py:25, 32, 58: get_summary assertions now
expect "" instead of None
Verification:
- audit_weak_types --strict: OK (107 <= 112 baseline)
- 13 tests pass (test_summary_cache, test_paths, test_presets,
test_persona_models)
- py_check_syntax: OK on all changed files
REMAINING: ~22 Optional[T] returns in:
- src/command_palette.py (1)
- src/diff_viewer.py (2)
- src/external_editor.py (3)
- src/file_cache.py (7)
- src/fuzzy_anchor.py (1)
- src/models.py (1)
- src/multi_agent_conductor.py (1)
- src/patch_modal.py (1)
- src/project_manager.py (1)
- src/session_logger.py (1)
- src/app_controller.py (3)
67 lines
2.1 KiB
Python
67 lines
2.1 KiB
Python
import os
|
|
import shutil
|
|
from pathlib import Path
|
|
from src.summary_cache import SummaryCache, get_file_hash
|
|
|
|
def test_get_file_hash():
|
|
content = "hello world"
|
|
# sha256 of "hello world"
|
|
expected = "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9"
|
|
assert get_file_hash(content) == expected
|
|
|
|
def test_summary_cache(tmp_path):
|
|
# v3 paths.py: use tmp_path (which is under ./tests/) instead of
|
|
# hardcoded project-root paths that the FR1 guard blocks.
|
|
cache_file = tmp_path / "cache.json"
|
|
|
|
cache = SummaryCache(str(cache_file))
|
|
|
|
file_path = "test.py"
|
|
content = "print('hello')"
|
|
content_hash = get_file_hash(content)
|
|
summary = "**Python** - 1 lines"
|
|
|
|
# Test empty cache
|
|
assert cache.get_summary(file_path, content_hash) == ""
|
|
|
|
# Test set and get
|
|
cache.set_summary(file_path, content_hash, summary)
|
|
assert cache.get_summary(file_path, content_hash) == summary
|
|
|
|
# Test cache invalidation
|
|
assert cache.get_summary(file_path, "different_hash") == ""
|
|
|
|
# Test persistence
|
|
cache2 = SummaryCache(str(cache_file))
|
|
assert cache2.get_summary(file_path, content_hash) == summary
|
|
|
|
|
|
def test_summary_cache_lru(tmp_path):
|
|
# v3 paths.py: use tmp_path instead of hardcoded project-root paths.
|
|
cache_file = tmp_path / "cache.json"
|
|
|
|
# Create cache with max 2 entries
|
|
cache = SummaryCache(str(cache_file), max_entries=2)
|
|
|
|
cache.set_summary("file1.py", "hash1", "summary1")
|
|
cache.set_summary("file2.py", "hash2", "summary2")
|
|
cache.set_summary("file3.py", "hash3", "summary3") # This should evict file1.py
|
|
|
|
assert cache.get_summary("file1.py", "hash1") == ""
|
|
assert cache.get_summary("file2.py", "hash2") == "summary2"
|
|
assert cache.get_summary("file3.py", "hash3") == "summary3"
|
|
|
|
# Access file2.py, then add file4.py. file3.py should be evicted
|
|
cache.get_summary("file2.py", "hash2")
|
|
cache.set_summary("file4.py", "hash4", "summary4")
|
|
|
|
assert cache.get_summary("file3.py", "hash3") == ""
|
|
assert cache.get_summary("file2.py", "hash2") == "summary2"
|
|
assert cache.get_summary("file4.py", "hash4") == "summary4"
|
|
|
|
if __name__ == "__main__":
|
|
test_get_file_hash()
|
|
test_summary_cache()
|
|
test_summary_cache_lru()
|
|
print("Tests passed!")
|