-
Notifications
You must be signed in to change notification settings - Fork 58
fix: recover from cache corruption #80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
OliverSherouse
wants to merge
1
commit into
master
Choose a base branch
from
fix/cache-corruption-78
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,33 @@ | ||
| # Repository Guidelines | ||
|
|
||
| ## Project Structure & Module Organization | ||
| Source lives in `wbdata/` with clients, caching helpers, and API utilities; `wbdata/version.py` centralizes the library version and should be the single source when updating releases. Tests reside in `tests/` with `test_*.py` modules mirroring public APIs. Contributor-facing docs and MkDocs content sit in `docs/`, while packaging metadata and tooling configuration are in `pyproject.toml`. | ||
| ## Quickstart | ||
| - Run `make setup` to install all extras and dev tools. | ||
| - Run `make check` to execute format (check), lint, ty check, and tests. | ||
| - Open pull requests against `master`. | ||
| - Do not commit or push unless explicitly instructed. | ||
|
|
||
| ## Build, Test, and Development Commands | ||
| Install dependencies with `uv sync --all-extras --group dev` so the docs, pandas extras, and developer tooling are available. Use `uv run pytest` for the default suite and coverage, matching the `--cov=wbdata` addopts in configuration. Run `uv run ruff check wbdata tests` to lint, and `uv run mypy wbdata` for type validation. During documentation work, serve the site locally via `uv run mkdocs serve`. | ||
| ## Dev Loop | ||
| - Run `make format` (ruff format). | ||
| - Run `make lint` (ruff check wbdata tests). | ||
| - Run `make typecheck` (ty check wbdata). | ||
| - Run `make test` (pytest with coverage addopts from config). | ||
| - Preview docs with `uv run mkdocs serve`. | ||
|
|
||
| ## Coding Style & Naming Conventions | ||
| Follow standard Python formatting with four-space indentation and readable, snake_case symbols. Public APIs exposed in `wbdata/__init__.py` should maintain descriptive, lowercase names; classes stay in CapWords. Ruff enforces PEP 8, import sorting, and selected Bugbear/Simplify rules—run it before committing. Keep modules typed, updating `py.typed` coverage when adding packages, and prefer explicit re-exports in `__all__` blocks where applicable. | ||
| ## Project Layout | ||
| - Keep code in `wbdata/` (client, caching, API helpers); treat `wbdata/version.py` as the single source of version. | ||
| - Add tests in `tests/` with `test_*.py` mirroring public APIs. | ||
| - Maintain docs in `docs/` and `mkdocs.yml`; adjust packaging/tooling in `pyproject.toml`. | ||
|
|
||
| ## Testing Guidelines | ||
| Write new tests under `tests/` using `pytest` conventions (`test_feature.py`, functions starting with `test_`). When adding network-heavy scenarios, leverage fixtures to isolate HTTP calls. Keep coverage from `pytest-cov` stable by exercising new branches, and include regression cases that mirror reported issues. | ||
| ## Style | ||
| - Use Python 4-space indent, snake_case; CapWords for classes; re-export via `__all__` when needed. | ||
| - Run ruff (PEP8, imports, Bugbear/Simplify) before commits. | ||
| - Preserve typing coverage (`py.typed`); prefer explicit types. | ||
|
|
||
| ## Commit & Pull Request Guidelines | ||
| Aim for concise, imperative subjects, optionally prefixed with Conventional Commit types as seen in `git log` (e.g., `fix: improve caching`). Reference related issues or discussions with `(#123)` in the subject when merging via GitHub. Before opening a PR, ensure lint, typing, and tests pass, document user-facing changes, and provide a short summary plus reproduction or screenshots when behavior shifts. | ||
| ## Testing | ||
| - Add pytest cases under `tests/`; favor fixtures for network isolation. | ||
| - Maintain coverage by exercising new branches; include regression cases for reported bugs. | ||
|
|
||
| ## Documentation & Release Notes | ||
| Update `docs/` pages when modifying user workflows, and verify the navigation using the MkDocs preview. Release metadata lives in `wbdata/version.py`; bump it in sync with changelog entries and confirm that packaging files (`pyproject.toml`, `MANIFEST.in`) need no extra updates. | ||
| ## PR Expectations | ||
| - Use conventional, imperative titles (e.g., `fix: improve caching`). | ||
| - Ensure format/lint/type/tests pass; document user-facing changes; include repro or screenshots when behavior shifts. | ||
| - Bump `wbdata/version.py` alongside changelog/release notes when shipping releases. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| setup: | ||
| uv sync --all-extras --group dev | ||
| .PHONY: setup | ||
|
|
||
| format: | ||
| uv run ruff format wbdata tests docs | ||
| .PHONY: format | ||
|
|
||
| lint: | ||
| uv run ruff check wbdata tests | ||
| .PHONY: lint | ||
|
|
||
| typecheck: | ||
| uv run ty check wbdata | ||
OliverSherouse marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| .PHONY: typecheck | ||
|
|
||
| test: | ||
| uv run pytest | ||
| .PHONY: test | ||
|
|
||
| check: format lint typecheck test | ||
| .PHONY: check | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| import wbdata.cache as cache | ||
|
|
||
|
|
||
| def test_get_cache_recovers_from_corruption(tmp_path, monkeypatch): | ||
| cache_path = tmp_path / "cachefile" | ||
| cache_path.write_bytes(b"corrupt") | ||
|
|
||
| call_count = {"value": 0} | ||
|
|
||
| class FakeCache: | ||
| def expire(self): | ||
| call_count["value"] += 1 | ||
| if call_count["value"] == 1: | ||
| raise SystemError("boom") | ||
|
|
||
| monkeypatch.setattr( | ||
| cache.shelved_cache, "PersistentCache", lambda *args, **kwargs: FakeCache() | ||
| ) | ||
|
|
||
| result = cache.get_cache(path=cache_path, ttl_days=1, max_size=1) | ||
|
|
||
| assert isinstance(result, FakeCache) | ||
| assert call_count["value"] == 2 # retried after clearing corruption | ||
| assert not cache_path.exists() | ||
|
|
||
|
|
||
| def test_clear_cache_files_removes_shelve_variants(tmp_path): | ||
| base = tmp_path / "cachefile" | ||
| variants = [ | ||
| base, | ||
| base.with_suffix(".db"), | ||
| base.with_suffix(".dat"), | ||
| base.with_suffix(".dir"), | ||
| base.with_suffix(".bak"), | ||
| tmp_path / "cachefile.extra", | ||
| ] | ||
| for path in variants: | ||
| if path.suffix == ".dir": | ||
| path.mkdir() | ||
| else: | ||
| path.write_text("x") | ||
|
|
||
| cache._clear_cache_files(base) | ||
|
|
||
| for path in variants: | ||
| assert not path.exists() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| __version__ = "1.1.0" | ||
| __version__ = "1.1.1" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.