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

@@ -7,6 +7,7 @@ the prompt before the LLM call.
and flexible online machine learning techniques for reinforcement learning,
supervised learning, and more.
"""
import logging
from langchain_experimental.rl_chain.base import (

View File

@@ -192,16 +192,13 @@ class Policy(Generic[TEvent], ABC):
pass
@abstractmethod
def predict(self, event: TEvent) -> Any:
...
def predict(self, event: TEvent) -> Any: ...
@abstractmethod
def learn(self, event: TEvent) -> None:
...
def learn(self, event: TEvent) -> None: ...
@abstractmethod
def log(self, event: TEvent) -> None:
...
def log(self, event: TEvent) -> None: ...
def save(self) -> None:
pass
@@ -257,8 +254,7 @@ class Embedder(Generic[TEvent], ABC):
pass
@abstractmethod
def format(self, event: TEvent) -> str:
...
def format(self, event: TEvent) -> str: ...
class SelectionScorer(Generic[TEvent], ABC, BaseModel):
@@ -267,8 +263,7 @@ class SelectionScorer(Generic[TEvent], ABC, BaseModel):
@abstractmethod
def score_response(
self, inputs: Dict[str, Any], llm_response: str, event: TEvent
) -> float:
...
) -> float: ...
class AutoSelectionScorer(SelectionScorer[Event], BaseModel):
@@ -316,7 +311,7 @@ class AutoSelectionScorer(SelectionScorer[Event], BaseModel):
[default_system_prompt, human_message_prompt]
)
values["prompt"] = prompt
values["llm_chain"] = LLMChain(llm=llm, prompt=prompt)
values["llm_chain"] = LLMChain(llm=llm, prompt=prompt) # type: ignore[arg-type, arg-type]
return values
def score_response(
@@ -495,26 +490,22 @@ class RLChain(Chain, Generic[TEvent]):
return self.selection_scorer is not None and self.selection_scorer_activated
@abstractmethod
def _call_before_predict(self, inputs: Dict[str, Any]) -> TEvent:
...
def _call_before_predict(self, inputs: Dict[str, Any]) -> TEvent: ...
@abstractmethod
def _call_after_predict_before_llm(
self, inputs: Dict[str, Any], event: TEvent, prediction: Any
) -> Tuple[Dict[str, Any], TEvent]:
...
) -> Tuple[Dict[str, Any], TEvent]: ...
@abstractmethod
def _call_after_llm_before_scoring(
self, llm_response: str, event: TEvent
) -> Tuple[Dict[str, Any], TEvent]:
...
) -> Tuple[Dict[str, Any], TEvent]: ...
@abstractmethod
def _call_after_scoring_before_learning(
self, event: TEvent, score: Optional[float]
) -> TEvent:
...
) -> TEvent: ...
def _call(
self,

View File

@@ -408,7 +408,7 @@ class PickBest(base.RLChain[PickBestEvent]):
) -> PickBest:
llm_chain = LLMChain(llm=llm, prompt=prompt)
if selection_scorer is SENTINEL:
selection_scorer = base.AutoSelectionScorer(llm=llm_chain.llm)
selection_scorer = base.AutoSelectionScorer(llm=llm_chain.llm) # type: ignore[call-arg]
return PickBest(
llm_chain=llm_chain,