From 46ac59c745e751a47631164315be9fa5afa7e8d9 Mon Sep 17 00:00:00 2001 From: Ivan Leo Date: Wed, 18 Dec 2024 13:04:17 +0800 Subject: [PATCH] Fixing Pyright Errors (#1267) This fixes the pyright errors introduced in a previous commit --- instructor/batch.py | 6 +++--- instructor/dsl/citation.py | 2 +- instructor/dsl/iterable.py | 16 +++++++++++++--- instructor/dsl/maybe.py | 2 +- instructor/dsl/validators.py | 2 +- instructor/multimodal.py | 22 ++++++++++------------ 6 files changed, 29 insertions(+), 21 deletions(-) diff --git a/instructor/batch.py b/instructor/batch.py index ee37a9ca5..480bcd70b 100644 --- a/instructor/batch.py +++ b/instructor/batch.py @@ -1,6 +1,6 @@ from typing import Any, Union, TypeVar, Optional from collections.abc import Iterable -from pydantic import BaseModel, Field # type: ignore +from pydantic import BaseModel, Field from instructor.process_response import handle_response_model import instructor import uuid @@ -139,7 +139,7 @@ def create_from_messages( "temperature": temperature, "messages": messages, **kwargs, - } + }, } file.write(json.dumps(request) + "\n") else: @@ -159,6 +159,6 @@ def create_from_messages( **kwargs, ), method="POST", - url="/v1/chat/completions" + url="/v1/chat/completions", ) file.write(batch_model.model_dump_json() + "\n") diff --git a/instructor/dsl/citation.py b/instructor/dsl/citation.py index 239de94f7..c5ccd4f04 100644 --- a/instructor/dsl/citation.py +++ b/instructor/dsl/citation.py @@ -1,4 +1,4 @@ -from pydantic import BaseModel, Field, model_validator, ValidationInfo # type: ignore +from pydantic import BaseModel, Field, model_validator, ValidationInfo from collections.abc import Generator diff --git a/instructor/dsl/iterable.py b/instructor/dsl/iterable.py index 55f1da287..d8aedfbcb 100644 --- a/instructor/dsl/iterable.py +++ b/instructor/dsl/iterable.py @@ -1,7 +1,7 @@ from typing import Any, Optional, cast, ClassVar from collections.abc import AsyncGenerator, Generator, Iterable -from pydantic import BaseModel, Field, create_model # type: ignore +from pydantic import BaseModel, Field, create_model from instructor.function_calls import OpenAISchema from instructor.mode import Mode @@ -109,7 +109,12 @@ def extract_json( }: if json_chunk := chunk.choices[0].delta.content: yield json_chunk - elif mode in {Mode.TOOLS, Mode.TOOLS_STRICT, Mode.FIREWORKS_TOOLS, Mode.WRITER_TOOLS}: + elif mode in { + Mode.TOOLS, + Mode.TOOLS_STRICT, + Mode.FIREWORKS_TOOLS, + Mode.WRITER_TOOLS, + }: if json_chunk := chunk.choices[0].delta.tool_calls: if json_chunk[0].function.arguments is not None: yield json_chunk[0].function.arguments @@ -145,7 +150,12 @@ async def extract_json_async( }: if json_chunk := chunk.choices[0].delta.content: yield json_chunk - elif mode in {Mode.TOOLS, Mode.TOOLS_STRICT, Mode.FIREWORKS_TOOLS, Mode.WRITER_TOOLS}: + elif mode in { + Mode.TOOLS, + Mode.TOOLS_STRICT, + Mode.FIREWORKS_TOOLS, + Mode.WRITER_TOOLS, + }: if json_chunk := chunk.choices[0].delta.tool_calls: if json_chunk[0].function.arguments is not None: yield json_chunk[0].function.arguments diff --git a/instructor/dsl/maybe.py b/instructor/dsl/maybe.py index 6363be51a..4314de057 100644 --- a/instructor/dsl/maybe.py +++ b/instructor/dsl/maybe.py @@ -1,4 +1,4 @@ -from pydantic import BaseModel, Field, create_model # type: ignore +from pydantic import BaseModel, Field, create_model from typing import Generic, Optional, TypeVar T = TypeVar("T", bound=BaseModel) diff --git a/instructor/dsl/validators.py b/instructor/dsl/validators.py index 2c14d23f4..d6fa99c44 100644 --- a/instructor/dsl/validators.py +++ b/instructor/dsl/validators.py @@ -1,7 +1,7 @@ from typing import Callable, Optional from openai import OpenAI -from pydantic import Field # type: ignore +from pydantic import Field from instructor.function_calls import OpenAISchema from instructor.client import Instructor diff --git a/instructor/multimodal.py b/instructor/multimodal.py index 3aff72c7b..a25738b55 100644 --- a/instructor/multimodal.py +++ b/instructor/multimodal.py @@ -18,7 +18,7 @@ from urllib.parse import urlparse import mimetypes import requests -from pydantic import BaseModel, Field # type:ignore +from pydantic import BaseModel, Field from .mode import Mode F = TypeVar("F", bound=Callable[..., Any]) @@ -76,9 +76,7 @@ def autodetect(cls, source: Union[str, Path]) -> Image: # noqa: UP007 raise ValueError("Unable to determine image type or unsupported image format") @classmethod - def autodetect_safely( - cls, source: str | Path - ) -> Union[Image, str]: # noqa: UP007 + def autodetect_safely(cls, source: str | Path) -> Union[Image, str]: # noqa: UP007 """Safely attempt to autodetect an image from a source string or path. Args: @@ -210,9 +208,7 @@ def to_openai(self) -> dict[str, Any]: class Audio(BaseModel): """Represents an audio that can be loaded from a URL or file path.""" - source: str | Path = Field( - description="URL or file path of the audio" - ) # noqa: UP007 + source: str | Path = Field(description="URL or file path of the audio") # noqa: UP007 data: Union[str, None] = Field( # noqa: UP007 None, description="Base64 encoded audio data", repr=False ) @@ -343,9 +339,7 @@ def is_image_params(x: Any) -> bool: } if autodetect_images: if isinstance(content, list): - new_content: list[str | dict[str, Any] | Image | Audio] = ( - [] - ) # noqa: UP007 + new_content: list[str | dict[str, Any] | Image | Audio] = [] # noqa: UP007 for item in content: if isinstance(item, str): new_content.append(Image.autodetect_safely(item)) @@ -365,8 +359,12 @@ def is_image_params(x: Any) -> bool: cast(ImageParams, content) ) if isinstance(content, str): - converted_messages.append({"role": role, "content": content, **other_kwargs}) # type: ignore + converted_messages.append( # type: ignore + {"role": role, "content": content, **other_kwargs} + ) else: converted_content = convert_contents(content, mode) - converted_messages.append({"role": role, "content": converted_content, **other_kwargs}) # type: ignore + converted_messages.append( # type: ignore + {"role": role, "content": converted_content, **other_kwargs} + ) return converted_messages # type: ignore