From 4f0087c13e26c7520fb25ed0f4a7a9ffa22ec312 Mon Sep 17 00:00:00 2001 From: jinno Date: Sun, 1 Sep 2024 18:36:31 +0900 Subject: [PATCH] genai: chore for changing mypy settings --- .../langchain_google_genai/_function_utils.py | 10 ++++++---- .../langchain_google_genai/_genai_extension.py | 2 +- libs/genai/langchain_google_genai/chat_models.py | 16 +++++++--------- .../google_vector_store.py | 2 +- libs/genai/langchain_google_genai/llms.py | 13 +++++-------- .../tests/integration_tests/test_chat_models.py | 6 ++++-- libs/genai/tests/integration_tests/test_llms.py | 2 +- .../tests/unit_tests/test_function_utils.py | 4 ++-- 8 files changed, 27 insertions(+), 28 deletions(-) diff --git a/libs/genai/langchain_google_genai/_function_utils.py b/libs/genai/langchain_google_genai/_function_utils.py index 3b0a9259..bd0373ce 100644 --- a/libs/genai/langchain_google_genai/_function_utils.py +++ b/libs/genai/langchain_google_genai/_function_utils.py @@ -20,8 +20,10 @@ import google.ai.generativelanguage as glm import google.ai.generativelanguage_v1beta.types as gapic -import proto # type: ignore[import] -from google.generativeai.types.content_types import ToolDict # type: ignore[import] +import proto # type: ignore[import-untyped] +from google.generativeai.types.content_types import ( # type: ignore[import-untyped] + ToolDict, +) from langchain_core.pydantic_v1 import BaseModel from langchain_core.tools import BaseTool from langchain_core.tools import tool as callable_as_lc_tool @@ -147,7 +149,7 @@ def convert_to_genai_function_declarations( tools: Sequence[_ToolsType], ) -> gapic.Tool: if not isinstance(tools, collections.abc.Sequence): - logger.warning( + logger.warning( # type: ignore[unreachable] "convert_to_genai_function_declarations expects a Sequence " "and not a single tool." ) @@ -205,7 +207,7 @@ def _format_to_gapic_function_declaration( function = cast(dict, tool) function["parameters"] = {} else: - if "parameters" in tool and tool["parameters"].get("properties"): + if "parameters" in tool and tool["parameters"].get("properties"): # type: ignore[index] function = convert_to_openai_tool(cast(dict, tool))["function"] else: function = cast(dict, tool) diff --git a/libs/genai/langchain_google_genai/_genai_extension.py b/libs/genai/langchain_google_genai/_genai_extension.py index 071e481b..44c508e7 100644 --- a/libs/genai/langchain_google_genai/_genai_extension.py +++ b/libs/genai/langchain_google_genai/_genai_extension.py @@ -21,7 +21,7 @@ from google.api_core import client_options as client_options_lib from google.api_core import exceptions as gapi_exception from google.api_core import gapic_v1 -from google.auth import credentials, exceptions # type: ignore +from google.auth import credentials, exceptions from google.protobuf import timestamp_pb2 _logger = logging.getLogger(__name__) diff --git a/libs/genai/langchain_google_genai/chat_models.py b/libs/genai/langchain_google_genai/chat_models.py index 85032191..d2a3dbf8 100644 --- a/libs/genai/langchain_google_genai/chat_models.py +++ b/libs/genai/langchain_google_genai/chat_models.py @@ -29,7 +29,7 @@ import google.api_core # TODO: remove ignore once the google package is published with types -import proto # type: ignore[import] +import proto # type: ignore[import-untyped] import requests from google.ai.generativelanguage_v1beta.types import ( Blob, @@ -46,8 +46,8 @@ ToolConfig, VideoMetadata, ) -from google.generativeai.types import Tool as GoogleTool # type: ignore[import] -from google.generativeai.types.content_types import ( # type: ignore[import] +from google.generativeai.types import Tool as GoogleTool # type: ignore[import-untyped] +from google.generativeai.types.content_types import ( # type: ignore[import-untyped] FunctionDeclarationType, ToolDict, ) @@ -214,7 +214,7 @@ async def _achat_with_retry(generation_method: Callable, **kwargs: Any) -> Any: Any: The result from the chat generation method. """ retry_decorator = _create_retry_decorator() - from google.api_core.exceptions import InvalidArgument # type: ignore + from google.api_core.exceptions import InvalidArgument @retry_decorator async def _achat_with_retry(**kwargs: Any) -> Any: @@ -283,7 +283,7 @@ def _url_to_pil(image_source: str) -> Image: ) try: if isinstance(image_source, IMAGE_TYPES): - return image_source # type: ignore[return-value] + return image_source elif _is_url(image_source): if image_source.startswith("gs://"): return _load_image_from_gcs(image_source) @@ -1232,9 +1232,7 @@ def bind_tools( genai_tools = [tool_to_dict(convert_to_genai_function_declarations(tools))] if tool_choice: all_names = [ - f["name"] # type: ignore[index] - for t in genai_tools - for f in t["function_declarations"] + f["name"] for t in genai_tools for f in t["function_declarations"] ] tool_config = _tool_choice_to_tool_config(tool_choice, all_names) return self.bind(tools=genai_tools, tool_config=tool_config, **kwargs) @@ -1248,4 +1246,4 @@ def _get_tool_name( tool: Union[ToolDict, GoogleTool], ) -> str: genai_tool = tool_to_dict(convert_to_genai_function_declarations([tool])) - return [f["name"] for f in genai_tool["function_declarations"]][0] # type: ignore[index] + return [f["name"] for f in genai_tool["function_declarations"]][0] diff --git a/libs/genai/langchain_google_genai/google_vector_store.py b/libs/genai/langchain_google_genai/google_vector_store.py index 79c75d15..9e3184be 100644 --- a/libs/genai/langchain_google_genai/google_vector_store.py +++ b/libs/genai/langchain_google_genai/google_vector_store.py @@ -467,7 +467,7 @@ def as_aqa(self, **kwargs: Any) -> Runnable[str, AqaOutput]: return ( RunnablePassthrough[str]() | { - "prompt": RunnablePassthrough(), + "prompt": RunnablePassthrough(), # type: ignore "passages": self.as_retriever(), } | RunnableLambda(_toAqaInput) diff --git a/libs/genai/langchain_google_genai/llms.py b/libs/genai/langchain_google_genai/llms.py index d8ad1203..82e5eb85 100644 --- a/libs/genai/langchain_google_genai/llms.py +++ b/libs/genai/langchain_google_genai/llms.py @@ -4,7 +4,7 @@ from typing import Any, Callable, Dict, Iterator, List, Optional, Union import google.api_core -import google.generativeai as genai # type: ignore[import] +import google.generativeai as genai # type: ignore[import-untyped] from langchain_core.callbacks import ( AsyncCallbackManagerForLLMRun, CallbackManagerForLLMRun, @@ -15,10 +15,7 @@ from langchain_core.pydantic_v1 import BaseModel, Field, SecretStr, root_validator from langchain_core.utils import get_from_dict_or_env -from langchain_google_genai._enums import ( - HarmBlockThreshold, - HarmCategory, -) +from langchain_google_genai._enums import HarmBlockThreshold, HarmCategory class GoogleModelFamily(str, Enum): @@ -167,9 +164,9 @@ class _BaseGoogleGenerativeAI(BaseModel): ) safety_settings: Optional[Dict[HarmCategory, HarmBlockThreshold]] = None - """The default safety settings to use for all generations. - - For example: + """The default safety settings to use for all generations. + + For example: from google.generativeai.types.safety_types import HarmBlockThreshold, HarmCategory diff --git a/libs/genai/tests/integration_tests/test_chat_models.py b/libs/genai/tests/integration_tests/test_chat_models.py index 60bee7b5..890aa84c 100644 --- a/libs/genai/tests/integration_tests/test_chat_models.py +++ b/libs/genai/tests/integration_tests/test_chat_models.py @@ -5,7 +5,7 @@ from typing import Generator, List, Optional, Type import pytest -from google.generativeai.types import ( # type: ignore[import] +from google.generativeai.types import ( # type: ignore[import-untyped] HarmBlockThreshold, HarmCategory, ) @@ -21,7 +21,9 @@ ) from langchain_core.pydantic_v1 import BaseModel from langchain_core.tools import tool -from langchain_standard_tests.integration_tests import ChatModelIntegrationTests +from langchain_standard_tests.integration_tests import ( + ChatModelIntegrationTests, +) from langchain_google_genai import ChatGoogleGenerativeAI from langchain_google_genai.chat_models import ChatGoogleGenerativeAIError diff --git a/libs/genai/tests/integration_tests/test_llms.py b/libs/genai/tests/integration_tests/test_llms.py index 5e35c382..dd05f687 100644 --- a/libs/genai/tests/integration_tests/test_llms.py +++ b/libs/genai/tests/integration_tests/test_llms.py @@ -7,7 +7,7 @@ from typing import Generator import pytest -from google.generativeai.types import ( # type: ignore[import] +from google.generativeai.types import ( # type: ignore[import-untyped] HarmBlockThreshold, HarmCategory, ) diff --git a/libs/genai/tests/unit_tests/test_function_utils.py b/libs/genai/tests/unit_tests/test_function_utils.py index 786ad9f5..30ad9360 100644 --- a/libs/genai/tests/unit_tests/test_function_utils.py +++ b/libs/genai/tests/unit_tests/test_function_utils.py @@ -37,7 +37,7 @@ def sum_two_numbers(a: float, b: float) -> str: """ return str(a + b) - schema = convert_to_genai_function_declarations([sum_two_numbers]) # type: ignore + schema = convert_to_genai_function_declarations([sum_two_numbers]) function_declaration = schema.function_declarations[0] assert function_declaration.name == "sum_two_numbers" assert function_declaration.parameters @@ -48,7 +48,7 @@ def do_something_optional(a: float, b: float = 0) -> str: """Some description""" return str(a + b) - schema = convert_to_genai_function_declarations([do_something_optional]) # type: ignore + schema = convert_to_genai_function_declarations([do_something_optional]) function_declaration = schema.function_declarations[0] assert function_declaration.name == "do_something_optional" assert function_declaration.parameters