Skip to content

Commit 3d280da

Browse files
refactor: Use functools.lru_cache instead of the stale memoization library (#1981)
1 parent 49aa4a9 commit 3d280da

File tree

7 files changed

+461
-608
lines changed

7 files changed

+461
-608
lines changed

docs/code_samples.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -240,16 +240,15 @@ class SingletonAuthStream(RESTStream):
240240
### Make a stream reuse the same authenticator instance for all requests
241241

242242
```python
243-
from memoization import cached
243+
from functools import cached_property
244244

245245
from singer_sdk.authenticators import APIAuthenticatorBase
246246
from singer_sdk.streams import RESTStream
247247

248248
class CachedAuthStream(RESTStream):
249249
"""A stream with singleton authenticator."""
250250

251-
@property
252-
@cached
251+
@cached_property
253252
def authenticator(self) -> APIAuthenticatorBase:
254253
"""Stream authenticator."""
255254
return APIAuthenticatorBase(stream=self)

poetry.lock

Lines changed: 450 additions & 585 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ inflection = ">=0.5.1"
4747
joblib = ">=1.0.1"
4848
jsonpath-ng = ">=1.5.3"
4949
jsonschema = ">=4.16.0"
50-
memoization = ">=0.3.2,<0.5.0"
5150
packaging = ">=23.1"
5251
pendulum = ">=2.1.0"
5352
PyJWT = "~=2.4"

singer_sdk/helpers/_catalog.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,19 @@
55
import typing as t
66
from copy import deepcopy
77

8-
from memoization import cached
9-
108
from singer_sdk.helpers._typing import is_object_type
119

1210
if t.TYPE_CHECKING:
1311
from logging import Logger
1412

1513
from singer_sdk._singerlib import Catalog, SelectionMask
1614

17-
_MAX_LRU_CACHE = 500
18-
1915

20-
@cached(max_size=_MAX_LRU_CACHE)
16+
# TODO: this was previously cached using the `memoization` library. However, the
17+
# `functools.lru_cache` decorator does not support non-hashable arguments.
18+
# It is possible that this function is not a bottleneck, but if it is, we should
19+
# consider implementing a custom LRU cache decorator that supports non-hashable
20+
# arguments.
2121
def get_selected_schema(
2222
stream_name: str,
2323
schema: dict,

singer_sdk/helpers/jsonpath.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
import logging
66
import typing as t
7+
from functools import lru_cache
78

8-
import memoization
99
from jsonpath_ng.ext import parse
1010

1111
if t.TYPE_CHECKING:
@@ -39,7 +39,7 @@ def extract_jsonpath(
3939
yield match.value
4040

4141

42-
@memoization.cached
42+
@lru_cache
4343
def _compile_jsonpath(expression: str) -> jsonpath_ng.JSONPath:
4444
"""Parse a JSONPath expression and cache the result.
4545

tests/core/rest/conftest.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
from __future__ import annotations
44

55
import typing as t
6+
from functools import cached_property
67

78
import pytest
8-
from memoization.memoization import cached
99
from requests.auth import HTTPProxyAuth
1010

1111
from singer_sdk.authenticators import APIAuthenticatorBase, SingletonMeta
@@ -49,8 +49,7 @@ class NaiveAuthenticator(APIAuthenticatorBase):
4949
class CachedAuthStream(SimpleRESTStream):
5050
"""A stream with Naive authentication."""
5151

52-
@property
53-
@cached
52+
@cached_property
5453
def authenticator(self) -> NaiveAuthenticator:
5554
"""Stream authenticator."""
5655
return NaiveAuthenticator(stream=self)

tests/core/test_mapper.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
from singer_sdk._singerlib import Catalog
1818
from singer_sdk.exceptions import MapExpressionError
19-
from singer_sdk.helpers._catalog import get_selected_schema
2019
from singer_sdk.mapper import PluginMapper, RemoveRecordTransform, md5
2120
from singer_sdk.streams.core import Stream
2221
from singer_sdk.tap_base import Tap
@@ -563,19 +562,11 @@ def discover_streams(self):
563562
return [MappedStream(self)]
564563

565564

566-
@pytest.fixture
567-
def _clear_schema_cache() -> None:
568-
"""Schemas are cached, so the cache needs to be cleared between test invocations."""
569-
yield
570-
get_selected_schema.cache_clear()
571-
572-
573565
@time_machine.travel(
574566
datetime.datetime(2022, 1, 1, tzinfo=datetime.timezone.utc),
575567
tick=False,
576568
)
577569
@pytest.mark.snapshot()
578-
@pytest.mark.usefixtures("_clear_schema_cache")
579570
@pytest.mark.parametrize(
580571
"stream_maps,flatten,flatten_max_depth,snapshot_name",
581572
[

0 commit comments

Comments
 (0)