From b40433667e21af15a68c687a3cb243f1f03939c6 Mon Sep 17 00:00:00 2001 From: cbornet Date: Fri, 5 Dec 2025 14:31:53 +0100 Subject: [PATCH 1/2] chore(standard-tests): enable mypy disallow_any_generics rule --- libs/standard-tests/langchain_tests/conftest.py | 12 ++++++------ .../integration_tests/chat_models.py | 6 +++--- .../integration_tests/retrievers.py | 3 ++- .../langchain_tests/unit_tests/chat_models.py | 12 +++++++----- .../langchain_tests/unit_tests/embeddings.py | 7 +++++-- .../langchain_tests/unit_tests/tools.py | 9 ++++++--- libs/standard-tests/pyproject.toml | 4 ---- .../tests/unit_tests/test_basic_retriever.py | 2 +- .../tests/unit_tests/test_basic_tool.py | 14 +++++++------- .../tests/unit_tests/test_custom_chat_model.py | 6 +++--- .../tests/unit_tests/test_decorated_tool.py | 6 ++++-- .../tests/unit_tests/test_embeddings.py | 6 ++++-- 12 files changed, 48 insertions(+), 39 deletions(-) diff --git a/libs/standard-tests/langchain_tests/conftest.py b/libs/standard-tests/langchain_tests/conftest.py index 161fc744cbff5..4afc7b2004839 100644 --- a/libs/standard-tests/langchain_tests/conftest.py +++ b/libs/standard-tests/langchain_tests/conftest.py @@ -28,7 +28,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"] = [ { @@ -43,7 +43,7 @@ def serialize(cassette_dict: dict) -> bytes: 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)) @@ -59,7 +59,7 @@ def load_cassette( 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) @@ -74,7 +74,7 @@ def load_cassette( @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 @@ def save_cassette( @pytest.fixture(scope="session") -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.) @@ -116,6 +116,6 @@ def _base_vcr_config() -> dict: @pytest.fixture(scope="session") -def vcr_config(_base_vcr_config: dict) -> dict: +def vcr_config(_base_vcr_config: dict[str, Any]) -> 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 f547ef3711600..138a70ebc3375 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 validate_joke_dict(result: Any) -> bool: class _TestCallbackHandler(BaseCallbackHandler): - options: list[dict | None] + options: list[dict[str, Any] | None] def __init__(self) -> None: super().__init__() @@ -743,7 +743,7 @@ def pytest_recording_configure(config: dict, vcr: VCR) -> None: ''' # 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 {} @@ -3076,7 +3076,7 @@ def supports_anthropic_inputs(self) -> bool: "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 00ed2b0414622..636a5008ced32 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 @@ def retriever_constructor(self) -> type[BaseRetriever]: ... @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 5460623bd9aaa..70773e4361adc 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 @@ def chat_model_class(self) -> type[BaseChatModel]: ... @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 @@ def has_structured_output(self) -> bool: ) 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 @@ -906,14 +906,16 @@ def init_from_env_params(self) -> Tuple[dict, dict, dict]: ''' # 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 c8901ab55b7fb..34ae029b2932a 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 @@ def embeddings_class(self) -> type[Embeddings]: """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 @@ def test_init(self) -> None: 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 530ed03e8c88a..24f7548a9ee3c 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 @@ import os from abc import abstractmethod +from typing import Any from unittest import mock import pytest @@ -27,12 +28,12 @@ def tool_constructor(self) -> type[BaseTool] | BaseTool: ... @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 dddecbc0e4704..6390daf40c1ce 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 ccff1e43e140a..847a137fc35ec 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 @@ def retriever_constructor(self) -> type[ParrotRetriever]: 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 d7ab342f04329..b641b55f8a6df 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 @@ def tool_constructor(self) -> type[ParrotMultiplyTool]: 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 @@ def tool_constructor(self) -> type[ParrotMultiplyTool]: 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 @@ def tool_constructor(self) -> type[ParrotMultiplyArtifactTool]: 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 eea5550ee622c..c14b2ac848290 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 @@ def chat_model_class(self) -> type[ChatParrotLink]: 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 @@ def chat_model_class(self) -> type[ChatParrotLink]: 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 c3b7b89d04db7..a559fe6f98e01 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 @@ def tool_constructor(self) -> BaseTool: 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 @@ def tool_constructor(self) -> BaseTool: 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 a46d0c8e935b7..a4f075ae02938 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 @@ def embeddings_class(self) -> type[Embeddings]: 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 @@ def embeddings_class(self) -> type[Embeddings]: return DeterministicFakeEmbedding @property - def embedding_model_params(self) -> dict: + def embedding_model_params(self) -> dict[str, Any]: return {"size": 6} From 90619fdb26d1d9992f30490b3c419a08725a35c5 Mon Sep 17 00:00:00 2001 From: Sydney Runkle Date: Fri, 12 Dec 2025 14:27:56 -0500 Subject: [PATCH 2/2] patch --- libs/standard-tests/langchain_tests/conftest.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/standard-tests/langchain_tests/conftest.py b/libs/standard-tests/langchain_tests/conftest.py index ceddf6a8b4580..96ff7bf741e84 100644 --- a/libs/standard-tests/langchain_tests/conftest.py +++ b/libs/standard-tests/langchain_tests/conftest.py @@ -122,6 +122,6 @@ def _base_vcr_config() -> dict[str, Any]: @pytest.fixture(scope="session") -def vcr_config() -> dict: +def vcr_config() -> dict[str, Any]: """VCR config fixture.""" return base_vcr_config()