Skip to content

The agent engineering platform.

style(core): add ruff RUF012 rule (#34492)

Christophe Bornet5ef9f6e0362a
diff --git a/libs/core/langchain_core/messages/ai.py b/libs/core/langchain_core/messages/ai.py
index bf23d7a12e..b582b11329 100644
--- a/libs/core/langchain_core/messages/ai.py
+++ b/libs/core/langchain_core/messages/ai.py
@@ -6,7 +6,7 @@ import operator
from collections.abc import Sequence
from typing import Any, Literal, cast, overload
-from pydantic import model_validator
+from pydantic import Field, model_validator
from typing_extensions import NotRequired, Self, TypedDict, override
from langchain_core.messages import content as types
@@ -166,10 +166,10 @@ class AIMessage(BaseMessage):
(e.g., tool calls, usage metadata) added by the LangChain framework.
"""
- tool_calls: list[ToolCall] = []
+ tool_calls: list[ToolCall] = Field(default_factory=list)
"""If present, tool calls associated with the message."""
- invalid_tool_calls: list[InvalidToolCall] = []
+ invalid_tool_calls: list[InvalidToolCall] = Field(default_factory=list)
"""If present, tool calls with parsing errors associated with the message."""
usage_metadata: UsageMetadata | None = None
@@ -394,7 +394,7 @@ class AIMessageChunk(AIMessage, BaseMessageChunk):
type: Literal["AIMessageChunk"] = "AIMessageChunk" # type: ignore[assignment]
"""The type of the message (used for deserialization)."""
- tool_call_chunks: list[ToolCallChunk] = []
+ tool_call_chunks: list[ToolCallChunk] = Field(default_factory=list)
"""If provided, tool call chunks associated with the message."""
chunk_position: Literal["last"] | None = None
diff --git a/libs/core/pyproject.toml b/libs/core/pyproject.toml
index 4a18b23cd5..23f5f7a6cc 100644
--- a/libs/core/pyproject.toml
+++ b/libs/core/pyproject.toml
@@ -90,7 +90,6 @@ ignore = [
"FIX002", # Line contains TODO
"PERF203", # Rarely useful
"PLR09", # Too many something (arg, statements, etc)
- "RUF012", # Doesn't play well with Pydantic
"TD002", # Missing author in TODO
"TD003", # Missing issue link in TODO
diff --git a/libs/core/tests/unit_tests/prompts/__snapshots__/test_chat.ambr b/libs/core/tests/unit_tests/prompts/__snapshots__/test_chat.ambr
index e323c92274..3aa0d38504 100644
--- a/libs/core/tests/unit_tests/prompts/__snapshots__/test_chat.ambr
+++ b/libs/core/tests/unit_tests/prompts/__snapshots__/test_chat.ambr
@@ -51,8 +51,6 @@
'title': 'Id',
}),
'invalid_tool_calls': dict({
- 'default': list([
- ]),
'items': dict({
'$ref': '#/$defs/InvalidToolCall',
}),
@@ -76,8 +74,6 @@
'type': 'object',
}),
'tool_calls': dict({
- 'default': list([
- ]),
'items': dict({
'$ref': '#/$defs/ToolCall',
}),
@@ -162,8 +158,6 @@
'title': 'Id',
}),
'invalid_tool_calls': dict({
- 'default': list([
- ]),
'items': dict({
'$ref': '#/$defs/InvalidToolCall',
}),
@@ -187,8 +181,6 @@
'type': 'object',
}),
'tool_call_chunks': dict({
- 'default': list([
- ]),
'items': dict({
'$ref': '#/$defs/ToolCallChunk',
}),
@@ -196,8 +188,6 @@
'type': 'array',
}),
'tool_calls': dict({
- 'default': list([
- ]),
'items': dict({
'$ref': '#/$defs/ToolCall',
}),
@@ -1469,8 +1459,6 @@
'title': 'Id',
}),
'invalid_tool_calls': dict({
- 'default': list([
- ]),
'items': dict({
'$ref': '#/$defs/InvalidToolCall',
}),
@@ -1494,8 +1482,6 @@
'type': 'object',
}),
'tool_calls': dict({
- 'default': list([
- ]),
'items': dict({
'$ref': '#/$defs/ToolCall',
}),
@@ -1580,8 +1566,6 @@
'title': 'Id',
}),
'invalid_tool_calls': dict({
- 'default': list([
- ]),
'items': dict({
'$ref': '#/$defs/InvalidToolCall',
}),
@@ -1605,8 +1589,6 @@
'type': 'object',
}),
'tool_call_chunks': dict({
- 'default': list([
- ]),
'items': dict({
'$ref': '#/$defs/ToolCallChunk',
}),
@@ -1614,8 +1596,6 @@
'type': 'array',
}),
'tool_calls': dict({
- 'default': list([
- ]),
'items': dict({
'$ref': '#/$defs/ToolCall',
}),
diff --git a/libs/core/tests/unit_tests/runnables/__snapshots__/test_graph.ambr b/libs/core/tests/unit_tests/runnables/__snapshots__/test_graph.ambr
index ec54153f05..af75866593 100644
--- a/libs/core/tests/unit_tests/runnables/__snapshots__/test_graph.ambr
+++ b/libs/core/tests/unit_tests/runnables/__snapshots__/test_graph.ambr
@@ -475,8 +475,6 @@
'title': 'Id',
}),
'invalid_tool_calls': dict({
- 'default': list([
- ]),
'items': dict({
'$ref': '#/$defs/InvalidToolCall',
}),
@@ -500,8 +498,6 @@
'type': 'object',
}),
'tool_calls': dict({
- 'default': list([
- ]),
'items': dict({
'$ref': '#/$defs/ToolCall',
}),
@@ -586,8 +582,6 @@
'title': 'Id',
}),
'invalid_tool_calls': dict({
- 'default': list([
- ]),
'items': dict({
'$ref': '#/$defs/InvalidToolCall',
}),
@@ -611,8 +605,6 @@
'type': 'object',
}),
'tool_call_chunks': dict({
- 'default': list([
- ]),
'items': dict({
'$ref': '#/$defs/ToolCallChunk',
}),
@@ -620,8 +612,6 @@
'type': 'array',
}),
'tool_calls': dict({
- 'default': list([
- ]),
'items': dict({
'$ref': '#/$defs/ToolCall',
}),
diff --git a/libs/core/tests/unit_tests/runnables/__snapshots__/test_runnable.ambr b/libs/core/tests/unit_tests/runnables/__snapshots__/test_runnable.ambr
index c6201697c6..da03e50520 100644
--- a/libs/core/tests/unit_tests/runnables/__snapshots__/test_runnable.ambr
+++ b/libs/core/tests/unit_tests/runnables/__snapshots__/test_runnable.ambr
@@ -2007,8 +2007,6 @@
'title': 'Id',
}),
'invalid_tool_calls': dict({
- 'default': list([
- ]),
'items': dict({
'$ref': '#/$defs/InvalidToolCall',
}),
@@ -2032,8 +2030,6 @@
'type': 'object',
}),
'tool_calls': dict({
- 'default': list([
- ]),
'items': dict({
'$ref': '#/$defs/ToolCall',
}),
@@ -2116,8 +2112,6 @@
'title': 'Id',
}),
'invalid_tool_calls': dict({
- 'default': list([
- ]),
'items': dict({
'$ref': '#/$defs/InvalidToolCall',
}),
@@ -2141,8 +2135,6 @@
'type': 'object',
}),
'tool_call_chunks': dict({
- 'default': list([
- ]),
'items': dict({
'$ref': '#/$defs/ToolCallChunk',
}),
@@ -2150,8 +2142,6 @@
'type': 'array',
}),
'tool_calls': dict({
- 'default': list([
- ]),
'items': dict({
'$ref': '#/$defs/ToolCall',
}),
@@ -3405,8 +3395,6 @@
'title': 'Id',
}),
'invalid_tool_calls': dict({
- 'default': list([
- ]),
'items': dict({
'$ref': '#/$defs/InvalidToolCall',
}),
@@ -3430,8 +3418,6 @@
'type': 'object',
}),
'tool_calls': dict({
- 'default': list([
- ]),
'items': dict({
'$ref': '#/$defs/ToolCall',
}),
@@ -3514,8 +3500,6 @@
'title': 'Id',
}),
'invalid_tool_calls': dict({
- 'default': list([
- ]),
'items': dict({
'$ref': '#/$defs/InvalidToolCall',
}),
@@ -3539,8 +3523,6 @@
'type': 'object',
}),
'tool_call_chunks': dict({
- 'default': list([
- ]),
'items': dict({
'$ref': '#/$defs/ToolCallChunk',
}),
@@ -3548,8 +3530,6 @@
'type': 'array',
}),
'tool_calls': dict({
- 'default': list([
- ]),
'items': dict({
'$ref': '#/$defs/ToolCall',
}),
@@ -4897,8 +4877,6 @@
'title': 'Id',
}),
'invalid_tool_calls': dict({
- 'default': list([
- ]),
'items': dict({
'$ref': '#/definitions/InvalidToolCall',
}),
@@ -4922,8 +4900,6 @@
'type': 'object',
}),
'tool_calls': dict({
- 'default': list([
- ]),
'items': dict({
'$ref': '#/definitions/ToolCall',
}),
@@ -5006,8 +4982,6 @@
'title': 'Id',
}),
'invalid_tool_calls': dict({
- 'default': list([
- ]),
'items': dict({
'$ref': '#/definitions/InvalidToolCall',
}),
@@ -5031,8 +5005,6 @@
'type': 'object',
}),
'tool_call_chunks': dict({
- 'default': list([
- ]),
'items': dict({
'$ref': '#/definitions/ToolCallChunk',
}),
@@ -5040,8 +5012,6 @@
'type': 'array',
}),
'tool_calls': dict({
- 'default': list([
- ]),
'items': dict({
'$ref': '#/definitions/ToolCall',
}),
@@ -6327,8 +6297,6 @@
'title': 'Id',
}),
'invalid_tool_calls': dict({
- 'default': list([
- ]),
'items': dict({
'$ref': '#/definitions/InvalidToolCall',
}),
@@ -6352,8 +6320,6 @@
'type': 'object',
}),
'tool_calls': dict({
- 'default': list([
- ]),
'items': dict({
'$ref': '#/definitions/ToolCall',
}),
@@ -6436,8 +6402,6 @@
'title': 'Id',
}),
'invalid_tool_calls': dict({
- 'default': list([
- ]),
'items': dict({
'$ref': '#/definitions/InvalidToolCall',
}),
@@ -6461,8 +6425,6 @@
'type': 'object',
}),
'tool_call_chunks': dict({
- 'default': list([
- ]),
'items': dict({
'$ref': '#/definitions/ToolCallChunk',
}),
@@ -6470,8 +6432,6 @@
'type': 'array',
}),
'tool_calls': dict({
- 'default': list([
- ]),
'items': dict({
'$ref': '#/definitions/ToolCall',
}),
@@ -7767,8 +7727,6 @@
'title': 'Id',
}),
'invalid_tool_calls': dict({
- 'default': list([
- ]),
'items': dict({
'$ref': '#/definitions/InvalidToolCall',
}),
@@ -7792,8 +7750,6 @@
'type': 'object',
}),
'tool_calls': dict({
- 'default': list([
- ]),
'items': dict({
'$ref': '#/definitions/ToolCall',
}),
@@ -7876,8 +7832,6 @@
'title': 'Id',
}),
'invalid_tool_calls': dict({
- 'default': list([
- ]),
'items': dict({
'$ref': '#/definitions/InvalidToolCall',
}),
@@ -7901,8 +7855,6 @@
'type': 'object',
}),
'tool_call_chunks': dict({
- 'default': list([
- ]),
'items': dict({
'$ref': '#/definitions/ToolCallChunk',
}),
@@ -7910,8 +7862,6 @@
'type': 'array',
}),
'tool_calls': dict({
- 'default': list([
- ]),
'items': dict({
'$ref': '#/definitions/ToolCall',
}),
@@ -9242,8 +9192,6 @@
'title': 'Id',
}),
'invalid_tool_calls': dict({
- 'default': list([
- ]),
'items': dict({
'$ref': '#/definitions/InvalidToolCall',
}),
@@ -9267,8 +9215,6 @@
'type': 'object',
}),
'tool_calls': dict({
- 'default': list([
- ]),
'items': dict({
'$ref': '#/definitions/ToolCall',
}),
@@ -9351,8 +9297,6 @@
'title': 'Id',
}),
'invalid_tool_calls': dict({
- 'default': list([
- ]),
'items': dict({
'$ref': '#/definitions/InvalidToolCall',
}),
@@ -9376,8 +9320,6 @@
'type': 'object',
}),
'tool_call_chunks': dict({
- 'default': list([
- ]),
'items': dict({
'$ref': '#/definitions/ToolCallChunk',
}),
@@ -9385,8 +9327,6 @@
'type': 'array',
}),
'tool_calls': dict({
- 'default': list([
- ]),
'items': dict({
'$ref': '#/definitions/ToolCall',
}),
@@ -10590,8 +10530,6 @@
'title': 'Id',
}),
'invalid_tool_calls': dict({
- 'default': list([
- ]),
'items': dict({
'$ref': '#/definitions/InvalidToolCall',
}),
@@ -10615,8 +10553,6 @@
'type': 'object',
}),
'tool_calls': dict({
- 'default': list([
- ]),
'items': dict({
'$ref': '#/definitions/ToolCall',
}),
@@ -10699,8 +10635,6 @@
'title': 'Id',
}),
'invalid_tool_calls': dict({
- 'default': list([
- ]),
'items': dict({
'$ref': '#/definitions/InvalidToolCall',
}),
@@ -10724,8 +10658,6 @@
'type': 'object',
}),
'tool_call_chunks': dict({
- 'default': list([
- ]),
'items': dict({
'$ref': '#/definitions/ToolCallChunk',
}),
@@ -10733,8 +10665,6 @@
'type': 'array',
}),
'tool_calls': dict({
- 'default': list([
- ]),
'items': dict({
'$ref': '#/definitions/ToolCall',
}),
@@ -12043,8 +11973,6 @@
'title': 'Id',
}),
'invalid_tool_calls': dict({
- 'default': list([
- ]),
'items': dict({
'$ref': '#/definitions/InvalidToolCall',
}),
@@ -12068,8 +11996,6 @@
'type': 'object',
}),
'tool_calls': dict({
- 'default': list([
- ]),
'items': dict({
'$ref': '#/definitions/ToolCall',
}),
@@ -12152,8 +12078,6 @@
'title': 'Id',
}),
'invalid_tool_calls': dict({
- 'default': list([
- ]),
'items': dict({
'$ref': '#/definitions/InvalidToolCall',
}),
@@ -12177,8 +12101,6 @@
'type': 'object',
}),
'tool_call_chunks': dict({
- 'default': list([
- ]),
'items': dict({
'$ref': '#/definitions/ToolCallChunk',
}),
@@ -12186,8 +12108,6 @@
'type': 'array',
}),
'tool_calls': dict({
- 'default': list([
- ]),
'items': dict({
'$ref': '#/definitions/ToolCall',
}),
diff --git a/libs/core/tests/unit_tests/test_tools.py b/libs/core/tests/unit_tests/test_tools.py
index 0c62c2d9fa..6d65704ed4 100644
--- a/libs/core/tests/unit_tests/test_tools.py
+++ b/libs/core/tests/unit_tests/test_tools.py
@@ -2943,7 +2943,7 @@ def test_tool_args_schema_with_annotated_type() -> None:
class CallbackHandlerWithInputCapture(FakeCallbackHandler):
"""Callback handler that captures inputs passed to on_tool_start."""
- captured_inputs: list[dict | None] = []
+ captured_inputs: list[dict | None] = Field(default_factory=list)
def on_tool_start(
self,
@@ -3158,19 +3158,18 @@ def test_filter_tool_runtime_directly_injected_arg() -> None:
handler = CallbackHandlerWithInputCapture(captured_inputs=[])
- # Create a mock ToolRuntime instance
- class MockRuntime:
- """Mock ToolRuntime for testing."""
-
- agent_name = "test_agent"
- context: dict[str, Any] = {}
- state: dict[str, Any] = {}
-
result = tool_with_runtime.invoke(
{
"query": "test",
"limit": 5,
- "runtime": MockRuntime(),
+ "runtime": ToolRuntime(
+ context={},
+ state={},
+ config={},
+ stream_writer=lambda _: None,
+ tool_call_id=None,
+ store=None,
+ ),
},
config={"callbacks": [handler]},
)
@@ -3193,7 +3192,7 @@ class CallbackHandlerWithToolCallIdCapture(FakeCallbackHandler):
callback method.
"""
- captured_tool_call_ids: list[str | None] = []
+ captured_tool_call_ids: list[str | None] = Field(default_factory=list)
def on_tool_start(
self,