Skip to content

Commit 84f7371

Browse files
committed
chore(core): fix some ruff preview rules
1 parent 5c94e47 commit 84f7371

File tree

25 files changed

+84
-71
lines changed

25 files changed

+84
-71
lines changed

libs/core/langchain_core/example_selectors/length_based.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212

1313
def _get_length_based(text: str) -> int:
14-
return len(re.split("\n| ", text))
14+
return len(re.split(r"\n| ", text))
1515

1616

1717
class LengthBasedExampleSelector(BaseExampleSelector, BaseModel):

libs/core/langchain_core/language_models/chat_models.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,7 @@ def stream(
547547
):
548548
if block["type"] != index_type:
549549
index_type = block["type"]
550-
index = index + 1
550+
index += 1
551551
if "index" not in block:
552552
block["index"] = index
553553
run_manager.on_llm_new_token(
@@ -679,7 +679,7 @@ async def astream(
679679
):
680680
if block["type"] != index_type:
681681
index_type = block["type"]
682-
index = index + 1
682+
index += 1
683683
if "index" not in block:
684684
block["index"] = index
685685
await run_manager.on_llm_new_token(
@@ -730,7 +730,7 @@ async def astream(
730730

731731
# --- Custom methods ---
732732

733-
def _combine_llm_outputs(self, llm_outputs: list[dict | None]) -> dict: # noqa: ARG002
733+
def _combine_llm_outputs(self, _llm_outputs: list[dict | None], /) -> dict:
734734
return {}
735735

736736
def _convert_cached_generations(self, cache_val: list) -> list[ChatGeneration]:
@@ -1187,7 +1187,7 @@ def _generate_with_cache(
11871187
):
11881188
if block["type"] != index_type:
11891189
index_type = block["type"]
1190-
index = index + 1
1190+
index += 1
11911191
if "index" not in block:
11921192
block["index"] = index
11931193
if run_manager:
@@ -1305,7 +1305,7 @@ async def _agenerate_with_cache(
13051305
):
13061306
if block["type"] != index_type:
13071307
index_type = block["type"]
1308-
index = index + 1
1308+
index += 1
13091309
if "index" not in block:
13101310
block["index"] = index
13111311
if run_manager:

libs/core/langchain_core/messages/ai.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ def _backwards_compat_tool_calls(cls, values: dict) -> Any:
326326
if tool_calls := values.get("tool_calls"):
327327
values["tool_calls"] = [
328328
create_tool_call(
329-
**{k: v for k, v in tc.items() if k not in ("type", "extras")}
329+
**{k: v for k, v in tc.items() if k not in {"type", "extras"}}
330330
)
331331
for tc in tool_calls
332332
]
@@ -442,7 +442,7 @@ def content_blocks(self) -> list[types.ContentBlock]:
442442
blocks = [
443443
block
444444
for block in blocks
445-
if block["type"] not in ("tool_call", "invalid_tool_call")
445+
if block["type"] not in {"tool_call", "invalid_tool_call"}
446446
]
447447
for tool_call_chunk in self.tool_call_chunks:
448448
tc: types.ToolCallChunk = {
@@ -573,7 +573,7 @@ def init_server_tool_calls(self) -> Self:
573573
if (
574574
isinstance(block, dict)
575575
and block.get("type")
576-
in ("server_tool_call", "server_tool_call_chunk")
576+
in {"server_tool_call", "server_tool_call_chunk"}
577577
and (args_str := block.get("args"))
578578
and isinstance(args_str, str)
579579
):

libs/core/langchain_core/messages/block_translators/anthropic.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -159,12 +159,12 @@ def _convert_citation_to_v1(citation: dict[str, Any]) -> types.Annotation:
159159

160160
return url_citation
161161

162-
if citation_type in (
162+
if citation_type in {
163163
"char_location",
164164
"content_block_location",
165165
"page_location",
166166
"search_result_location",
167-
):
167+
}:
168168
document_citation: types.Citation = {
169169
"type": "citation",
170170
"cited_text": citation["cited_text"],
@@ -173,8 +173,6 @@ def _convert_citation_to_v1(citation: dict[str, Any]) -> types.Annotation:
173173
document_citation["title"] = citation["document_title"]
174174
elif title := citation.get("title"):
175175
document_citation["title"] = title
176-
else:
177-
pass
178176
known_fields = {
179177
"type",
180178
"cited_text",
@@ -280,8 +278,6 @@ def _iter_blocks() -> Iterable[types.ContentBlock]:
280278
"id": tc.get("id"),
281279
}
282280
break
283-
else:
284-
pass
285281
if not tool_call_block:
286282
tool_call_block = {
287283
"type": "tool_call",

libs/core/langchain_core/messages/block_translators/bedrock_converse.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,6 @@ def _iter_blocks() -> Iterable[types.ContentBlock]:
240240
"id": tc.get("id"),
241241
}
242242
break
243-
else:
244-
pass
245243
if not tool_call_block:
246244
tool_call_block = {
247245
"type": "tool_call",

libs/core/langchain_core/messages/block_translators/google_genai.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,10 @@ def _convert_to_v1_from_genai(message: AIMessage) -> list[types.ContentBlock]:
411411
except Exception:
412412
# Not valid base64, treat as non-standard
413413
converted_blocks.append(
414-
{"type": "non_standard", "value": item}
414+
{
415+
"type": "non_standard",
416+
"value": item,
417+
}
415418
)
416419
else:
417420
# This likely won't be reached according to previous implementations

libs/core/langchain_core/messages/block_translators/openai.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ def _convert_from_v1_to_chat_completions(message: AIMessage) -> AIMessage:
248248
if block_type == "text":
249249
# Strip annotations
250250
new_content.append({"type": "text", "text": block["text"]})
251-
elif block_type in ("reasoning", "tool_call"):
251+
elif block_type in {"reasoning", "tool_call"}:
252252
pass
253253
else:
254254
new_content.append(block)
@@ -706,8 +706,6 @@ def _iter_blocks() -> Iterable[types.ContentBlock]:
706706
if invalid_tool_call.get("id") == call_id:
707707
tool_call_block = invalid_tool_call.copy()
708708
break
709-
else:
710-
pass
711709
if tool_call_block:
712710
if "id" in block:
713711
if "extras" not in tool_call_block:
@@ -735,7 +733,7 @@ def _iter_blocks() -> Iterable[types.ContentBlock]:
735733
k: v for k, v in block["action"].items() if k != "sources"
736734
}
737735
for key in block:
738-
if key not in ("type", "id", "action", "status", "index"):
736+
if key not in {"type", "id", "action", "status", "index"}:
739737
web_search_call[key] = block[key]
740738

741739
yield cast("types.ServerToolCall", web_search_call)
@@ -761,8 +759,6 @@ def _iter_blocks() -> Iterable[types.ContentBlock]:
761759
web_search_result["status"] = "success"
762760
elif status:
763761
web_search_result["extras"] = {"status": status}
764-
else:
765-
pass
766762
if "index" in block and isinstance(block["index"], int):
767763
web_search_result["index"] = f"lc_wsr_{block['index'] + 1}"
768764
yield cast("types.ServerToolResult", web_search_result)
@@ -778,14 +774,14 @@ def _iter_blocks() -> Iterable[types.ContentBlock]:
778774
file_search_call["index"] = f"lc_fsc_{block['index']}"
779775

780776
for key in block:
781-
if key not in (
777+
if key not in {
782778
"type",
783779
"id",
784780
"queries",
785781
"results",
786782
"status",
787783
"index",
788-
):
784+
}:
789785
file_search_call[key] = block[key]
790786

791787
yield cast("types.ServerToolCall", file_search_call)
@@ -804,8 +800,6 @@ def _iter_blocks() -> Iterable[types.ContentBlock]:
804800
file_search_result["status"] = "success"
805801
elif status:
806802
file_search_result["extras"] = {"status": status}
807-
else:
808-
pass
809803
if "index" in block and isinstance(block["index"], int):
810804
file_search_result["index"] = f"lc_fsr_{block['index'] + 1}"
811805
yield cast("types.ServerToolResult", file_search_result)
@@ -849,8 +843,6 @@ def _iter_blocks() -> Iterable[types.ContentBlock]:
849843
code_interpreter_result["status"] = "success"
850844
elif status:
851845
code_interpreter_result["extras"] = {"status": status}
852-
else:
853-
pass
854846
if "index" in block and isinstance(block["index"], int):
855847
code_interpreter_result["index"] = f"lc_cir_{block['index'] + 1}"
856848

libs/core/langchain_core/messages/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1397,7 +1397,7 @@ def convert_to_openai_messages(
13971397
},
13981398
}
13991399
)
1400-
elif block.get("type") in ["thinking", "reasoning"]:
1400+
elif block.get("type") in {"thinking", "reasoning"}:
14011401
content.append(block)
14021402
else:
14031403
err = (

libs/core/langchain_core/runnables/graph_png.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,8 @@ def add_edges(self, viz: Any, graph: Graph) -> None:
201201
viz, start, end, str(data) if data is not None else None, cond
202202
)
203203

204-
def update_styles(self, viz: Any, graph: Graph) -> None:
204+
@staticmethod
205+
def update_styles(viz: Any, graph: Graph) -> None:
205206
"""Update the styles of the entrypoint and END nodes.
206207
207208
Args:

libs/core/langchain_core/tools/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def _is_annotated_type(typ: type[Any]) -> bool:
9595
Returns:
9696
`True` if the type is an Annotated type, `False` otherwise.
9797
"""
98-
return get_origin(typ) in (typing.Annotated, typing_extensions.Annotated)
98+
return get_origin(typ) in {typing.Annotated, typing_extensions.Annotated}
9999

100100

101101
def _get_annotation_description(arg_type: type) -> str | None:

0 commit comments

Comments
 (0)