Skip to content

The agent engineering platform.

langchain-anthropic[patch]: Add ruff bandit rules (#31789)

Mason Daugherty645e25f624c5
diff --git a/libs/partners/anthropic/langchain_anthropic/_client_utils.py b/libs/partners/anthropic/langchain_anthropic/_client_utils.py
index b98b9e1643..a82e5655a9 100644
--- a/libs/partners/anthropic/langchain_anthropic/_client_utils.py
+++ b/libs/partners/anthropic/langchain_anthropic/_client_utils.py
@@ -25,7 +25,7 @@ class _SyncHttpxClientWrapper(anthropic.DefaultHttpxClient):
try:
self.close()
- except Exception:
+ except Exception: # noqa: S110
pass
@@ -39,7 +39,7 @@ class _AsyncHttpxClientWrapper(anthropic.DefaultAsyncHttpxClient):
try:
# TODO(someday): support non asyncio runtimes here
asyncio.get_running_loop().create_task(self.aclose())
- except Exception:
+ except Exception: # noqa: S110
pass
diff --git a/libs/partners/anthropic/langchain_anthropic/chat_models.py b/libs/partners/anthropic/langchain_anthropic/chat_models.py
index e30bf35a43..44165a68c3 100644
--- a/libs/partners/anthropic/langchain_anthropic/chat_models.py
+++ b/libs/partners/anthropic/langchain_anthropic/chat_models.py
@@ -327,9 +327,10 @@ def _format_messages(
if not isinstance(message.content, str):
# parse as dict
- assert isinstance(
- message.content, list
- ), "Anthropic message content must be str or list of dicts"
+ if not isinstance(message.content, list):
+ raise ValueError(
+ "Anthropic message content must be str or list of dicts"
+ )
# populate content
content = []
diff --git a/libs/partners/anthropic/pyproject.toml b/libs/partners/anthropic/pyproject.toml
index cfecaeefdd..16c528bd08 100644
--- a/libs/partners/anthropic/pyproject.toml
+++ b/libs/partners/anthropic/pyproject.toml
@@ -60,7 +60,7 @@ plugins = ['pydantic.mypy']
target-version = "py39"
[tool.ruff.lint]
-select = ["E", "F", "I", "T201", "UP"]
+select = ["E", "F", "I", "T201", "UP", "S"]
ignore = [ "UP007", ]
[tool.coverage.run]
@@ -73,3 +73,9 @@ markers = [
"compile: mark placeholder test used to compile integration tests without running them",
]
asyncio_mode = "auto"
+
+[tool.ruff.lint.extend-per-file-ignores]
+"tests/**/*.py" = [
+ "S101", # Tests need assertions
+ "S311", # Standard pseudo-random generators are not suitable for cryptographic purposes
+]
diff --git a/libs/partners/anthropic/tests/integration_tests/test_chat_models.py b/libs/partners/anthropic/tests/integration_tests/test_chat_models.py
index 93550c1fb5..1192336d47 100644
--- a/libs/partners/anthropic/tests/integration_tests/test_chat_models.py
+++ b/libs/partners/anthropic/tests/integration_tests/test_chat_models.py
@@ -651,7 +651,7 @@ def test_anthropic_bind_tools_tool_choice(tool_choice: str) -> None:
def test_pdf_document_input() -> None:
url = "https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf"
- data = b64encode(requests.get(url).content).decode()
+ data = b64encode(requests.get(url, timeout=10).content).decode()
result = ChatAnthropic(model=IMAGE_MODEL_NAME).invoke(
[
diff --git a/libs/partners/anthropic/tests/unit_tests/test_chat_models.py b/libs/partners/anthropic/tests/unit_tests/test_chat_models.py
index b0b0cc82b0..2ca6b6cdf1 100644
--- a/libs/partners/anthropic/tests/unit_tests/test_chat_models.py
+++ b/libs/partners/anthropic/tests/unit_tests/test_chat_models.py
@@ -1071,4 +1071,4 @@ def test_mcp_tracing() -> None:
# Test headers are correctly propagated to request
payload = llm._get_request_payload([input_message])
- assert payload["mcp_servers"][0]["authorization_token"] == "PLACEHOLDER"
+ assert payload["mcp_servers"][0]["authorization_token"] == "PLACEHOLDER" # noqa: S105