From 6a416c618655102847a59cae280348c0712cf45c Mon Sep 17 00:00:00 2001 From: Christophe Bornet Date: Mon, 22 Dec 2025 02:31:50 +0100 Subject: [PATCH] style(langchain): add ruff rules PT (#34434) --- libs/langchain_v1/pyproject.toml | 1 - .../test_shell_execution_policies.py | 21 ++++++----- .../implementations/test_summarization.py | 12 ++++-- .../middleware/implementations/test_todo.py | 4 +- .../unit_tests/agents/test_system_message.py | 37 ++++++++++--------- 5 files changed, 41 insertions(+), 34 deletions(-) diff --git a/libs/langchain_v1/pyproject.toml b/libs/langchain_v1/pyproject.toml index 5068291e1c6..8ef821f4f5a 100644 --- a/libs/langchain_v1/pyproject.toml +++ b/libs/langchain_v1/pyproject.toml @@ -141,7 +141,6 @@ ignore-var-parameters = true # ignore missing documentation for *args and **kwa "tests/unit_tests/agents/*" = [ "ANN", # Annotations, needs to fix "ARG", # Arguments, needs to fix - "PT", # pytest, needs to fix ] "tests/unit_tests/agents/test_return_direct_spec.py" = ["F821"] "tests/unit_tests/agents/test_responses_spec.py" = ["F821"] diff --git a/libs/langchain_v1/tests/unit_tests/agents/middleware/implementations/test_shell_execution_policies.py b/libs/langchain_v1/tests/unit_tests/agents/middleware/implementations/test_shell_execution_policies.py index 4425d99db39..7e13cf73d25 100644 --- a/libs/langchain_v1/tests/unit_tests/agents/middleware/implementations/test_shell_execution_policies.py +++ b/libs/langchain_v1/tests/unit_tests/agents/middleware/implementations/test_shell_execution_policies.py @@ -51,13 +51,13 @@ def _make_resource( def test_host_policy_validations() -> None: - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="max_output_lines must be positive"): HostExecutionPolicy(max_output_lines=0) - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="cpu_time_seconds must be positive if provided"): HostExecutionPolicy(cpu_time_seconds=0) - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="memory_bytes must be positive if provided"): HostExecutionPolicy(memory_bytes=-1) @@ -310,11 +310,13 @@ def test_docker_policy_spawns_docker_run(monkeypatch, tmp_path: Path) -> None: assert command[1:4] == ["run", "-i", "--rm"] assert "--memory" in command assert "4096" in command - assert "-v" in command and any(str(tmp_path) in part for part in command) + assert "-v" in command + assert any(str(tmp_path) in part for part in command) assert "-w" in command w_index = command.index("-w") assert command[w_index + 1] == str(tmp_path) - assert "-e" in command and "PATH=/bin" in command + assert "-e" in command + assert "PATH=/bin" in command assert command[-2:] == ["ubuntu:22.04", "/bin/bash"] @@ -324,7 +326,7 @@ def test_docker_policy_rejects_cpu_limit() -> None: def test_docker_policy_validates_memory() -> None: - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="memory_bytes must be positive if provided"): DockerExecutionPolicy(memory_bytes=0) @@ -357,17 +359,18 @@ def test_docker_policy_skips_mount_for_temp_workspace( w_index = command.index("-w") assert command[w_index + 1] == "/" assert "--cpus" in command - assert "--network" in command and "none" in command + assert "--network" in command + assert "none" in command assert command[-2:] == [policy.image, "/bin/sh"] def test_docker_policy_validates_cpus() -> None: - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="cpus must be a non-empty string when provided"): DockerExecutionPolicy(cpus=" ") def test_docker_policy_validates_user() -> None: - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="user must be a non-empty string when provided"): DockerExecutionPolicy(user=" ") diff --git a/libs/langchain_v1/tests/unit_tests/agents/middleware/implementations/test_summarization.py b/libs/langchain_v1/tests/unit_tests/agents/middleware/implementations/test_summarization.py index 5e8c007d14a..d501f382011 100644 --- a/libs/langchain_v1/tests/unit_tests/agents/middleware/implementations/test_summarization.py +++ b/libs/langchain_v1/tests/unit_tests/agents/middleware/implementations/test_summarization.py @@ -54,7 +54,11 @@ def test_summarization_middleware_initialization() -> None: assert middleware.summary_prompt == "Custom prompt: {messages}" assert middleware.trim_tokens_to_summarize == 4000 - with pytest.raises(ValueError): + with pytest.raises( + ValueError, + match="Model profile information is required to use fractional token limits, " + "and is unavailable for the specified model", + ): SummarizationMiddleware(model=model, keep=("fraction", 0.5)) # no model profile # Test with string model @@ -346,7 +350,7 @@ def test_summarization_middleware_missing_profile() -> None: def test_summarization_middleware_full_workflow() -> None: """Test SummarizationMiddleware complete summarization workflow.""" - with pytest.warns(DeprecationWarning): + with pytest.warns(DeprecationWarning, match="messages_to_keep is deprecated"): # keep test for functionality middleware = SummarizationMiddleware( model=MockChatModel(), max_tokens_before_summary=1000, messages_to_keep=2 @@ -753,14 +757,14 @@ def test_summarization_middleware_cutoff_at_boundary() -> None: def test_summarization_middleware_deprecated_parameters_with_defaults() -> None: """Test that deprecated parameters work correctly with default values.""" # Test that deprecated max_tokens_before_summary is ignored when trigger is set - with pytest.warns(DeprecationWarning): + with pytest.warns(DeprecationWarning, match="max_tokens_before_summary is deprecated"): middleware = SummarizationMiddleware( model=MockChatModel(), trigger=("tokens", 2000), max_tokens_before_summary=1000 ) assert middleware.trigger == ("tokens", 2000) # Test that messages_to_keep is ignored when keep is not default - with pytest.warns(DeprecationWarning): + with pytest.warns(DeprecationWarning, match="messages_to_keep is deprecated"): middleware = SummarizationMiddleware( model=MockChatModel(), keep=("messages", 5), messages_to_keep=10 ) diff --git a/libs/langchain_v1/tests/unit_tests/agents/middleware/implementations/test_todo.py b/libs/langchain_v1/tests/unit_tests/agents/middleware/implementations/test_todo.py index aa0d71e706e..a3c2458a9d8 100644 --- a/libs/langchain_v1/tests/unit_tests/agents/middleware/implementations/test_todo.py +++ b/libs/langchain_v1/tests/unit_tests/agents/middleware/implementations/test_todo.py @@ -125,7 +125,7 @@ def test_appends_to_existing_system_prompt() -> None: @pytest.mark.parametrize( - "original_prompt,expected_prompt_prefix", + ("original_prompt", "expected_prompt_prefix"), [ ("Original prompt", "Original prompt\n\n## `write_todos`"), (None, "## `write_todos`"), @@ -291,7 +291,7 @@ def test_todo_middleware_custom_system_prompt_and_tool_description() -> None: @pytest.mark.parametrize( - "todos,expected_message", + ("todos", "expected_message"), [ ([], "Updated todo list to []"), ( diff --git a/libs/langchain_v1/tests/unit_tests/agents/test_system_message.py b/libs/langchain_v1/tests/unit_tests/agents/test_system_message.py index 532f465b18a..f12027935ca 100644 --- a/libs/langchain_v1/tests/unit_tests/agents/test_system_message.py +++ b/libs/langchain_v1/tests/unit_tests/agents/test_system_message.py @@ -65,7 +65,7 @@ class TestModelRequestSystemMessage: """Test ModelRequest with system_message field.""" @pytest.mark.parametrize( - "system_message,system_prompt,expected_msg,expected_prompt", + ("system_message", "system_prompt", "expected_msg", "expected_prompt"), [ # Test with SystemMessage ( @@ -124,7 +124,7 @@ class TestModelRequestSystemMessage: assert "Part 1" in request.system_prompt @pytest.mark.parametrize( - "override_with,expected_text", + ("override_with", "expected_text"), [ ("system_message", "New"), ("system_prompt", "New prompt"), @@ -184,8 +184,8 @@ class TestModelRequestSystemMessage: """Test that setting both system_prompt and system_message raises error.""" model = GenericFakeChatModel(messages=iter([AIMessage(content="Hello")])) - with pytest.raises(ValueError, match="Cannot specify both"): - if use_constructor: + if use_constructor: + with pytest.raises(ValueError, match="Cannot specify both"): ModelRequest( model=model, system_prompt="String prompt", @@ -197,24 +197,25 @@ class TestModelRequestSystemMessage: state={}, runtime=None, ) - else: - request = ModelRequest( - model=model, - system_message=None, - messages=[], - tool_choice=None, - tools=[], - response_format=None, - state={}, - runtime=None, - ) + else: + request = ModelRequest( + model=model, + system_message=None, + messages=[], + tool_choice=None, + tools=[], + response_format=None, + state={}, + runtime=None, + ) + with pytest.raises(ValueError, match="Cannot specify both"): request.override( system_prompt="String prompt", system_message=SystemMessage(content="Message prompt"), ) @pytest.mark.parametrize( - "new_value,should_be_none", + ("new_value", "should_be_none"), [ ("New prompt", False), (None, True), @@ -651,7 +652,7 @@ class TestMetadataMerging: """Test metadata merging behavior when updating system messages.""" @pytest.mark.parametrize( - "metadata_type,initial_metadata,update_metadata,expected_result", + ("metadata_type", "initial_metadata", "update_metadata", "expected_result"), [ # additional_kwargs merging ( @@ -948,7 +949,7 @@ class TestEdgeCasesAndErrorHandling: """Test edge cases and error handling for system messages.""" @pytest.mark.parametrize( - "content,expected_blocks,expected_prompt", + ("content", "expected_blocks", "expected_prompt"), [ ("", 0, ""), (