core: Add ruff rules SLF (#30666)

Add ruff rules SLF: https://docs.astral.sh/ruff/rules/#flake8-self-slf

---------

Co-authored-by: Eugene Yurtsev <eyurtsev@gmail.com>
This commit is contained in:
Christophe Bornet 2025-05-14 20:42:39 +02:00 committed by GitHub
parent d8a7eda12e
commit 921573e2b7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 1219 additions and 1220 deletions

View File

@ -412,8 +412,8 @@ class BaseChatModel(BaseLanguageModel[BaseMessage], ABC):
**kwargs: Any,
) -> bool:
"""Determine if a given model call should hit the streaming API."""
sync_not_implemented = type(self)._stream == BaseChatModel._stream
async_not_implemented = type(self)._astream == BaseChatModel._astream
sync_not_implemented = type(self)._stream == BaseChatModel._stream # noqa: SLF001
async_not_implemented = type(self)._astream == BaseChatModel._astream # noqa: SLF001
# Check if streaming is implemented.
if (not async_api) and sync_not_implemented:

View File

@ -522,7 +522,7 @@ class BaseLLM(BaseLanguageModel[str], ABC):
stop: Optional[list[str]] = None,
**kwargs: Any,
) -> Iterator[str]:
if type(self)._stream == BaseLLM._stream:
if type(self)._stream == BaseLLM._stream: # noqa: SLF001
# model doesn't implement streaming, so use default implementation
yield self.invoke(input, config=config, stop=stop, **kwargs)
else:
@ -590,8 +590,8 @@ class BaseLLM(BaseLanguageModel[str], ABC):
**kwargs: Any,
) -> AsyncIterator[str]:
if (
type(self)._astream is BaseLLM._astream
and type(self)._stream is BaseLLM._stream
type(self)._astream is BaseLLM._astream # noqa: SLF001
and type(self)._stream is BaseLLM._stream # noqa: SLF001
):
yield await self.ainvoke(input, config=config, stop=stop, **kwargs)
return

View File

@ -131,7 +131,7 @@ class DynamicRunnable(RunnableSerializable[Input, Output]):
"""
runnable: Runnable[Input, Output] = self
while isinstance(runnable, DynamicRunnable):
runnable, config = runnable._prepare(merge_configs(runnable.config, config))
runnable, config = runnable._prepare(merge_configs(runnable.config, config)) # noqa: SLF001
return runnable, cast("RunnableConfig", config)
@abstractmethod

View File

@ -845,7 +845,7 @@ class ChildTool(BaseTool):
child_config = patch_config(config, callbacks=run_manager.get_child())
with set_config_context(child_config) as context:
func_to_check = (
self._run if self.__class__._arun is BaseTool._arun else self._arun
self._run if self.__class__._arun is BaseTool._arun else self._arun # noqa: SLF001
)
if signature(func_to_check).parameters.get("run_manager"):
tool_kwargs["run_manager"] = run_manager

View File

@ -50,8 +50,8 @@ def log_error_once(method: str, exception: Exception) -> None:
def wait_for_all_tracers() -> None:
"""Wait for all tracers to finish."""
if rt._CLIENT is not None:
rt._CLIENT.flush()
if rt._CLIENT is not None: # noqa: SLF001
rt._CLIENT.flush() # noqa: SLF001
def get_client() -> Client:
@ -123,8 +123,8 @@ class LangChainTracer(BaseTracer):
run.tags = self.tags.copy()
super()._start_trace(run)
if run._client is None:
run._client = self.client # type: ignore[misc]
if run.ls_client is None:
run.ls_client = self.client
def on_chat_model_start(
self,

View File

@ -379,7 +379,7 @@ def _get_key(
try:
# This allows for custom falsy data types
# https://github.com/noahmorrison/chevron/issues/35
if resolved_scope._CHEVRON_return_scope_when_falsy: # type: ignore[union-attr]
if resolved_scope._CHEVRON_return_scope_when_falsy: # type: ignore[union-attr] # noqa: SLF001
return resolved_scope
except AttributeError:
if resolved_scope in (0, False):

View File

@ -7,7 +7,7 @@ authors = []
license = {text = "MIT"}
requires-python = ">=3.9"
dependencies = [
"langsmith<0.4,>=0.1.125",
"langsmith<0.4,>=0.1.126",
"tenacity!=8.4.0,<10.0.0,>=8.1.0",
"jsonpatch<2.0,>=1.33",
"PyYAML>=5.3",
@ -105,7 +105,6 @@ ignore = [
"ERA",
"PLR2004",
"RUF",
"SLF",
]
flake8-type-checking.runtime-evaluated-base-classes = ["pydantic.BaseModel","langchain_core.load.serializable.Serializable","langchain_core.runnables.base.RunnableSerializable"]
flake8-annotations.allow-star-arg-any = true
@ -132,5 +131,5 @@ classmethod-decorators = [ "classmethod", "langchain_core.utils.pydantic.pre_ini
"tests/unit_tests/runnables/test_runnable.py" = [ "E501",]
"tests/unit_tests/runnables/test_graph.py" = [ "E501",]
"tests/unit_tests/test_tools.py" = [ "ARG",]
"tests/**" = [ "D", "S",]
"tests/**" = [ "D", "S", "SLF",]
"scripts/**" = [ "INP", "S",]

File diff suppressed because it is too large Load Diff