Skip to content
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

refactor: Use functools.lru_cache instead of the stale memoization library #1981

Merged
merged 1 commit into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions docs/code_samples.md
Original file line number Diff line number Diff line change
Expand Up @@ -240,16 +240,15 @@ class SingletonAuthStream(RESTStream):
### Make a stream reuse the same authenticator instance for all requests

```python
from memoization import cached
from functools import cached_property

from singer_sdk.authenticators import APIAuthenticatorBase
from singer_sdk.streams import RESTStream

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

@property
@cached
@cached_property
def authenticator(self) -> APIAuthenticatorBase:
"""Stream authenticator."""
return APIAuthenticatorBase(stream=self)
Expand Down
12 changes: 1 addition & 11 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ PyJWT = "~=2.4"
requests = ">=2.25.1"
cryptography = ">=3.4.6,<42.0.0"
importlib-resources = {version = ">=5.12.0", markers = "python_version < \"3.9\""}
memoization = ">=0.3.2,<0.5.0"
jsonpath-ng = ">=1.5.3"
joblib = ">=1.0.1"
inflection = ">=0.5.1"
Expand Down
10 changes: 5 additions & 5 deletions singer_sdk/helpers/_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@
import typing as t
from copy import deepcopy

from memoization import cached

from singer_sdk.helpers._typing import is_object_type

if t.TYPE_CHECKING:
from logging import Logger

from singer_sdk._singerlib import Catalog, SelectionMask

_MAX_LRU_CACHE = 500


@cached(max_size=_MAX_LRU_CACHE)
# TODO: this was previously cached using the `memoization` library. However, the
# `functools.lru_cache` decorator does not support non-hashable arguments.
# It is possible that this function is not a bottleneck, but if it is, we should
# consider implementing a custom LRU cache decorator that supports non-hashable
# arguments.
def get_selected_schema(
stream_name: str,
schema: dict,
Expand Down
4 changes: 2 additions & 2 deletions singer_sdk/helpers/jsonpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
from __future__ import annotations

import typing as t
from functools import lru_cache

import memoization
from jsonpath_ng.ext import parse

if t.TYPE_CHECKING:
Expand All @@ -31,7 +31,7 @@ def extract_jsonpath(
yield match.value


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

Expand Down
5 changes: 2 additions & 3 deletions tests/core/rest/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
from __future__ import annotations

import typing as t
from functools import cached_property

import pytest
from memoization.memoization import cached
from requests.auth import HTTPProxyAuth

from singer_sdk.authenticators import APIAuthenticatorBase, SingletonMeta
Expand Down Expand Up @@ -49,8 +49,7 @@ class NaiveAuthenticator(APIAuthenticatorBase):
class CachedAuthStream(SimpleRESTStream):
"""A stream with Naive authentication."""

@property
@cached
@cached_property
def authenticator(self) -> NaiveAuthenticator:
"""Stream authenticator."""
return NaiveAuthenticator(stream=self)
Expand Down
9 changes: 0 additions & 9 deletions tests/core/test_mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

from singer_sdk._singerlib import Catalog
from singer_sdk.exceptions import MapExpressionError
from singer_sdk.helpers._catalog import get_selected_schema
from singer_sdk.mapper import PluginMapper, RemoveRecordTransform, md5
from singer_sdk.streams.core import Stream
from singer_sdk.tap_base import Tap
Expand Down Expand Up @@ -463,16 +462,8 @@ def discover_streams(self):
return [MappedStream(self)]


@pytest.fixture
def _clear_schema_cache() -> None:
"""Schemas are cached, so the cache needs to be cleared between test invocations."""
yield
get_selected_schema.cache_clear()


@freeze_time("2022-01-01T00:00:00Z")
@pytest.mark.snapshot()
@pytest.mark.usefixtures("_clear_schema_cache")
@pytest.mark.parametrize(
"stream_maps,flatten,flatten_max_depth,snapshot_name",
[
Expand Down