infra: update mypy 1.10, ruff 0.5 (#23721)

```python
"""python scripts/update_mypy_ruff.py"""
import glob
import tomllib
from pathlib import Path

import toml
import subprocess
import re

ROOT_DIR = Path(__file__).parents[1]


def main():
    for path in glob.glob(str(ROOT_DIR / "libs/**/pyproject.toml"), recursive=True):
        print(path)
        with open(path, "rb") as f:
            pyproject = tomllib.load(f)
        try:
            pyproject["tool"]["poetry"]["group"]["typing"]["dependencies"]["mypy"] = (
                "^1.10"
            )
            pyproject["tool"]["poetry"]["group"]["lint"]["dependencies"]["ruff"] = (
                "^0.5"
            )
        except KeyError:
            continue
        with open(path, "w") as f:
            toml.dump(pyproject, f)
        cwd = "/".join(path.split("/")[:-1])
        completed = subprocess.run(
            "poetry lock --no-update; poetry install --with typing; poetry run mypy . --no-color",
            cwd=cwd,
            shell=True,
            capture_output=True,
            text=True,
        )
        logs = completed.stdout.split("\n")

        to_ignore = {}
        for l in logs:
            if re.match("^(.*)\:(\d+)\: error:.*\[(.*)\]", l):
                path, line_no, error_type = re.match(
                    "^(.*)\:(\d+)\: error:.*\[(.*)\]", l
                ).groups()
                if (path, line_no) in to_ignore:
                    to_ignore[(path, line_no)].append(error_type)
                else:
                    to_ignore[(path, line_no)] = [error_type]
        print(len(to_ignore))
        for (error_path, line_no), error_types in to_ignore.items():
            all_errors = ", ".join(error_types)
            full_path = f"{cwd}/{error_path}"
            try:
                with open(full_path, "r") as f:
                    file_lines = f.readlines()
            except FileNotFoundError:
                continue
            file_lines[int(line_no) - 1] = (
                file_lines[int(line_no) - 1][:-1] + f"  # type: ignore[{all_errors}]\n"
            )
            with open(full_path, "w") as f:
                f.write("".join(file_lines))

        subprocess.run(
            "poetry run ruff format .; poetry run ruff --select I --fix .",
            cwd=cwd,
            shell=True,
            capture_output=True,
            text=True,
        )


if __name__ == "__main__":
    main()

```
This commit is contained in:
Bagatur
2024-07-03 13:33:27 -04:00
committed by GitHub
parent 6cd56821dc
commit a0c2281540
915 changed files with 4759 additions and 4047 deletions

View File

@@ -28,7 +28,7 @@ from tests.unit_tests.fake.callbacks import (
@pytest.mark.scheduled
def test_invoke() -> None:
"""Test Chat wrapper."""
chat = ChatGroq(
chat = ChatGroq( # type: ignore[call-arg]
temperature=0.7,
base_url=None,
groq_proxy=None,
@@ -49,7 +49,7 @@ def test_invoke() -> None:
@pytest.mark.scheduled
async def test_ainvoke() -> None:
"""Test ainvoke tokens from ChatGroq."""
chat = ChatGroq(max_tokens=10)
chat = ChatGroq(max_tokens=10) # type: ignore[call-arg]
result = await chat.ainvoke("Welcome to the Groqetship!", config={"tags": ["foo"]})
assert isinstance(result, BaseMessage)
@@ -59,7 +59,7 @@ async def test_ainvoke() -> None:
@pytest.mark.scheduled
def test_batch() -> None:
"""Test batch tokens from ChatGroq."""
chat = ChatGroq(max_tokens=10)
chat = ChatGroq(max_tokens=10) # type: ignore[call-arg]
result = chat.batch(["Hello!", "Welcome to the Groqetship!"])
for token in result:
@@ -70,7 +70,7 @@ def test_batch() -> None:
@pytest.mark.scheduled
async def test_abatch() -> None:
"""Test abatch tokens from ChatGroq."""
chat = ChatGroq(max_tokens=10)
chat = ChatGroq(max_tokens=10) # type: ignore[call-arg]
result = await chat.abatch(["Hello!", "Welcome to the Groqetship!"])
for token in result:
@@ -81,7 +81,7 @@ async def test_abatch() -> None:
@pytest.mark.scheduled
async def test_stream() -> None:
"""Test streaming tokens from Groq."""
chat = ChatGroq(max_tokens=10)
chat = ChatGroq(max_tokens=10) # type: ignore[call-arg]
for token in chat.stream("Welcome to the Groqetship!"):
assert isinstance(token, BaseMessageChunk)
@@ -91,7 +91,7 @@ async def test_stream() -> None:
@pytest.mark.scheduled
async def test_astream() -> None:
"""Test streaming tokens from Groq."""
chat = ChatGroq(max_tokens=10)
chat = ChatGroq(max_tokens=10) # type: ignore[call-arg]
full: Optional[BaseMessageChunk] = None
chunks_with_token_counts = 0
@@ -124,7 +124,7 @@ async def test_astream() -> None:
def test_generate() -> None:
"""Test sync generate."""
n = 1
chat = ChatGroq(max_tokens=10)
chat = ChatGroq(max_tokens=10) # type: ignore[call-arg]
message = HumanMessage(content="Hello", n=1)
response = chat.generate([[message], [message]])
assert isinstance(response, LLMResult)
@@ -143,7 +143,7 @@ def test_generate() -> None:
async def test_agenerate() -> None:
"""Test async generation."""
n = 1
chat = ChatGroq(max_tokens=10, n=1)
chat = ChatGroq(max_tokens=10, n=1) # type: ignore[call-arg]
message = HumanMessage(content="Hello")
response = await chat.agenerate([[message], [message]])
assert isinstance(response, LLMResult)
@@ -165,7 +165,7 @@ async def test_agenerate() -> None:
def test_invoke_streaming() -> None:
"""Test that streaming correctly invokes on_llm_new_token callback."""
callback_handler = FakeCallbackHandler()
chat = ChatGroq(
chat = ChatGroq( # type: ignore[call-arg]
max_tokens=2,
streaming=True,
temperature=0,
@@ -181,7 +181,7 @@ def test_invoke_streaming() -> None:
async def test_agenerate_streaming() -> None:
"""Test that streaming correctly invokes on_llm_new_token callback."""
callback_handler = FakeCallbackHandlerWithChatStart()
chat = ChatGroq(
chat = ChatGroq( # type: ignore[call-arg]
max_tokens=10,
streaming=True,
temperature=0,
@@ -220,7 +220,7 @@ def test_streaming_generation_info() -> None:
self.saved_things["generation"] = args[0]
callback = _FakeCallback()
chat = ChatGroq(
chat = ChatGroq( # type: ignore[call-arg]
max_tokens=2,
temperature=0,
callbacks=[callback],
@@ -234,7 +234,7 @@ def test_streaming_generation_info() -> None:
def test_system_message() -> None:
"""Test ChatGroq wrapper with system message."""
chat = ChatGroq(max_tokens=10)
chat = ChatGroq(max_tokens=10) # type: ignore[call-arg]
system_message = SystemMessage(content="You are to chat with the user.")
human_message = HumanMessage(content="Hello")
response = chat.invoke([system_message, human_message])
@@ -245,7 +245,7 @@ def test_system_message() -> None:
@pytest.mark.xfail(reason="Groq tool_choice doesn't currently force a tool call")
def test_tool_choice() -> None:
"""Test that tool choice is respected."""
llm = ChatGroq()
llm = ChatGroq() # type: ignore[call-arg]
class MyTool(BaseModel):
name: str
@@ -276,7 +276,7 @@ def test_tool_choice() -> None:
@pytest.mark.xfail(reason="Groq tool_choice doesn't currently force a tool call")
def test_tool_choice_bool() -> None:
"""Test that tool choice is respected just passing in True."""
llm = ChatGroq()
llm = ChatGroq() # type: ignore[call-arg]
class MyTool(BaseModel):
name: str
@@ -301,7 +301,7 @@ def test_tool_choice_bool() -> None:
@pytest.mark.xfail(reason="Groq tool_choice doesn't currently force a tool call")
def test_streaming_tool_call() -> None:
"""Test that tool choice is respected."""
llm = ChatGroq()
llm = ChatGroq() # type: ignore[call-arg]
class MyTool(BaseModel):
name: str
@@ -339,7 +339,7 @@ def test_streaming_tool_call() -> None:
@pytest.mark.xfail(reason="Groq tool_choice doesn't currently force a tool call")
async def test_astreaming_tool_call() -> None:
"""Test that tool choice is respected."""
llm = ChatGroq()
llm = ChatGroq() # type: ignore[call-arg]
class MyTool(BaseModel):
name: str
@@ -384,7 +384,7 @@ def test_json_mode_structured_output() -> None:
setup: str = Field(description="question to set up a joke")
punchline: str = Field(description="answer to resolve the joke")
chat = ChatGroq().with_structured_output(Joke, method="json_mode")
chat = ChatGroq().with_structured_output(Joke, method="json_mode") # type: ignore[call-arg]
result = chat.invoke(
"Tell me a joke about cats, respond in JSON with `setup` and `punchline` keys"
)