cli: standard tests in cli, test that they run, skip vectorstore tests (#28521)

This commit is contained in:
Erick Friis
2024-12-05 00:38:32 -08:00
committed by GitHub
parent c5acedddc2
commit 43c35d19d4
36 changed files with 1573 additions and 631 deletions

View File

@@ -6,7 +6,7 @@ import re
import shutil
import subprocess
from pathlib import Path
from typing import Optional
from typing import Dict, Optional, cast
import typer
from typing_extensions import Annotated, TypedDict
@@ -15,19 +15,17 @@ from langchain_cli.utils.find_replace import replace_file, replace_glob
integration_cli = typer.Typer(no_args_is_help=True, add_completion=False)
Replacements = TypedDict(
"Replacements",
{
"__package_name__": str,
"__module_name__": str,
"__ModuleName__": str,
"__MODULE_NAME__": str,
"__package_name_short__": str,
},
)
class Replacements(TypedDict):
__package_name__: str
__module_name__: str
__ModuleName__: str
__MODULE_NAME__: str
__package_name_short__: str
__package_name_short_snake__: str
def _process_name(name: str, *, community: bool = False):
def _process_name(name: str, *, community: bool = False) -> Replacements:
preprocessed = name.replace("_", "-").lower()
if preprocessed.startswith("langchain-"):
@@ -42,7 +40,7 @@ def _process_name(name: str, *, community: bool = False):
raise ValueError("Name should not end with `-`.")
if preprocessed.find("--") != -1:
raise ValueError("Name should not contain consecutive hyphens.")
replacements = {
replacements: Replacements = {
"__package_name__": f"langchain-{preprocessed}",
"__module_name__": "langchain_" + preprocessed.replace("-", "_"),
"__ModuleName__": preprocessed.title().replace("-", ""),
@@ -52,7 +50,7 @@ def _process_name(name: str, *, community: bool = False):
}
if community:
replacements["__module_name__"] = preprocessed.replace("-", "_")
return Replacements(replacements)
return replacements
@integration_cli.command()
@@ -74,16 +72,7 @@ def new(
):
"""
Creates a new integration package.
Should be run from libs/partners
"""
# confirm that we are in the right directory
if not Path.cwd().name == "partners" or not Path.cwd().parent.name == "libs":
typer.echo(
"This command should be run from the `libs/partners` directory in the "
"langchain-ai/langchain monorepo. Continuing is NOT recommended."
)
typer.confirm("Are you sure you want to continue?", abort=True)
try:
replacements = _process_name(name)
@@ -104,7 +93,7 @@ def new(
"Name of integration in PascalCase", default=replacements["__ModuleName__"]
)
destination_dir = Path.cwd() / replacements["__package_name_short__"]
destination_dir = Path.cwd() / replacements["__package_name__"]
if destination_dir.exists():
typer.echo(f"Folder {destination_dir} exists.")
raise typer.Exit(code=1)
@@ -118,7 +107,7 @@ def new(
shutil.move(destination_dir / "integration_template", package_dir)
# replacements in files
replace_glob(destination_dir, "**/*", replacements)
replace_glob(destination_dir, "**/*", cast(Dict[str, str], replacements))
# poetry install
subprocess.run(
@@ -226,4 +215,4 @@ def create_doc(
shutil.copy(docs_template, destination_path)
# replacements in file
replace_file(destination_path, replacements)
replace_file(destination_path, cast(Dict[str, str], replacements))

View File

@@ -17,7 +17,7 @@ PARTNER_PKGS = PKGS_ROOT / "partners"
class ImportExtractor(ast.NodeVisitor):
def __init__(self, *, from_package: Optional[str] = None) -> None:
"""Extract all imports from the given code, optionally filtering by package."""
self.imports = []
self.imports: list = []
self.package = from_package
def visit_ImportFrom(self, node):
@@ -68,7 +68,7 @@ def find_subclasses_in_module(module, classes_: List[Type]) -> List[str]:
return subclasses
def _get_all_classnames_from_file(file: str, pkg: str) -> List[Tuple[str, str]]:
def _get_all_classnames_from_file(file: Path, pkg: str) -> List[Tuple[str, str]]:
"""Extract all class names from a file."""
with open(file, encoding="utf-8") as f:
code = f.read()
@@ -145,7 +145,7 @@ def find_imports_from_package(
return extractor.imports
def _get_current_module(path: str, pkg_root: str) -> str:
def _get_current_module(path: Path, pkg_root: str) -> str:
"""Convert a path to a module name."""
path_as_pathlib = pathlib.Path(os.path.abspath(path))
relative_path = path_as_pathlib.relative_to(pkg_root).with_suffix("")

View File

@@ -4,7 +4,7 @@ from pathlib import Path
import rich
import typer
from gritql import run
from gritql import run # type: ignore
from typer import Option