From d76e3d07f6a11b3f776a44356e74c997fd5d5e7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edgar=20Ram=C3=ADrez=20Mondrag=C3=B3n?= Date: Tue, 26 Sep 2023 15:06:47 -0600 Subject: [PATCH] refactor: Use `functools.lru_cache` instead of the stale `memoization` library (#1981) --- poetry.lock | 12 +----------- pyproject.toml | 1 - singer_sdk/helpers/_catalog.py | 10 +++++----- singer_sdk/helpers/jsonpath.py | 4 ++-- tests/core/rest/conftest.py | 5 ++--- tests/core/test_mapper.py | 9 --------- 6 files changed, 10 insertions(+), 31 deletions(-) diff --git a/poetry.lock b/poetry.lock index 77ce8dff3..ace8612dd 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1059,16 +1059,6 @@ files = [ {file = "mdurl-0.1.2.tar.gz", hash = "sha256:bb413d29f5eea38f31dd4754dd7377d4465116fb207585f97bf925588687c1ba"}, ] -[[package]] -name = "memoization" -version = "0.4.0" -description = "A powerful caching library for Python, with TTL support and multiple algorithm options. (https://github.com/lonelyenvoy/python-memoization)" -optional = false -python-versions = ">=3, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, <4" -files = [ - {file = "memoization-0.4.0.tar.gz", hash = "sha256:fde5e7cd060ef45b135e0310cfec17b2029dc472ccb5bbbbb42a503d4538a135"}, -] - [[package]] name = "mypy" version = "1.8.0" @@ -2620,4 +2610,4 @@ testing = ["pytest", "pytest-durations"] [metadata] lock-version = "2.0" python-versions = ">=3.8" -content-hash = "3308a0e70f8f097b0a678989aa8d25133ff05e8d9ea37232d739a9664b05f361" +content-hash = "d4e6a793a168c7440918376ba46cf015de7cd633d0f957dfc244db89f81b85d8" diff --git a/pyproject.toml b/pyproject.toml index a01fe5500..7558d9e18 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -50,7 +50,6 @@ inflection = ">=0.5.1" joblib = ">=1.0.1" jsonpath-ng = ">=1.5.3" jsonschema = ">=4.16.0" -memoization = { version = ">=0.3.2,<0.5.0", python = "<4" } packaging = ">=23.1" pendulum = ">=2.1.0,<4" PyJWT = "~=2.4" diff --git a/singer_sdk/helpers/_catalog.py b/singer_sdk/helpers/_catalog.py index 39df3f0f1..4a263ff3f 100644 --- a/singer_sdk/helpers/_catalog.py +++ b/singer_sdk/helpers/_catalog.py @@ -5,17 +5,17 @@ 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 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, diff --git a/singer_sdk/helpers/jsonpath.py b/singer_sdk/helpers/jsonpath.py index 33e65c7fa..56c0895f3 100644 --- a/singer_sdk/helpers/jsonpath.py +++ b/singer_sdk/helpers/jsonpath.py @@ -4,8 +4,8 @@ import logging import typing as t +from functools import lru_cache -import memoization from jsonpath_ng.ext import parse if t.TYPE_CHECKING: @@ -39,7 +39,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. diff --git a/tests/core/rest/conftest.py b/tests/core/rest/conftest.py index 13d7eb9eb..daeb8d2dc 100644 --- a/tests/core/rest/conftest.py +++ b/tests/core/rest/conftest.py @@ -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 @@ -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) diff --git a/tests/core/test_mapper.py b/tests/core/test_mapper.py index 10f65cf8e..ab8c5f1fc 100644 --- a/tests/core/test_mapper.py +++ b/tests/core/test_mapper.py @@ -16,7 +16,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 @@ -563,19 +562,11 @@ 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() - - @time_machine.travel( datetime.datetime(2022, 1, 1, tzinfo=datetime.timezone.utc), tick=False, ) @pytest.mark.snapshot() -@pytest.mark.usefixtures("_clear_schema_cache") @pytest.mark.parametrize( "stream_maps,flatten,flatten_max_depth,snapshot_name", [