Skip to content

Commit f7cb87c

Browse files
committed
chore: some minor cleanup
- update readme - remote references to pylint - more exceptions to common instead of utils (there are more internal) - hide legacy 'experimental' stuff under TYPE_CHECKING
1 parent ff45108 commit f7cb87c

File tree

12 files changed

+49
-54
lines changed

12 files changed

+49
-54
lines changed

README.ipynb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@
189189
"outputs": [],
190190
"source": [
191191
"# FIXME hmm seems like this doesn't work if there are type annotations on cachew_impl? odd\n",
192+
"# likely this? https://github.com/davidhalter/jedi/issues/2025\n",
192193
"doc = getdoc('cachew_impl').split('Usage example:')[-1].lstrip()\n",
193194
"dmd(f\"\"\"```python\n",
194195
"{doc}\n",

README.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ Cachew gives the best of two worlds and makes it both **easy and efficient**. Th
128128
- first your objects get [converted](src/cachew/marshall/cachew.py#L29) into a simpler JSON-like representation
129129
- after that, they are mapped into byte blobs via [`orjson`](https://github.com/ijl/orjson).
130130

131-
When the function is called, cachew [computes the hash of your function's arguments ](src/cachew/__init__.py#L586)
131+
When the function is called, cachew [computes the hash of your function's arguments ](src/cachew/__init__.py#L580)
132132
and compares it against the previously stored hash value.
133133

134134
- If they match, it would deserialize and yield whatever is stored in the cache database
@@ -145,13 +145,13 @@ and compares it against the previously stored hash value.
145145

146146
* primitive: `str`, `int`, `float`, `bool`, `datetime`, `date`, `Exception`
147147

148-
See [tests.test_types](src/cachew/tests/test_cachew.py#L683), [tests.test_primitive](src/cachew/tests/test_cachew.py#L721), [tests.test_dates](src/cachew/tests/test_cachew.py#L633), [tests.test_exceptions](src/cachew/tests/test_cachew.py#L1125)
149-
* [@dataclass and NamedTuple](src/cachew/tests/test_cachew.py#L598)
150-
* [Optional](src/cachew/tests/test_cachew.py#L525) types
151-
* [Union](src/cachew/tests/test_cachew.py#L828) types
152-
* [nested datatypes](src/cachew/tests/test_cachew.py#L441)
148+
See [tests.test_types](src/cachew/tests/test_cachew.py#L682), [tests.test_primitive](src/cachew/tests/test_cachew.py#L720), [tests.test_dates](src/cachew/tests/test_cachew.py#L632), [tests.test_exceptions](src/cachew/tests/test_cachew.py#L1124)
149+
* [@dataclass and NamedTuple](src/cachew/tests/test_cachew.py#L597)
150+
* [Optional](src/cachew/tests/test_cachew.py#L524) types
151+
* [Union](src/cachew/tests/test_cachew.py#L827) types
152+
* [nested datatypes](src/cachew/tests/test_cachew.py#L440)
153153

154-
* detects [datatype schema changes](src/cachew/tests/test_cachew.py#L471) and discards old data automatically
154+
* detects [datatype schema changes](src/cachew/tests/test_cachew.py#L470) and discards old data automatically
155155

156156

157157
# Performance
@@ -165,12 +165,12 @@ You can find some of my performance tests in [benchmarks/](benchmarks) dir, and
165165

166166

167167
# Using
168-
See [docstring](src/cachew/__init__.py#L285) for up-to-date documentation on parameters and return types.
168+
See [docstring](src/cachew/__init__.py#L279) for up-to-date documentation on parameters and return types.
169169
You can also use [extensive unit tests](src/cachew/tests/test_cachew.py#L1) as a reference.
170170

171171
Some useful (but optional) arguments of `@cachew` decorator:
172172

173-
* `cache_path` can be a directory, or a callable that [returns a path](src/cachew/tests/test_cachew.py#L418) and depends on function's arguments.
173+
* `cache_path` can be a directory, or a callable that [returns a path](src/cachew/tests/test_cachew.py#L417) and depends on function's arguments.
174174

175175
By default, `settings.DEFAULT_CACHEW_DIR` is used.
176176

@@ -274,7 +274,7 @@ Now you can use `@mcachew` in place of `@cachew`, and be certain things don't br
274274
## Settings
275275

276276

277-
[cachew.settings](src/cachew/__init__.py#L59) exposes some parameters that allow you to control `cachew` behaviour:
277+
[cachew.settings](src/cachew/__init__.py#L55) exposes some parameters that allow you to control `cachew` behaviour:
278278
- `ENABLE`: set to `False` if you want to disable caching for without removing the decorators (useful for testing and debugging).
279279
You can also use [cachew.extra.disabled_cachew](src/cachew/extra.py#L25) context manager to do it temporarily.
280280
- `DEFAULT_CACHEW_DIR`: override to set a different base directory. The default is the "user cache directory" (see [platformdirs docs](https://github.com/tox-dev/platformdirs?tab=readme-ov-file#example-output)).

src/cachew/__init__.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,15 @@ def orjson_dumps(*args, **kwargs): # type: ignore[misc]
3939
from .backend.common import AbstractBackend
4040
from .backend.file import FileBackend
4141
from .backend.sqlite import SqliteBackend
42-
from .common import SourceHash
42+
from .common import CachewException, SourceHash, TypeNotSupported
4343
from .logging_helper import make_logger
4444
from .marshall.cachew import CachewMarshall, build_schema
45-
from .utils import (
46-
CachewException,
47-
TypeNotSupported,
48-
resolve_type_parameters,
49-
)
45+
from .utils import resolve_type_parameters
5046

5147
# in case of changes in the way cachew stores data, this should be changed to discard old caches
5248
CACHEW_VERSION: str = importlib.metadata.version(__name__)
5349

54-
PathIsh = Path | str
50+
type PathIsh = Path | str
5551

5652
Backend = Literal['sqlite', 'file']
5753

src/cachew/backend/sqlite.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ def set_sqlite_pragma(dbapi_connection, connection_record): # noqa: ARG001
5050
"""
5151

5252
@event.listens_for(self.connection, 'begin')
53-
# pylint: disable=unused-variable
5453
def do_begin(conn):
5554
# NOTE there is also BEGIN CONCURRENT in newer versions of sqlite. could use it later?
5655
conn.execute(text('BEGIN DEFERRED'))
@@ -178,7 +177,6 @@ def flush_blobs(self, chunk: Sequence[bytes]) -> None:
178177

179178
def finalize(self, new_hash: SourceHash) -> None:
180179
# delete hash first, so if we are interrupted somewhere, it mismatches next time and everything is recomputed
181-
# pylint: disable=no-value-for-parameter
182180
self.connection.execute(self.table_hash.delete())
183181

184182
# checkfirst is necessary since it might not have existed in the first place
@@ -189,5 +187,4 @@ def finalize(self, new_hash: SourceHash) -> None:
189187
# also seems like sqlalchemy doesn't have any primitives to escape table names.. sigh
190188
self.connection.execute(text(f"ALTER TABLE `{self.table_cache_tmp.name}` RENAME TO `{self.table_cache.name}`"))
191189

192-
# pylint: disable=no-value-for-parameter
193190
self.connection.execute(self.table_hash.insert().values([{'value': new_hash}]))

src/cachew/common.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,17 @@
1+
from dataclasses import dataclass
2+
13
# TODO better name to represent what it means?
2-
SourceHash = str
4+
type SourceHash = str
5+
6+
7+
class CachewException(RuntimeError):
8+
pass
9+
10+
11+
@dataclass
12+
class TypeNotSupported(CachewException):
13+
type_: type
14+
reason: str
15+
16+
def __str__(self) -> str:
17+
return f"{self.type_} isn't supported by cachew: {self.reason}. See https://github.com/karlicoss/cachew#features for the list of supported types."

src/cachew/experimental.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
from .compat import deprecated
1+
from typing import TYPE_CHECKING
22

3+
if not TYPE_CHECKING:
4+
from .compat import deprecated
35

4-
@deprecated("Exceptions are not an experimental feature anymore and enabled by default.")
5-
def enable_exceptions() -> None:
6-
pass
6+
@deprecated("Exceptions are not an experimental feature anymore and enabled by default.")
7+
def enable_exceptions() -> None:
8+
pass
79

8-
9-
@deprecated("Exceptions are not an experimental feature anymore and enabled by default.")
10-
def disable_exceptions() -> None:
11-
pass
10+
@deprecated("Exceptions are not an experimental feature anymore and enabled by default.")
11+
def disable_exceptions() -> None:
12+
pass

src/cachew/legacy.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from sqlalchemy import Column
1919

2020
from .pytest import parametrize
21-
from .utils import CachewException
21+
from .common import CachewException
2222

2323

2424
def get_union_args(cls) -> Optional[tuple[type]]:
@@ -300,7 +300,7 @@ class NTBinder(Generic[NT]):
300300
>>> binder.from_row(('ann', 25, True, None, None, 'extra'))
301301
Traceback (most recent call last):
302302
...
303-
cachew.utils.CachewException: unconsumed items in iterator ['extra']
303+
cachew.common.CachewException: unconsumed items in iterator ['extra']
304304
"""
305305

306306
name: Optional[str] # None means toplevel

src/cachew/logging_helper.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import os
55
import warnings
66
from functools import lru_cache
7+
from typing import TYPE_CHECKING
78

89

910
def test() -> None:
@@ -34,7 +35,7 @@ def test() -> None:
3435
M(
3536
"\n Also exception logging is kinda lame, doesn't print traceback by default unless you remember to pass exc_info:"
3637
)
37-
l.exception(ex) # type: ignore[possibly-undefined] # pylint: disable=used-before-assignment
38+
l.exception(ex) # type: ignore[possibly-undefined]
3839

3940
M(
4041
"\n\n With make_logger you get a reasonable logging format, colours (via colorlog library) and other neat things:"
@@ -245,6 +246,7 @@ def get_enlighten():
245246

246247

247248
## legacy/deprecated methods for backwards compatilibity
248-
LazyLogger = make_logger
249-
logger = make_logger
249+
if not TYPE_CHECKING:
250+
LazyLogger = make_logger
251+
logger = make_logger
250252
##

src/cachew/marshall/cachew.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
)
2222
from zoneinfo import ZoneInfo
2323

24-
from ..utils import TypeNotSupported, is_namedtuple, resolve_type_parameters
24+
from ..common import TypeNotSupported
25+
from ..utils import is_namedtuple, resolve_type_parameters
2526
from .common import AbstractMarshall, Json
2627

2728

src/cachew/tests/marshall.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@
55
from dataclasses import dataclass
66
from datetime import UTC, datetime
77
from pathlib import Path
8-
from typing import (
9-
Any,
10-
Literal,
11-
)
8+
from typing import Any, Literal
129

1310
import orjson
1411
import pytest

0 commit comments

Comments
 (0)