Skip to content

Commit de8de84

Browse files
committed
Remove mypy usage and add size util
1 parent b321a17 commit de8de84

14 files changed

Lines changed: 44 additions & 65 deletions

File tree

.github/copilot-instructions.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Welcome to the Cachier codebase! Please follow these guidelines to ensure code s
2626

2727
## 4. Coverage, Linting, and Typing
2828

29-
- Code must pass `mypy`, `ruff`, and `pytest`.
29+
- Code must pass `ruff` and `pytest`.
3030
- Use per-file or per-line ignores for known, justified issues (e.g., SQLAlchemy model base class typing, intentional use of `pickle`).
3131
- All new code must include full type annotations and docstrings matching the style of the existing codebase.
3232
- All docstrings should follow numpy docstring conventions.

.pre-commit-config.yaml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,6 @@ repos:
6565
name: Ruff check
6666
args: ["--fix"]
6767

68-
# it needs to be after formatting hooks because the lines might be changed
69-
- repo: https://github.com/pre-commit/mirrors-mypy
70-
rev: v1.16.1
71-
hooks:
72-
- id: mypy
73-
files: "src/*"
7468

7569
- repo: https://github.com/tox-dev/pyproject-fmt
7670
rev: v2.6.0

AGENTS.md

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
- **Key Dependencies:** `portalocker`, `watchdog` (optional: `pymongo`, `sqlalchemy`, `redis`)
1010
- **Test Framework:** `pytest` with backend-specific markers
1111
- **Linting:** `ruff` (replaces black/flake8)
12-
- **Type Checking:** `mypy`
1312
- **CI:** GitHub Actions (matrix for backends/OS with Dockerized services)
1413
- **Issue Tracking:** GitHub Issues
1514
- **Additional Docs:** `.github/copilot-instructions.md` for contributor guidelines
@@ -65,11 +64,10 @@ ______________________________________________________________________
6564
pytest -m "not (mongo or sql)" # Exclude external service backends
6665
```
6766

68-
3. **Lint and type-check:**
67+
3. **Lint:**
6968

7069
```bash
7170
ruff check .
72-
mypy src/cachier/
7371
```
7472

7573
4. **Try an example:**
@@ -102,7 +100,6 @@ ______________________________________________________________________
102100
- **Type annotations** required for all new code.
103101
- **Docstrings:** Use numpy style, multi-line, no single-line docstrings.
104102
- **Lint:** Run `ruff` before PRs. Use per-line/file ignores only for justified cases.
105-
- **Type check:** Run `mypy` before PRs.
106103
- **Testing:** All public methods must have at least one test. Use `pytest.mark.<backend>` for backend-specific tests.
107104
- **No warnings/errors for missing optional dependencies at import time.** Only raise when backend is used.
108105

@@ -415,7 +412,6 @@ ______________________________________________________________________
415412
- **Run multiple backends:** `pytest -m "redis or sql"`
416413
- **Exclude backends:** `pytest -m "not mongo"`
417414
- **Lint:** `ruff check .`
418-
- **Type check:** `mypy src/cachier/`
419415
- **Format:** `ruff format .`
420416
- **Pre-commit:** `pre-commit run --all-files`
421417
- **Build package:** `python -m build`
@@ -505,7 +501,6 @@ ______________________________________________________________________
505501
- **If adding new dependencies, use context7 MCP to get latest versions.**
506502
- **Always check GitHub Issues before starting new features/PRs.**
507503
- **Create a relevant issue for every new PR.**
508-
- **Use per-file or per-line ignores for mypy/ruff only when justified.**
509504
- **All new code must have full type annotations and numpy-style docstrings.**
510505

511506
______________________________________________________________________
@@ -557,7 +552,6 @@ ______________________________________________________________________
557552
| Test multiple backends | `pytest -m "redis or sql"` |
558553
| Exclude backends | `pytest -m "not mongo"` |
559554
| Lint | `ruff check .` |
560-
| Type check | `mypy src/cachier/` |
561555
| Format code | `ruff format .` |
562556
| Build package | `python -m build` |
563557
| Check docs | `python setup.py checkdocs` |

CLAUDE.md

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
- **Key Dependencies:** `portalocker`, `watchdog` (optional: `pymongo`, `sqlalchemy`, `redis`)
1010
- **Test Framework:** `pytest` with backend-specific markers
1111
- **Linting:** `ruff` (replaces black/flake8)
12-
- **Type Checking:** `mypy`
1312
- **CI:** GitHub Actions (matrix for backends/OS with Dockerized services)
1413
- **Issue Tracking:** GitHub Issues
1514
- **Additional Docs:** `.github/copilot-instructions.md` for contributor guidelines
@@ -69,7 +68,6 @@ ______________________________________________________________________
6968

7069
```bash
7170
ruff check .
72-
mypy src/cachier/
7371
```
7472

7573
4. **Try an example:**
@@ -102,7 +100,6 @@ ______________________________________________________________________
102100
- **Type annotations** required for all new code.
103101
- **Docstrings:** Use numpy style, multi-line, no single-line docstrings.
104102
- **Lint:** Run `ruff` before PRs. Use per-line/file ignores only for justified cases.
105-
- **Type check:** Run `mypy` before PRs.
106103
- **Testing:** All public methods must have at least one test. Use `pytest.mark.<backend>` for backend-specific tests.
107104
- **No warnings/errors for missing optional dependencies at import time.** Only raise when backend is used.
108105

@@ -415,7 +412,6 @@ ______________________________________________________________________
415412
- **Run multiple backends:** `pytest -m "redis or sql"`
416413
- **Exclude backends:** `pytest -m "not mongo"`
417414
- **Lint:** `ruff check .`
418-
- **Type check:** `mypy src/cachier/`
419415
- **Format:** `ruff format .`
420416
- **Pre-commit:** `pre-commit run --all-files`
421417
- **Build package:** `python -m build`
@@ -505,7 +501,6 @@ ______________________________________________________________________
505501
- **If adding new dependencies, use context7 MCP to get latest versions.**
506502
- **Always check GitHub Issues before starting new features/PRs.**
507503
- **Create a relevant issue for every new PR.**
508-
- **Use per-file or per-line ignores for mypy/ruff only when justified.**
509504
- **All new code must have full type annotations and numpy-style docstrings.**
510505

511506
______________________________________________________________________
@@ -557,7 +552,6 @@ ______________________________________________________________________
557552
| Test multiple backends | `pytest -m "redis or sql"` |
558553
| Exclude backends | `pytest -m "not mongo"` |
559554
| Lint | `ruff check .` |
560-
| Type check | `mypy src/cachier/` |
561555
| Format code | `ruff format .` |
562556
| Build package | `python -m build` |
563557
| Check docs | `python setup.py checkdocs` |

Makefile

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
test-mongo-local test-mongo-docker test-mongo-inmemory test-mongo-also-local \
66
test-redis-local test-sql-local \
77
services-start services-stop services-logs \
8-
mongo-start mongo-stop mongo-logs lint type-check format clean \
8+
mongo-start mongo-stop mongo-logs lint format clean \
99
install install-dev install-all
1010

1111
# Default target
@@ -30,7 +30,6 @@ help:
3030
@echo ""
3131
@echo "Code Quality:"
3232
@echo " make lint - Run ruff linter"
33-
@echo " make type-check - Run mypy type checker"
3433
@echo " make format - Format code with ruff"
3534
@echo ""
3635
@echo "Installation:"
@@ -131,8 +130,6 @@ mongo-logs:
131130
lint:
132131
ruff check .
133132

134-
type-check:
135-
mypy src/cachier/
136133

137134
format:
138135
ruff format .
@@ -145,7 +142,6 @@ clean:
145142
rm -rf .coverage
146143
rm -rf htmlcov/
147144
rm -rf .pytest_cache/
148-
rm -rf .mypy_cache/
149145
rm -rf .ruff_cache/
150146
find . -type d -name __pycache__ -exec rm -rf {} + 2>/dev/null || true
151147
find . -type f -name "*.pyc" -delete

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ dynamic = [
4646
dependencies = [
4747
"portalocker>=2.3.2",
4848
"watchdog>=2.3.1",
49+
"pympler>=1.0",
4950
]
5051
urls.Source = "https://github.com/python-cachier/cachier"
5152
# --- setuptools ---
@@ -197,5 +198,5 @@ show_missing = true
197198
exclude_lines = [
198199
"pragma: no cover", # Have to re-enable the standard pragma
199200
"raise NotImplementedError", # Don't complain if tests don't hit defensive assertion code:
200-
"if TYPE_CHECKING:", # Is only true when running mypy, not tests
201+
"if TYPE_CHECKING:",
201202
]

src/cachier/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
enable_caching,
55
get_default_params,
66
get_global_params,
7-
parse_bytes,
87
set_default_params,
98
set_global_params,
109
)
1110
from .core import cachier
11+
from .util import parse_bytes
1212

1313
__all__ = [
1414
"cachier",

src/cachier/config.py

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import hashlib
22
import os
33
import pickle
4-
import re
54
import threading
65
from dataclasses import dataclass, field, replace
76
from datetime import datetime, timedelta
@@ -33,27 +32,6 @@ def _default_cache_dir():
3332
return os.path.expanduser("~/.cachier/")
3433

3534

36-
def parse_bytes(size: Union[int, str, None]) -> Optional[int]:
37-
"""Convert a human friendly size string to bytes."""
38-
if size is None:
39-
return None
40-
if isinstance(size, int):
41-
return size
42-
match = re.fullmatch(r"(?i)\s*(\d+(?:\.\d+)?)\s*([kmgt]?b)?\s*", str(size))
43-
if not match:
44-
raise ValueError(f"Invalid size value: {size}")
45-
number = float(match.group(1))
46-
unit = (match.group(2) or "b").upper()
47-
factor = {
48-
"B": 1,
49-
"KB": 1024,
50-
"MB": 1024**2,
51-
"GB": 1024**3,
52-
"TB": 1024**4,
53-
}[unit]
54-
return int(number * factor)
55-
56-
5735
class LazyCacheDir:
5836
"""Lazily resolve the default cache directory using $XDG_CACHE_HOME."""
5937

src/cachier/core.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,14 @@
1919
from warnings import warn
2020

2121
from ._types import RedisClient
22-
from .config import (
23-
Backend,
24-
HashFunc,
25-
Mongetter,
26-
_update_with_defaults,
27-
parse_bytes,
28-
)
22+
from .config import Backend, HashFunc, Mongetter, _update_with_defaults
2923
from .cores.base import RecalculationNeeded, _BaseCore
3024
from .cores.memory import _MemoryCore
3125
from .cores.mongo import _MongoCore
3226
from .cores.pickle import _PickleCore
3327
from .cores.redis import _RedisCore
3428
from .cores.sql import _SQLCore
29+
from .util import parse_bytes
3530

3631
MAX_WORKERS_ENVAR_NAME = "CACHIER_MAX_WORKERS"
3732
DEFAULT_MAX_WORKERS = 8

src/cachier/cores/base.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@
99

1010
import abc # for the _BaseCore abstract base class
1111
import inspect
12-
import pickle
1312
import sys
1413
import threading
1514
from datetime import timedelta
1615
from typing import Any, Callable, Optional, Tuple
1716

17+
from pympler import asizeof
18+
1819
from .._types import HashFunc
1920
from ..config import CacheEntry, _update_with_defaults
2021

@@ -96,7 +97,7 @@ def get_entry_by_key(self, key: str) -> Tuple[str, Optional[CacheEntry]]:
9697

9798
def _estimate_size(self, value: Any) -> int:
9899
try:
99-
return len(pickle.dumps(value))
100+
return asizeof.asizeof(value)
100101
except Exception:
101102
return sys.getsizeof(value)
102103

0 commit comments

Comments
 (0)