Skip to content

Commit 3496e17

Browse files
authored
feat(langchain): add ruff rules PL (#32079)
See https://docs.astral.sh/ruff/rules/#pylint-pl
1 parent 0f39155 commit 3496e17

File tree

24 files changed

+97
-102
lines changed

24 files changed

+97
-102
lines changed

libs/langchain/langchain/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ def __getattr__(name: str) -> Any:
357357

358358
return ElasticVectorSearch
359359
# For backwards compatibility
360-
if name == "SerpAPIChain" or name == "SerpAPIWrapper":
360+
if name in {"SerpAPIChain", "SerpAPIWrapper"}:
361361
from langchain_community.utilities import SerpAPIWrapper
362362

363363
_warn_on_import(

libs/langchain/langchain/agents/format_scratchpad/xml.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@ def format_xml(
4040
# Escape XML tags in tool names and inputs using custom delimiters
4141
tool = _escape(action.tool)
4242
tool_input = _escape(str(action.tool_input))
43-
observation = _escape(str(observation))
43+
observation_ = _escape(str(observation))
4444
else:
4545
tool = action.tool
4646
tool_input = str(action.tool_input)
47-
observation = str(observation)
47+
observation_ = str(observation)
4848
log += (
4949
f"<tool>{tool}</tool><tool_input>{tool_input}"
50-
f"</tool_input><observation>{observation}</observation>"
50+
f"</tool_input><observation>{observation_}</observation>"
5151
)
5252
return log

libs/langchain/langchain/agents/openai_assistant/base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,7 @@ def _get_response(self, run: Any) -> Any:
582582
major_version = int(openai.version.VERSION.split(".")[0])
583583
minor_version = int(openai.version.VERSION.split(".")[1])
584584
version_gte_1_14 = (major_version > 1) or (
585-
major_version == 1 and minor_version >= 14
585+
major_version == 1 and minor_version >= 14 # noqa: PLR2004
586586
)
587587

588588
messages = self.client.beta.threads.messages.list(
@@ -739,7 +739,7 @@ async def _aget_response(self, run: Any) -> Any:
739739
major_version = int(openai.version.VERSION.split(".")[0])
740740
minor_version = int(openai.version.VERSION.split(".")[1])
741741
version_gte_1_14 = (major_version > 1) or (
742-
major_version == 1 and minor_version >= 14
742+
major_version == 1 and minor_version >= 14 # noqa: PLR2004
743743
)
744744

745745
messages = await self.async_client.beta.threads.messages.list(

libs/langchain/langchain/agents/react/base.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
from langchain_community.docstore.base import Docstore
2525

2626

27+
_LOOKUP_AND_SEARCH_TOOLS = {"Lookup", "Search"}
28+
29+
2730
@deprecated(
2831
"0.1.0",
2932
message=AGENT_DEPRECATION_WARNING,
@@ -52,11 +55,11 @@ def create_prompt(cls, tools: Sequence[BaseTool]) -> BasePromptTemplate:
5255
def _validate_tools(cls, tools: Sequence[BaseTool]) -> None:
5356
validate_tools_single_input(cls.__name__, tools)
5457
super()._validate_tools(tools)
55-
if len(tools) != 2:
58+
if len(tools) != len(_LOOKUP_AND_SEARCH_TOOLS):
5659
msg = f"Exactly two tools must be specified, but got {tools}"
5760
raise ValueError(msg)
5861
tool_names = {tool.name for tool in tools}
59-
if tool_names != {"Lookup", "Search"}:
62+
if tool_names != _LOOKUP_AND_SEARCH_TOOLS:
6063
msg = f"Tool names should be Lookup and Search, got {tool_names}"
6164
raise ValueError(msg)
6265

libs/langchain/langchain/chains/api/base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ def _check_in_allowed_domain(url: str, limit_to_domains: Sequence[str]) -> bool:
4747
scheme, domain = _extract_scheme_and_domain(url)
4848

4949
for allowed_domain in limit_to_domains:
50-
allowed_scheme, allowed_domain = _extract_scheme_and_domain(allowed_domain)
51-
if scheme == allowed_scheme and domain == allowed_domain:
50+
allowed_scheme, allowed_domain_ = _extract_scheme_and_domain(allowed_domain)
51+
if scheme == allowed_scheme and domain == allowed_domain_:
5252
return True
5353
return False
5454

libs/langchain/langchain/chains/combine_documents/map_reduce.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -193,13 +193,12 @@ def get_default_document_variable_name(cls, values: dict) -> Any:
193193
"multiple llm_chain input_variables"
194194
)
195195
raise ValueError(msg)
196-
else:
197-
if values["document_variable_name"] not in llm_chain_variables:
198-
msg = (
199-
f"document_variable_name {values['document_variable_name']} was "
200-
f"not found in llm_chain input_variables: {llm_chain_variables}"
201-
)
202-
raise ValueError(msg)
196+
elif values["document_variable_name"] not in llm_chain_variables:
197+
msg = (
198+
f"document_variable_name {values['document_variable_name']} was "
199+
f"not found in llm_chain input_variables: {llm_chain_variables}"
200+
)
201+
raise ValueError(msg)
203202
return values
204203

205204
@property

libs/langchain/langchain/chains/combine_documents/map_rerank.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -161,13 +161,12 @@ def get_default_document_variable_name(cls, values: dict) -> Any:
161161
"multiple llm_chain input_variables"
162162
)
163163
raise ValueError(msg)
164-
else:
165-
if values["document_variable_name"] not in llm_chain_variables:
166-
msg = (
167-
f"document_variable_name {values['document_variable_name']} was "
168-
f"not found in llm_chain input_variables: {llm_chain_variables}"
169-
)
170-
raise ValueError(msg)
164+
elif values["document_variable_name"] not in llm_chain_variables:
165+
msg = (
166+
f"document_variable_name {values['document_variable_name']} was "
167+
f"not found in llm_chain input_variables: {llm_chain_variables}"
168+
)
169+
raise ValueError(msg)
171170
return values
172171

173172
def combine_docs(

libs/langchain/langchain/chains/combine_documents/reduce.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -325,10 +325,10 @@ def _collapse_docs_func(docs: list[Document], **kwargs: Any) -> str:
325325
_token_max,
326326
**kwargs,
327327
)
328-
result_docs = []
329-
for docs in new_result_doc_list:
330-
new_doc = collapse_docs(docs, _collapse_docs_func, **kwargs)
331-
result_docs.append(new_doc)
328+
result_docs = [
329+
collapse_docs(docs_, _collapse_docs_func, **kwargs)
330+
for docs_ in new_result_doc_list
331+
]
332332
num_tokens = length_func(result_docs, **kwargs)
333333
retries += 1
334334
if self.collapse_max_retries and retries == self.collapse_max_retries:
@@ -364,10 +364,10 @@ async def _collapse_docs_func(docs: list[Document], **kwargs: Any) -> str:
364364
_token_max,
365365
**kwargs,
366366
)
367-
result_docs = []
368-
for docs in new_result_doc_list:
369-
new_doc = await acollapse_docs(docs, _collapse_docs_func, **kwargs)
370-
result_docs.append(new_doc)
367+
result_docs = [
368+
await acollapse_docs(docs_, _collapse_docs_func, **kwargs)
369+
for docs_ in new_result_doc_list
370+
]
371371
num_tokens = length_func(result_docs, **kwargs)
372372
retries += 1
373373
if self.collapse_max_retries and retries == self.collapse_max_retries:

libs/langchain/langchain/chains/combine_documents/refine.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -140,13 +140,12 @@ def get_default_document_variable_name(cls, values: dict) -> Any:
140140
"multiple llm_chain input_variables"
141141
)
142142
raise ValueError(msg)
143-
else:
144-
if values["document_variable_name"] not in llm_chain_variables:
145-
msg = (
146-
f"document_variable_name {values['document_variable_name']} was "
147-
f"not found in llm_chain input_variables: {llm_chain_variables}"
148-
)
149-
raise ValueError(msg)
143+
elif values["document_variable_name"] not in llm_chain_variables:
144+
msg = (
145+
f"document_variable_name {values['document_variable_name']} was "
146+
f"not found in llm_chain input_variables: {llm_chain_variables}"
147+
)
148+
raise ValueError(msg)
150149
return values
151150

152151
def combine_docs(

libs/langchain/langchain/chains/combine_documents/stuff.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -180,13 +180,12 @@ def get_default_document_variable_name(cls, values: dict) -> Any:
180180
"multiple llm_chain_variables"
181181
)
182182
raise ValueError(msg)
183-
else:
184-
if values["document_variable_name"] not in llm_chain_variables:
185-
msg = (
186-
f"document_variable_name {values['document_variable_name']} was "
187-
f"not found in llm_chain input_variables: {llm_chain_variables}"
188-
)
189-
raise ValueError(msg)
183+
elif values["document_variable_name"] not in llm_chain_variables:
184+
msg = (
185+
f"document_variable_name {values['document_variable_name']} was "
186+
f"not found in llm_chain input_variables: {llm_chain_variables}"
187+
)
188+
raise ValueError(msg)
190189
return values
191190

192191
@property

0 commit comments

Comments
 (0)