From dc8ca22b1994af994ce9502494f4df1741c0559d Mon Sep 17 00:00:00 2001 From: Stainless Bot <107565488+stainless-bot@users.noreply.github.com> Date: Wed, 17 Apr 2024 18:47:53 -0400 Subject: [PATCH] chore(internal): ban usage of lru_cache (#464) --- pyproject.toml | 7 ++++++- src/anthropic/_base_client.py | 3 +-- src/anthropic/_models.py | 2 +- src/anthropic/_utils/_utils.py | 4 +++- src/anthropic/lib/bedrock/_stream_decoder.py | 2 +- 5 files changed, 12 insertions(+), 6 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index abfd32d5..fcba0e7c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -163,7 +163,9 @@ select = [ "T201", "T203", # misuse of typing.TYPE_CHECKING - "TCH004" + "TCH004", + # import rules + "TID251", ] ignore = [ # mutable defaults @@ -179,6 +181,9 @@ ignore-init-module-imports = true [tool.ruff.format] docstring-code-format = true +[tool.ruff.lint.flake8-tidy-imports.banned-api] +"functools.lru_cache".msg = "This function does not retain type information for the wrapped function's arguments; The `lru_cache` function from `_utils` should be used instead" + [tool.ruff.lint.isort] length-sort = true length-sort-straight = true diff --git a/src/anthropic/_base_client.py b/src/anthropic/_base_client.py index 99cdfc58..3e688b50 100644 --- a/src/anthropic/_base_client.py +++ b/src/anthropic/_base_client.py @@ -29,7 +29,6 @@ cast, overload, ) -from functools import lru_cache from typing_extensions import Literal, override, get_origin import anyio @@ -61,7 +60,7 @@ RequestOptions, ModelBuilderProtocol, ) -from ._utils import is_dict, is_list, is_given, is_mapping +from ._utils import is_dict, is_list, is_given, lru_cache, is_mapping from ._compat import model_copy, model_dump from ._models import GenericModel, FinalRequestOptions, validate_type, construct_type from ._response import ( diff --git a/src/anthropic/_models.py b/src/anthropic/_models.py index 80ab5125..ff93fbd8 100644 --- a/src/anthropic/_models.py +++ b/src/anthropic/_models.py @@ -4,7 +4,6 @@ import inspect from typing import TYPE_CHECKING, Any, Type, Union, Generic, TypeVar, Callable, cast from datetime import date, datetime -from functools import lru_cache from typing_extensions import ( Unpack, Literal, @@ -37,6 +36,7 @@ PropertyInfo, is_list, is_given, + lru_cache, is_mapping, parse_date, coerce_boolean, diff --git a/src/anthropic/_utils/_utils.py b/src/anthropic/_utils/_utils.py index 5123a230..fd3a8a4d 100644 --- a/src/anthropic/_utils/_utils.py +++ b/src/anthropic/_utils/_utils.py @@ -395,5 +395,7 @@ def lru_cache(*, maxsize: int | None = 128) -> Callable[[CallableT], CallableT]: """A version of functools.lru_cache that retains the type signature for the wrapped function arguments. """ - wrapper = functools.lru_cache(maxsize=maxsize) + wrapper = functools.lru_cache( # noqa: TID251 + maxsize=maxsize, + ) return cast(Any, wrapper) # type: ignore[no-any-return] diff --git a/src/anthropic/lib/bedrock/_stream_decoder.py b/src/anthropic/lib/bedrock/_stream_decoder.py index 2065de4b..02e81a3c 100644 --- a/src/anthropic/lib/bedrock/_stream_decoder.py +++ b/src/anthropic/lib/bedrock/_stream_decoder.py @@ -1,8 +1,8 @@ from __future__ import annotations from typing import TYPE_CHECKING, Iterator, AsyncIterator -from functools import lru_cache +from ..._utils import lru_cache from ..._streaming import ServerSentEvent if TYPE_CHECKING: