Skip to content

The agent engineering platform.

chore(standard-tests): enable mypy disallow_any_generics rule (#34222)

Christophe Bornetfc35544e0d8a
diff --git a/libs/standard-tests/langchain_tests/conftest.py b/libs/standard-tests/langchain_tests/conftest.py
index bfee299005..96ff7bf741 100644
--- a/libs/standard-tests/langchain_tests/conftest.py
+++ b/libs/standard-tests/langchain_tests/conftest.py
@@ -29,7 +29,7 @@ class CustomSerializer:
"""
@staticmethod
- def serialize(cassette_dict: dict) -> bytes:
+ def serialize(cassette_dict: dict[str, Any]) -> bytes:
"""Convert cassette to YAML and compress it."""
cassette_dict["requests"] = [
{
@@ -44,7 +44,7 @@ class CustomSerializer:
return gzip.compress(yml.encode("utf-8"))
@staticmethod
- def deserialize(data: bytes) -> dict:
+ def deserialize(data: bytes) -> dict[str, Any]:
"""Decompress data and convert it from YAML."""
decoded_yaml = gzip.decompress(data).decode("utf-8")
cassette = cast("dict[str, Any]", yaml.safe_load(decoded_yaml))
@@ -60,7 +60,7 @@ class CustomPersister:
cls,
cassette_path: str | PathLike[str],
serializer: CustomSerializer,
- ) -> tuple[dict, dict]:
+ ) -> tuple[list[Any], list[Any]]:
"""Load a cassette from a file."""
# If cassette path is already Path this is a no-op
cassette_path = Path(cassette_path)
@@ -75,7 +75,7 @@ class CustomPersister:
@staticmethod
def save_cassette(
cassette_path: str | PathLike[str],
- cassette_dict: dict,
+ cassette_dict: dict[str, Any],
serializer: CustomSerializer,
) -> None:
"""Save a cassette to a file."""
@@ -99,7 +99,7 @@ _BASE_FILTER_HEADERS = [
]
-def base_vcr_config() -> dict:
+def base_vcr_config() -> dict[str, Any]:
"""Return VCR configuration that every cassette will receive.
(Anything permitted by `vcr.VCR(**kwargs)` can be put here.)
@@ -117,11 +117,11 @@ def base_vcr_config() -> dict:
@pytest.fixture(scope="session")
@deprecated("1.0.3", alternative="base_vcr_config", removal="2.0")
-def _base_vcr_config() -> dict:
+def _base_vcr_config() -> dict[str, Any]:
return base_vcr_config()
@pytest.fixture(scope="session")
-def vcr_config() -> dict:
+def vcr_config() -> dict[str, Any]:
"""VCR config fixture."""
return base_vcr_config()
diff --git a/libs/standard-tests/langchain_tests/integration_tests/chat_models.py b/libs/standard-tests/langchain_tests/integration_tests/chat_models.py
index 06d42ef6b0..05663bf90c 100644
--- a/libs/standard-tests/langchain_tests/integration_tests/chat_models.py
+++ b/libs/standard-tests/langchain_tests/integration_tests/chat_models.py
@@ -75,7 +75,7 @@ def _get_joke_class( # noqa: RET503
class _TestCallbackHandler(BaseCallbackHandler):
- options: list[dict | None]
+ options: list[dict[str, Any] | None]
def __init__(self) -> None:
super().__init__()
@@ -739,7 +739,7 @@ class ChatModelIntegrationTests(ChatModelTests):
''' # noqa: E501
@property
- def standard_chat_model_params(self) -> dict:
+ def standard_chat_model_params(self) -> dict[str, Any]:
"""Standard parameters for chat model."""
return {}
@@ -3072,7 +3072,7 @@ class ChatModelIntegrationTests(ChatModelTests):
"cache_control": {"type": "ephemeral"},
}
- human_content: list[dict] = [
+ human_content = [
{
"type": "text",
"text": "what's your favorite color in this image",
diff --git a/libs/standard-tests/langchain_tests/integration_tests/retrievers.py b/libs/standard-tests/langchain_tests/integration_tests/retrievers.py
index 00ed2b0414..636a5008ce 100644
--- a/libs/standard-tests/langchain_tests/integration_tests/retrievers.py
+++ b/libs/standard-tests/langchain_tests/integration_tests/retrievers.py
@@ -1,6 +1,7 @@
"""Integration tests for retrievers."""
from abc import abstractmethod
+from typing import Any
import pytest
from langchain_core.documents import Document
@@ -19,7 +20,7 @@ class RetrieversIntegrationTests(BaseStandardTests):
...
@property
- def retriever_constructor_params(self) -> dict:
+ def retriever_constructor_params(self) -> dict[str, Any]:
"""Returns a dictionary of parameters to pass to the retriever constructor."""
return {}
diff --git a/libs/standard-tests/langchain_tests/unit_tests/chat_models.py b/libs/standard-tests/langchain_tests/unit_tests/chat_models.py
index bfec8d53a1..b3b0a24a7d 100644
--- a/libs/standard-tests/langchain_tests/unit_tests/chat_models.py
+++ b/libs/standard-tests/langchain_tests/unit_tests/chat_models.py
@@ -49,12 +49,12 @@ class ChatModelTests(BaseStandardTests):
...
@property
- def chat_model_params(self) -> dict:
+ def chat_model_params(self) -> dict[str, Any]:
"""Initialization parameters for the chat model."""
return {}
@property
- def standard_chat_model_params(self) -> dict:
+ def standard_chat_model_params(self) -> dict[str, Any]:
"""Standard chat model parameters."""
return {
"temperature": 0,
@@ -112,7 +112,7 @@ class ChatModelTests(BaseStandardTests):
) or self.has_tool_calling
@property
- def structured_output_kwargs(self) -> dict:
+ def structured_output_kwargs(self) -> dict[str, Any]:
"""Additional kwargs to pass to `with_structured_output()` in tests.
Override this property to customize how structured output is generated
@@ -902,14 +902,16 @@ class ChatModelUnitTests(ChatModelTests):
''' # noqa: E501,D214
@property
- def standard_chat_model_params(self) -> dict:
+ def standard_chat_model_params(self) -> dict[str, Any]:
"""Standard chat model parameters."""
params = super().standard_chat_model_params
params["api_key"] = "test"
return params
@property
- def init_from_env_params(self) -> tuple[dict, dict, dict]:
+ def init_from_env_params(
+ self,
+ ) -> tuple[dict[str, str], dict[str, Any], dict[str, Any]]:
"""Init from env params.
Environment variables, additional initialization args, and expected instance
diff --git a/libs/standard-tests/langchain_tests/unit_tests/embeddings.py b/libs/standard-tests/langchain_tests/unit_tests/embeddings.py
index c8901ab55b..34ae029b29 100644
--- a/libs/standard-tests/langchain_tests/unit_tests/embeddings.py
+++ b/libs/standard-tests/langchain_tests/unit_tests/embeddings.py
@@ -2,6 +2,7 @@
import os
from abc import abstractmethod
+from typing import Any
from unittest import mock
import pytest
@@ -20,7 +21,7 @@ class EmbeddingsTests(BaseStandardTests):
"""Embeddings class."""
@property
- def embedding_model_params(self) -> dict:
+ def embedding_model_params(self) -> dict[str, Any]:
"""Embeddings model parameters."""
return {}
@@ -100,7 +101,9 @@ class EmbeddingsUnitTests(EmbeddingsTests):
assert model is not None
@property
- def init_from_env_params(self) -> tuple[dict, dict, dict]:
+ def init_from_env_params(
+ self,
+ ) -> tuple[dict[str, str], dict[str, Any], dict[str, Any]]:
"""Init from env params.
This property is used in unit tests to test initialization from environment
diff --git a/libs/standard-tests/langchain_tests/unit_tests/tools.py b/libs/standard-tests/langchain_tests/unit_tests/tools.py
index 530ed03e8c..24f7548a9e 100644
--- a/libs/standard-tests/langchain_tests/unit_tests/tools.py
+++ b/libs/standard-tests/langchain_tests/unit_tests/tools.py
@@ -4,6 +4,7 @@ from __future__ import annotations
import os
from abc import abstractmethod
+from typing import Any
from unittest import mock
import pytest
@@ -27,12 +28,12 @@ class ToolsTests(BaseStandardTests):
...
@property
- def tool_constructor_params(self) -> dict:
+ def tool_constructor_params(self) -> dict[str, Any]:
"""Returns a dictionary of parameters to pass to the tool constructor."""
return {}
@property
- def tool_invoke_params_example(self) -> dict:
+ def tool_invoke_params_example(self) -> dict[str, Any]:
"""Returns a dictionary representing the "args" of an example tool call.
This should NOT be a `ToolCall` dict - it should not have
@@ -58,7 +59,9 @@ class ToolsUnitTests(ToolsTests):
"""Base class for tools unit tests."""
@property
- def init_from_env_params(self) -> tuple[dict, dict, dict]:
+ def init_from_env_params(
+ self,
+ ) -> tuple[dict[str, str], dict[str, Any], dict[str, Any]]:
"""Init from env params.
Return env vars, init args, and expected instance attrs for initializing
diff --git a/libs/standard-tests/pyproject.toml b/libs/standard-tests/pyproject.toml
index dddecbc0e4..6390daf40c 100644
--- a/libs/standard-tests/pyproject.toml
+++ b/libs/standard-tests/pyproject.toml
@@ -54,9 +54,6 @@ strict = true
enable_error_code = "deprecated"
warn_unreachable = true
-# TODO: activate for 'strict' checking
-disallow_any_generics = false
-
[[tool.mypy.overrides]]
module = ["vcr.*",]
ignore_missing_imports = true
@@ -97,7 +94,6 @@ flake8-annotations.allow-star-arg-any = true
flake8-annotations.mypy-init-return = true
flake8-type-checking.runtime-evaluated-base-classes = ["pydantic.BaseModel","langchain_core.load.serializable.Serializable","langchain_core.runnables.base.RunnableSerializable"]
pep8-naming.classmethod-decorators = [ "classmethod", "langchain_core.utils.pydantic.pre_init", "pydantic.field_validator", "pydantic.v1.root_validator",]
-pyupgrade.keep-runtime-typing = true
[tool.ruff.lint.pydocstyle]
convention = "google"
diff --git a/libs/standard-tests/tests/unit_tests/test_basic_retriever.py b/libs/standard-tests/tests/unit_tests/test_basic_retriever.py
index ccff1e43e1..847a137fc3 100644
--- a/libs/standard-tests/tests/unit_tests/test_basic_retriever.py
+++ b/libs/standard-tests/tests/unit_tests/test_basic_retriever.py
@@ -21,7 +21,7 @@ class TestParrotRetrieverIntegration(RetrieversIntegrationTests):
return ParrotRetriever
@property
- def retriever_constructor_params(self) -> dict:
+ def retriever_constructor_params(self) -> dict[str, Any]:
return {"parrot_name": "Polly"}
@property
diff --git a/libs/standard-tests/tests/unit_tests/test_basic_tool.py b/libs/standard-tests/tests/unit_tests/test_basic_tool.py
index d7ab342f04..b641b55f8a 100644
--- a/libs/standard-tests/tests/unit_tests/test_basic_tool.py
+++ b/libs/standard-tests/tests/unit_tests/test_basic_tool.py
@@ -1,4 +1,4 @@
-from typing import Literal
+from typing import Any, Literal
from langchain_core.tools import BaseTool
from typing_extensions import override
@@ -36,14 +36,14 @@ class TestParrotMultiplyToolUnit(ToolsUnitTests):
return ParrotMultiplyTool
@property
- def tool_constructor_params(self) -> dict:
+ def tool_constructor_params(self) -> dict[str, Any]:
# if your tool constructor instead required initialization arguments like
# `def __init__(self, some_arg: int):`, you would return those here
# as a dictionary, e.g.: `return {'some_arg': 42}`
return {}
@property
- def tool_invoke_params_example(self) -> dict:
+ def tool_invoke_params_example(self) -> dict[str, Any]:
"""Returns a dictionary representing the "args" of an example tool call.
This should NOT be a ToolCall dict - i.e. it should not
@@ -58,14 +58,14 @@ class TestParrotMultiplyToolIntegration(ToolsIntegrationTests):
return ParrotMultiplyTool
@property
- def tool_constructor_params(self) -> dict:
+ def tool_constructor_params(self) -> dict[str, Any]:
# if your tool constructor instead required initialization arguments like
# `def __init__(self, some_arg: int):`, you would return those here
# as a dictionary, e.g.: `return {'some_arg': 42}`
return {}
@property
- def tool_invoke_params_example(self) -> dict:
+ def tool_invoke_params_example(self) -> dict[str, Any]:
"""Returns a dictionary representing the "args" of an example tool call.
This should NOT be a ToolCall dict - i.e. it should not
@@ -80,14 +80,14 @@ class TestParrotMultiplyArtifactToolIntegration(ToolsIntegrationTests):
return ParrotMultiplyArtifactTool
@property
- def tool_constructor_params(self) -> dict:
+ def tool_constructor_params(self) -> dict[str, Any]:
# if your tool constructor instead required initialization arguments like
# `def __init__(self, some_arg: int):`, you would return those here
# as a dictionary, e.g.: `return {'some_arg': 42}`
return {}
@property
- def tool_invoke_params_example(self) -> dict:
+ def tool_invoke_params_example(self) -> dict[str, Any]:
"""Returns a dictionary representing the "args" of an example tool call.
This should NOT be a ToolCall dict - i.e. it should not
diff --git a/libs/standard-tests/tests/unit_tests/test_custom_chat_model.py b/libs/standard-tests/tests/unit_tests/test_custom_chat_model.py
index eea5550ee6..c14b2ac848 100644
--- a/libs/standard-tests/tests/unit_tests/test_custom_chat_model.py
+++ b/libs/standard-tests/tests/unit_tests/test_custom_chat_model.py
@@ -2,7 +2,7 @@
from __future__ import annotations
-from typing import TYPE_CHECKING
+from typing import TYPE_CHECKING, Any
import pytest
@@ -21,7 +21,7 @@ class TestChatParrotLinkUnit(ChatModelUnitTests):
return ChatParrotLink
@property
- def chat_model_params(self) -> dict:
+ def chat_model_params(self) -> dict[str, Any]:
return {"model": "bird-brain-001", "temperature": 0, "parrot_buffer_length": 50}
@@ -31,7 +31,7 @@ class TestChatParrotLinkIntegration(ChatModelIntegrationTests):
return ChatParrotLink
@property
- def chat_model_params(self) -> dict:
+ def chat_model_params(self) -> dict[str, Any]:
return {"model": "bird-brain-001", "temperature": 0, "parrot_buffer_length": 50}
@pytest.mark.xfail(reason="ChatParrotLink doesn't implement bind_tools method")
diff --git a/libs/standard-tests/tests/unit_tests/test_decorated_tool.py b/libs/standard-tests/tests/unit_tests/test_decorated_tool.py
index c3b7b89d04..a559fe6f98 100644
--- a/libs/standard-tests/tests/unit_tests/test_decorated_tool.py
+++ b/libs/standard-tests/tests/unit_tests/test_decorated_tool.py
@@ -1,3 +1,5 @@
+from typing import Any
+
from langchain_core.tools import BaseTool, tool
from langchain_tests.integration_tests import ToolsIntegrationTests
@@ -16,7 +18,7 @@ class TestParrotMultiplyToolUnit(ToolsUnitTests):
return parrot_multiply_tool
@property
- def tool_invoke_params_example(self) -> dict:
+ def tool_invoke_params_example(self) -> dict[str, Any]:
"""Returns a dictionary representing the "args" of an example tool call.
This should NOT be a ToolCall dict - i.e. it should not
@@ -31,7 +33,7 @@ class TestParrotMultiplyToolIntegration(ToolsIntegrationTests):
return parrot_multiply_tool
@property
- def tool_invoke_params_example(self) -> dict:
+ def tool_invoke_params_example(self) -> dict[str, Any]:
"""Returns a dictionary representing the "args" of an example tool call.
This should NOT be a ToolCall dict - i.e. it should not
diff --git a/libs/standard-tests/tests/unit_tests/test_embeddings.py b/libs/standard-tests/tests/unit_tests/test_embeddings.py
index a46d0c8e93..a4f075ae02 100644
--- a/libs/standard-tests/tests/unit_tests/test_embeddings.py
+++ b/libs/standard-tests/tests/unit_tests/test_embeddings.py
@@ -1,3 +1,5 @@
+from typing import Any
+
from langchain_core.embeddings import DeterministicFakeEmbedding, Embeddings
from langchain_tests.integration_tests import EmbeddingsIntegrationTests
@@ -10,7 +12,7 @@ class TestFakeEmbeddingsUnit(EmbeddingsUnitTests):
return DeterministicFakeEmbedding
@property
- def embedding_model_params(self) -> dict:
+ def embedding_model_params(self) -> dict[str, Any]:
return {"size": 6} # embedding dimension
@@ -20,5 +22,5 @@ class TestFakeEmbeddingsIntegration(EmbeddingsIntegrationTests):
return DeterministicFakeEmbedding
@property
- def embedding_model_params(self) -> dict:
+ def embedding_model_params(self) -> dict[str, Any]:
return {"size": 6}