Skip to content

Commit

Permalink
feat: enable PyUpgrade Ruff Rule-Set (#633)
Browse files Browse the repository at this point in the history
  • Loading branch information
max-muoto authored Apr 30, 2024
1 parent bf69c0a commit 3c9820b
Show file tree
Hide file tree
Showing 37 changed files with 72 additions and 70 deletions.
7 changes: 3 additions & 4 deletions .ruff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ select = [
"E722",
# unused arguments
"ARG",
# Enforce modern type-syntax
"UP006",
# pyupgrade
"UP",
]
ignore = [
# mutable defaults
Expand All @@ -55,10 +55,9 @@ unfixable = [
]
ignore-init-module-imports = true

[extend-per-file-ignores]
[lint.extend-per-file-ignores]
"instructor/distil.py" = ["ARG002"]
"tests/test_distil.py" = ["ARG001"]
"tests/test_patch.py" = ["ARG001"]
"examples/task_planner/task_planner_topological_sort.py" = ["ARG002"]
"examples/citation_with_extraction/main.py" = ["ARG001"]

3 changes: 2 additions & 1 deletion examples/avail/run.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from pydantic import BaseModel, Field
from typing import Iterable, Literal
from typing import Literal
from collections.abc import Iterable
from datetime import datetime, timedelta

from openai import OpenAI
Expand Down
2 changes: 1 addition & 1 deletion examples/chain-of-density/finetune.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def distil_summarization(text: str) -> GeneratedSummary:
return GeneratedSummary(summary=summary_chain[-1])


with open("test.csv", "r") as file:
with open("test.csv") as file:
reader = csv.reader(file)
next(reader) # Skip the header
for article, _summary in reader:
Expand Down
2 changes: 1 addition & 1 deletion examples/citation_with_extraction/main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import json
from typing import Iterable
from collections.abc import Iterable
from fastapi import FastAPI, Request, HTTPException
from fastapi.params import Depends
from instructor import OpenAISchema
Expand Down
2 changes: 1 addition & 1 deletion examples/evals/streamlit.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from stats_dict import stats_dict

# Sample data
query_data = {i: line.strip() for i, line in enumerate(open("test.jsonl", "r"))}
query_data = {i: line.strip() for i, line in enumerate(open("test.jsonl"))}

# Initialize selected keys
selected_keys = {}
Expand Down
3 changes: 2 additions & 1 deletion examples/parallel/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import openai
import instructor

from typing import Iterable, Literal
from typing import Literal
from collections.abc import Iterable
from pydantic import BaseModel


Expand Down
3 changes: 2 additions & 1 deletion examples/patching/pcalls.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import Iterable, Literal, Union
from typing import Literal, Union
from collections.abc import Iterable
from pydantic import BaseModel
from instructor import OpenAISchema

Expand Down
3 changes: 2 additions & 1 deletion examples/stream_action_items/run.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import instructor

from pydantic import BaseModel, Field
from typing import Iterable, Optional
from typing import Optional
from collections.abc import Iterable
from openai import OpenAI
from rich.console import Console

Expand Down
2 changes: 1 addition & 1 deletion examples/streaming_multitask/streaming_multitask.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import time

from typing import Iterable
from collections.abc import Iterable
from openai import OpenAI
from pydantic import BaseModel

Expand Down
2 changes: 1 addition & 1 deletion examples/synethic-data/run.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import openai
import instructor
from typing import Iterable
from collections.abc import Iterable
from pydantic import BaseModel, ConfigDict

client = instructor.from_openai(openai.OpenAI())
Expand Down
2 changes: 1 addition & 1 deletion examples/task_planner/task_planner_topological_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"""

import asyncio
from typing import Generator
from collections.abc import Generator

from openai import OpenAI

Expand Down
2 changes: 1 addition & 1 deletion examples/validators/allm_validator.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import asyncio
from typing_extensions import Annotated
from typing import Annotated
from pydantic import BaseModel, BeforeValidator
from instructor import llm_validator, patch
from openai import AsyncOpenAI
Expand Down
2 changes: 1 addition & 1 deletion examples/validators/annotator.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing_extensions import Annotated
from typing import Annotated
from pydantic import BaseModel, ValidationError
from pydantic.functional_validators import AfterValidator

Expand Down
2 changes: 1 addition & 1 deletion examples/validators/citations.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing_extensions import Annotated
from typing import Annotated
from pydantic import BaseModel, ValidationError, ValidationInfo, AfterValidator
from openai import OpenAI
import instructor
Expand Down
2 changes: 1 addition & 1 deletion examples/validators/competitors.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing_extensions import Annotated
from typing import Annotated
from pydantic import BaseModel, ValidationError, AfterValidator
from openai import OpenAI

Expand Down
2 changes: 1 addition & 1 deletion examples/validators/llm_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from openai import OpenAI
from instructor import llm_validator
from pydantic import BaseModel, ValidationError, BeforeValidator
from typing_extensions import Annotated
from typing import Annotated

# Apply the patch to the OpenAI client
client = instructor.from_openai(OpenAI())
Expand Down
2 changes: 1 addition & 1 deletion examples/validators/moderation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from instructor import openai_moderation

from typing_extensions import Annotated
from typing import Annotated
from pydantic import BaseModel, AfterValidator
from openai import OpenAI

Expand Down
2 changes: 1 addition & 1 deletion examples/vision/image_to_ad_copy.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ def run(images: list[str]) -> tuple[list[Product], list[AdCopy]]:
sys.exit(1)

image_file = sys.argv[1]
with open(image_file, "r") as file:
with open(image_file) as file:
logger.info(f"Reading images from file: {image_file}")
try:
image_list = file.read().splitlines()
Expand Down
2 changes: 1 addition & 1 deletion examples/vision/slides.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def process_and_identify_competitors():
logger.info("Starting app...")

try:
with open(IMAGE_FILE, "r") as file:
with open(IMAGE_FILE) as file:
logger.info(f"Reading images from file: {IMAGE_FILE}")
image_list = file.read().splitlines()
logger.info(f"{len(image_list)} images read from file: {IMAGE_FILE}")
Expand Down
2 changes: 1 addition & 1 deletion examples/youtube-clips/run.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from youtube_transcript_api import YouTubeTranscriptApi
from pydantic import BaseModel, Field
from typing import Generator, Iterable
from collections.abc import Generator, Iterable
import instructor
import openai

Expand Down
4 changes: 3 additions & 1 deletion instructor/cli/usage.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from typing import Any, Awaitable, List, Union
from typing import Any, Union
from collections.abc import Awaitable
from datetime import datetime, timedelta
import typer
import os
import aiohttp
import asyncio
from builtins import list as List
from collections import defaultdict
from rich.console import Console
from rich.table import Table
Expand Down
23 changes: 10 additions & 13 deletions instructor/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,12 @@
from openai.types.chat import ChatCompletionMessageParam
from typing import (
TypeVar,
Generator,
Iterable,
Callable,
overload,
Union,
Awaitable,
AsyncGenerator,
Any,
)
from collections.abc import Generator, Iterable, Awaitable, AsyncGenerator
from typing_extensions import Self
from pydantic import BaseModel
from instructor.dsl.partial import Partial
Expand Down Expand Up @@ -59,7 +56,7 @@ def messages(self) -> Self:

@overload
def create(
self: "AsyncInstructor",
self: AsyncInstructor,
response_model: type[T],
messages: list[ChatCompletionMessageParam],
max_retries: int = 3,
Expand Down Expand Up @@ -88,7 +85,7 @@ def create(
validation_context: dict[str, Any] | None = None,
strict: bool = True,
**kwargs: Any,
) -> Union[T, Awaitable[T]]:
) -> T | Awaitable[T]:
kwargs = self.handle_kwargs(kwargs)

return self.create_fn(
Expand All @@ -102,7 +99,7 @@ def create(

@overload
def create_partial(
self: "AsyncInstructor",
self: AsyncInstructor,
response_model: type[T],
messages: list[ChatCompletionMessageParam],
max_retries: int = 3,
Expand Down Expand Up @@ -130,7 +127,7 @@ def create_partial(
validation_context: dict[str, Any] | None = None,
strict: bool = True,
**kwargs: Any,
) -> Union[Generator[T, None, None], AsyncGenerator[T, None]]:
) -> Generator[T, None, None] | AsyncGenerator[T, None]:
assert self.provider != Provider.ANTHROPIC, "Anthropic doesn't support partial"

kwargs["stream"] = True
Expand All @@ -149,7 +146,7 @@ def create_partial(

@overload
def create_iterable(
self: "AsyncInstructor",
self: AsyncInstructor,
messages: list[ChatCompletionMessageParam],
response_model: type[T],
max_retries: int = 3,
Expand Down Expand Up @@ -177,7 +174,7 @@ def create_iterable(
validation_context: dict[str, Any] | None = None,
strict: bool = True,
**kwargs: Any,
) -> Union[Generator[T, None, None], AsyncGenerator[T, None]]:
) -> Generator[T, None, None] | AsyncGenerator[T, None]:
assert self.provider != Provider.ANTHROPIC, "Anthropic doesn't support iterable"

kwargs["stream"] = True
Expand All @@ -195,7 +192,7 @@ def create_iterable(

@overload
def create_with_completion(
self: "AsyncInstructor",
self: AsyncInstructor,
messages: list[ChatCompletionMessageParam],
response_model: type[T],
max_retries: int = 3,
Expand Down Expand Up @@ -223,7 +220,7 @@ def create_with_completion(
validation_context: dict[str, Any] | None = None,
strict: bool = True,
**kwargs: Any,
) -> Union[tuple[T, Any], Awaitable[tuple[T, Any]]]:
) -> tuple[T, Any] | Awaitable[tuple[T, Any]]:
kwargs = self.handle_kwargs(kwargs)
model = self.create_fn(
messages=messages,
Expand Down Expand Up @@ -368,7 +365,7 @@ def from_openai(


def from_openai(
client: Union[openai.OpenAI, openai.AsyncOpenAI],
client: openai.OpenAI | openai.AsyncOpenAI,
mode: instructor.Mode = instructor.Mode.TOOLS,
**kwargs: Any,
) -> Instructor | AsyncInstructor:
Expand Down
6 changes: 3 additions & 3 deletions instructor/client_cohere.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
TypeVar,
overload,
)
from typing import Any, Optional
from typing import Any
from typing_extensions import ParamSpec
from pydantic import BaseModel
from instructor.process_response import handle_response_model
Expand Down Expand Up @@ -58,8 +58,8 @@ def from_cohere(

@wraps(client.chat)
async def new_create_async(
response_model: Optional[type[T_Model]] = None,
validation_context: Optional[dict[str, Any]] = None,
response_model: type[T_Model] | None = None,
validation_context: dict[str, Any] | None = None,
max_retries: int = 1,
*args: T_ParamSpec.args,
**kwargs: T_ParamSpec.kwargs,
Expand Down
2 changes: 1 addition & 1 deletion instructor/distil.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def get_signature_from_fn(fn: Callable[..., Any]) -> str:
return f"{lines}\n{formatted_docstring}"


@functools.lru_cache()
@functools.lru_cache
def format_function(func: Callable[..., Any]) -> str:
"""
Format a function as a string with docstring and body.
Expand Down
2 changes: 1 addition & 1 deletion instructor/dsl/citation.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from pydantic import BaseModel, Field, model_validator, ValidationInfo
from typing import Generator
from collections.abc import Generator


class CitationMixin(BaseModel):
Expand Down
3 changes: 2 additions & 1 deletion instructor/dsl/iterable.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import Any, AsyncGenerator, Generator, Iterable, Optional, cast, ClassVar
from typing import Any, Optional, cast, ClassVar
from collections.abc import AsyncGenerator, Generator, Iterable

from pydantic import BaseModel, Field, create_model # type: ignore - remove once Pydantic is updated

Expand Down
2 changes: 1 addition & 1 deletion instructor/dsl/parallel.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from typing import (
Any,
Generator,
Optional,
TypeVar,
Union,
get_args,
get_origin,
)
from collections.abc import Generator
from pydantic import BaseModel
from instructor.function_calls import OpenAISchema, openai_schema
from collections.abc import Iterable
Expand Down
12 changes: 5 additions & 7 deletions instructor/dsl/partial.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,16 @@
from pydantic.fields import FieldInfo
from typing import (
Any,
AsyncGenerator,
Generator,
Generic,
get_args,
get_origin,
Iterable,
NoReturn,
Optional,
TypeVar,
)
from collections.abc import AsyncGenerator, Generator, Iterable
from copy import deepcopy
from functools import lru_cache
from functools import cache

from instructor.mode import Mode
from instructor.utils import extract_json_from_stream, extract_json_from_stream_async
Expand Down Expand Up @@ -78,7 +76,7 @@ def _make_field_optional(

class PartialBase(Generic[T_Model]):
@classmethod
@lru_cache(maxsize=None)
@cache
def get_partial_model(cls) -> type[T_Model]:
"""Return a partial model we can use to validate partial results."""
assert issubclass(
Expand Down Expand Up @@ -207,7 +205,7 @@ def __new__(
cls,
*args: object, # noqa :ARG003
**kwargs: object, # noqa :ARG003
) -> "Partial[T_Model]":
) -> Partial[T_Model]:
"""Cannot instantiate.
Raises:
Expand All @@ -225,7 +223,7 @@ def __init_subclass__(
Raises:
TypeError: Subclassing not allowed.
"""
raise TypeError("Cannot subclass {}.Partial".format(cls.__module__))
raise TypeError(f"Cannot subclass {cls.__module__}.Partial")

def __class_getitem__(
cls,
Expand Down
Loading

0 comments on commit 3c9820b

Please sign in to comment.