Skip to content

Refactor: Improve error handling and robustness #50

Description

@shimpeiws

Summary

Several areas of the codebase can benefit from improved type safety and robustness.

Items

1. Type-safe `_retry_request`

In genius/client.py, _retry_request accepts request_fn as Any and returns Any. This should use generics:

```python
from typing import TypeVar, Callable
T = TypeVar("T")

def _retry_request(self, request_fn: Callable[[], T], retries: int | None = None) -> T:
```

2. Cache race condition handling

cache.py uses file I/O for get_lyrics() and store_lyrics(). If lyrics fetching is parallelized, concurrent access to the same song's cache file could cause race conditions.

Proposed fix: Use atomic writes (write to temp file then rename) or adopt the filelock library.

3. Simplify `_get_cache_path` hashing

cache.py uses hashlib.md5(str(song_id).encode()).hexdigest()[:2] for directory distribution, but since song_id is an integer, a simple song_id % 256 would suffice without the MD5 overhead.

Affected Files

  • src/barscan/genius/client.py
  • src/barscan/genius/cache.py

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions