ref: Auto-fix ruff rules in tests (#4154)

This commit is contained in:
Christophe Bornet 2024-10-16 17:42:36 +02:00 • committed by GitHub
commit 45c8f98692
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
80 changed files with 359 additions and 456 deletions

View file

@ -1,7 +1,5 @@
#!/usr/bin/env python
"""
Idea from https://github.com/streamlit/streamlit/blob/4841cf91f1c820a392441092390c4c04907f9944/scripts/pypi_nightly_create_tag.py
"""
"""Idea from https://github.com/streamlit/streamlit/blob/4841cf91f1c820a392441092390c4c04907f9944/scripts/pypi_nightly_create_tag.py."""
import sys
@ -24,13 +22,15 @@ def get_latest_published_version(build_type: str, is_nightly: bool) -> Version:
elif build_type == "main":
url = PYPI_LANGFLOW_NIGHTLY_URL if is_nightly else PYPI_LANGFLOW_URL
else:
raise ValueError(f"Invalid build type: {build_type}")
msg = f"Invalid build type: {build_type}"
raise ValueError(msg)
res = requests.get(url)
try:
version_str = res.json()["info"]["version"]
except Exception as e:
raise RuntimeError("Got unexpected response from PyPI", e)
msg = "Got unexpected response from PyPI"
raise RuntimeError(msg, e)
return Version(version_str)
@ -75,7 +75,8 @@ def create_tag(build_type: str):
if __name__ == "__main__":
if len(sys.argv) != 2:
raise Exception("Specify base or main")
msg = "Specify base or main"
raise Exception(msg)
build_type = sys.argv[1]
tag = create_tag(build_type)

View file

@ -1,5 +1,5 @@
import sys
import re
import sys
from pathlib import Path
import packaging.version
@ -10,39 +10,38 @@ BASE_DIR = Path(__file__).parent.parent.parent
def update_base_dep(pyproject_path: str, new_version: str) -> None:
"""Update the langflow-base dependency in pyproject.toml."""
filepath = BASE_DIR / pyproject_path
content = filepath.read_text()
content = filepath.read_text(encoding="utf-8")
replacement = f'langflow-base-nightly = "{new_version}"'
# Updates the pattern for poetry
pattern = re.compile(r'langflow-base = \{ path = "\./src/backend/base", develop = true \}')
if not pattern.search(content):
raise Exception(f'langflow-base poetry dependency not found in "{filepath}"')
msg = f'langflow-base poetry dependency not found in "{filepath}"'
raise Exception(msg)
content = pattern.sub(replacement, content)
filepath.write_text(content)
filepath.write_text(content, encoding="utf-8")
def verify_pep440(version):
"""
Verify if version is PEP440 compliant.
"""Verify if version is PEP440 compliant.
https://github.com/pypa/packaging/blob/16.7/packaging/version.py#L191
"""
try:
return packaging.version.Version(version)
except packaging.version.InvalidVersion as e:
raise e
except packaging.version.InvalidVersion:
raise
def main() -> None:
if len(sys.argv) != 2:
raise Exception("New version not specified")
msg = "New version not specified"
raise Exception(msg)
base_version = sys.argv[1]
# Strip "v" prefix from version if present
if base_version.startswith("v"):
base_version = base_version[1:]
base_version = base_version.removeprefix("v")
verify_pep440(base_version)
update_base_dep("pyproject.toml", base_version)

View file

@ -1,5 +1,5 @@
import sys
import re
import sys
from pathlib import Path
BASE_DIR = Path(__file__).parent.parent.parent
@ -8,22 +8,23 @@ BASE_DIR = Path(__file__).parent.parent.parent
def update_pyproject_name(pyproject_path: str, new_project_name: str) -> None:
"""Update the project name in pyproject.toml."""
filepath = BASE_DIR / pyproject_path
content = filepath.read_text()
content = filepath.read_text(encoding="utf-8")
# Regex to match the version line under [tool.poetry]
pattern = re.compile(r'(?<=^name = ")[^"]+(?=")', re.MULTILINE)
if not pattern.search(content):
raise Exception(f'Project name not found in "{filepath}"')
msg = f'Project name not found in "{filepath}"'
raise Exception(msg)
content = pattern.sub(new_project_name, content)
filepath.write_text(content)
filepath.write_text(content, encoding="utf-8")
def update_uv_dep(pyproject_path: str, new_project_name: str) -> None:
"""Update the langflow-base dependency in pyproject.toml."""
filepath = BASE_DIR / pyproject_path
content = filepath.read_text()
content = filepath.read_text(encoding="utf-8")
if new_project_name == "langflow-nightly":
pattern = re.compile(r"langflow = \{ workspace = true \}")
@ -32,18 +33,21 @@ def update_uv_dep(pyproject_path: str, new_project_name: str) -> None:
pattern = re.compile(r"langflow-base = \{ workspace = true \}")
replacement = "langflow-base-nightly = { workspace = true }"
else:
raise ValueError(f"Invalid project name: {new_project_name}")
msg = f"Invalid project name: {new_project_name}"
raise ValueError(msg)
# Updates the dependency name for uv
if not pattern.search(content):
raise Exception(f"{replacement} uv dependency not found in {filepath}")
msg = f"{replacement} uv dependency not found in {filepath}"
raise Exception(msg)
content = pattern.sub(replacement, content)
filepath.write_text(content)
filepath.write_text(content, encoding="utf-8")
def main() -> None:
if len(sys.argv) != 3:
raise Exception("Must specify project name and build type, e.g. langflow-nightly base")
msg = "Must specify project name and build type, e.g. langflow-nightly base"
raise Exception(msg)
new_project_name = sys.argv[1]
build_type = sys.argv[2]
@ -54,7 +58,8 @@ def main() -> None:
update_pyproject_name("pyproject.toml", new_project_name)
update_uv_dep("pyproject.toml", new_project_name)
else:
raise ValueError(f"Invalid build type: {build_type}")
msg = f"Invalid build type: {build_type}"
raise ValueError(msg)
if __name__ == "__main__":

View file

@ -1,5 +1,5 @@
import sys
import re
import sys
from pathlib import Path
import packaging.version
@ -10,40 +10,39 @@ BASE_DIR = Path(__file__).parent.parent.parent
def update_pyproject_version(pyproject_path: str, new_version: str) -> None:
"""Update the version in pyproject.toml."""
filepath = BASE_DIR / pyproject_path
content = filepath.read_text()
content = filepath.read_text(encoding="utf-8")
# Regex to match the version line under [tool.poetry]
pattern = re.compile(r'(?<=^version = ")[^"]+(?=")', re.MULTILINE)
if not pattern.search(content):
raise Exception(f'Project version not found in "{filepath}"')
msg = f'Project version not found in "{filepath}"'
raise Exception(msg)
content = pattern.sub(new_version, content)
filepath.write_text(content)
filepath.write_text(content, encoding="utf-8")
def verify_pep440(version):
"""
Verify if version is PEP440 compliant.
"""Verify if version is PEP440 compliant.
https://github.com/pypa/packaging/blob/16.7/packaging/version.py#L191
"""
try:
return packaging.version.Version(version)
except packaging.version.InvalidVersion as e:
raise e
except packaging.version.InvalidVersion:
raise
def main() -> None:
if len(sys.argv) != 3:
raise Exception("New version not specified")
msg = "New version not specified"
raise Exception(msg)
new_version = sys.argv[1]
# Strip "v" prefix from version if present
if new_version.startswith("v"):
new_version = new_version[1:]
new_version = new_version.removeprefix("v")
build_type = sys.argv[2]
@ -54,7 +53,8 @@ def main() -> None:
elif build_type == "main":
update_pyproject_version("pyproject.toml", new_version)
else:
raise ValueError(f"Invalid build type: {build_type}")
msg = f"Invalid build type: {build_type}"
raise ValueError(msg)
if __name__ == "__main__":

View file

@ -1,5 +1,5 @@
import sys
import re
import sys
from pathlib import Path
BASE_DIR = Path(__file__).parent.parent.parent
@ -7,30 +7,31 @@ BASE_DIR = Path(__file__).parent.parent.parent
def update_uv_dep(base_version: str) -> None:
"""Update the langflow-base dependency in pyproject.toml."""
pyproject_path = BASE_DIR / "pyproject.toml"
# Read the pyproject.toml file content
content = pyproject_path.read_text()
content = pyproject_path.read_text(encoding="utf-8")
# For the main project, update the langflow-base dependency in the UV section
pattern = re.compile(r'(dependencies\s*=\s*\[\s*\n\s*)("langflow-base==[\d.]+")')
replacement = r'\1"langflow-base-nightly=={}"'.format(base_version)
replacement = rf'\1"langflow-base-nightly=={base_version}"'
# Check if the pattern is found
if not pattern.search(content):
raise Exception(f"{pattern} UV dependency not found in {pyproject_path}")
msg = f"{pattern} UV dependency not found in {pyproject_path}"
raise Exception(msg)
# Replace the matched pattern with the new one
content = pattern.sub(replacement, content)
# Write the updated content back to the file
pyproject_path.write_text(content)
pyproject_path.write_text(content, encoding="utf-8")
def main() -> None:
if len(sys.argv) != 2:
raise Exception("specify base version")
msg = "specify base version"
raise Exception(msg)
base_version = sys.argv[1]
base_version = base_version.lstrip("v")
update_uv_dep(base_version)

View file

@ -6,6 +6,7 @@
# ]
# ///
import argparse
import sys
from huggingface_hub import HfApi, list_models
from rich import print
@ -23,11 +24,11 @@ space = parsed_args.space
if not space:
print("Please provide a space to restart.")
exit()
sys.exit()
if not parsed_args.token:
print("Please provide an API token.")
exit()
sys.exit()
# Or configure a HfApi client
hf_api = HfApi(

View file

@ -1,6 +1,8 @@
import contextlib
def get_version() -> str:
"""
Retrieves the version of the package from a possible list of package names.
"""Retrieves the version of the package from a possible list of package names.
This accounts for after package names are updated for -nightly builds.
Returns:
@ -19,20 +21,18 @@ def get_version() -> str:
]
_version = None
for pkg_name in pkg_names:
try:
with contextlib.suppress(ImportError, metadata.PackageNotFoundError):
_version = metadata.version(pkg_name)
except (ImportError, metadata.PackageNotFoundError):
pass
if _version is None:
raise ValueError(f"Package not found from options {pkg_names}")
msg = f"Package not found from options {pkg_names}"
raise ValueError(msg)
return _version
def is_pre_release(v: str) -> bool:
"""
Returns a boolean indicating whether the version is a pre-release version,
"""Returns a boolean indicating whether the version is a pre-release version,
as per the definition of a pre-release segment from PEP 440.
"""
return any(label in v for label in ["a", "b", "rc"])

View file

@ -4,8 +4,7 @@ import os.path
def get_required_env_var(var: str) -> str:
"""
Get the value of the specified environment variable.
"""Get the value of the specified environment variable.
Args:
var (str): The environment variable to get.
@ -18,7 +17,8 @@ def get_required_env_var(var: str) -> str:
"""
value = os.getenv(var)
if not value:
raise ValueError(f"Environment variable {var} is not set")
msg = f"Environment variable {var} is not set"
raise ValueError(msg)
return value

View file

@ -16,13 +16,6 @@ from base.langflow.components.inputs.ChatInput import ChatInput
from dotenv import load_dotenv
from fastapi.testclient import TestClient
from httpx import ASGITransport, AsyncClient
from loguru import logger
from pytest import LogCaptureFixture
from sqlmodel import Session, SQLModel, create_engine, select
from sqlmodel.pool import StaticPool
from tests.api_keys import get_openai_api_key
from typer.testing import CliRunner
from langflow.graph.graph.base import Graph
from langflow.initial_setup.setup import STARTER_FOLDER_NAME
from langflow.services.auth.utils import get_password_hash
@ -34,6 +27,13 @@ from langflow.services.database.models.user.model import User, UserCreate, UserR
from langflow.services.database.models.vertex_builds.crud import delete_vertex_builds_by_flow_id
from langflow.services.database.utils import session_getter
from langflow.services.deps import get_db_service
from loguru import logger
from pytest import LogCaptureFixture
from sqlmodel import Session, SQLModel, create_engine, select
from sqlmodel.pool import StaticPool
from typer.testing import CliRunner
from tests.api_keys import get_openai_api_key
if TYPE_CHECKING:
from langflow.services.database.service import DatabaseService
@ -114,7 +114,7 @@ def caplog(caplog: LogCaptureFixture):
logger.remove(handler_id)
@pytest.fixture()
@pytest.fixture
async def async_client() -> AsyncGenerator:
from langflow.main import create_app
@ -188,8 +188,7 @@ def distributed_client_fixture(session: Session, monkeypatch, distributed_env):
def get_graph(_type="basic"):
"""Get a graph from a json file"""
"""Get a graph from a json file."""
if _type == "basic":
path = pytest.BASIC_EXAMPLE_PATH
elif _type == "complex":
@ -197,7 +196,7 @@ def get_graph(_type="basic"):
elif _type == "openapi":
path = pytest.OPENAPI_EXAMPLE_PATH
with path.open() as f:
with path.open(encoding="utf-8") as f:
flow_graph = json.load(f)
data_graph = flow_graph["data"]
nodes = data_graph["nodes"]
@ -209,13 +208,13 @@ def get_graph(_type="basic"):
@pytest.fixture
def basic_graph_data():
with pytest.BASIC_EXAMPLE_PATH.open() as f:
with pytest.BASIC_EXAMPLE_PATH.open(encoding="utf-8") as f:
return json.load(f)
@pytest.fixture
def basic_graph():
yield get_graph()
return get_graph()
@pytest.fixture
@ -230,47 +229,47 @@ def openapi_graph():
@pytest.fixture
def json_flow():
return pytest.BASIC_EXAMPLE_PATH.read_text()
return pytest.BASIC_EXAMPLE_PATH.read_text(encoding="utf-8")
@pytest.fixture
def grouped_chat_json_flow():
return pytest.GROUPED_CHAT_EXAMPLE_PATH.read_text()
return pytest.GROUPED_CHAT_EXAMPLE_PATH.read_text(encoding="utf-8")
@pytest.fixture
def one_grouped_chat_json_flow():
return pytest.ONE_GROUPED_CHAT_EXAMPLE_PATH.read_text()
return pytest.ONE_GROUPED_CHAT_EXAMPLE_PATH.read_text(encoding="utf-8")
@pytest.fixture
def vector_store_grouped_json_flow():
return pytest.VECTOR_STORE_GROUPED_EXAMPLE_PATH.read_text()
return pytest.VECTOR_STORE_GROUPED_EXAMPLE_PATH.read_text(encoding="utf-8")
@pytest.fixture
def json_flow_with_prompt_and_history():
return pytest.BASIC_CHAT_WITH_PROMPT_AND_HISTORY.read_text()
return pytest.BASIC_CHAT_WITH_PROMPT_AND_HISTORY.read_text(encoding="utf-8")
@pytest.fixture
def json_simple_api_test():
return pytest.SIMPLE_API_TEST.read_text()
return pytest.SIMPLE_API_TEST.read_text(encoding="utf-8")
@pytest.fixture
def json_vector_store():
return pytest.VECTOR_STORE_PATH.read_text()
return pytest.VECTOR_STORE_PATH.read_text(encoding="utf-8")
@pytest.fixture
def json_webhook_test():
return pytest.WEBHOOK_TEST.read_text()
return pytest.WEBHOOK_TEST.read_text(encoding="utf-8")
@pytest.fixture
def json_memory_chatbot_no_llm():
return pytest.MEMORY_CHATBOT_NO_LLM.read_text()
return pytest.MEMORY_CHATBOT_NO_LLM.read_text(encoding="utf-8")
@pytest.fixture(name="client")
@ -315,12 +314,12 @@ def session_getter_fixture(client):
with Session(db_service.engine) as session:
yield session
yield blank_session_getter
return blank_session_getter
@pytest.fixture
def runner():
yield CliRunner()
return CliRunner()
@pytest.fixture
@ -337,7 +336,7 @@ async def test_user(client):
await client.delete(f"/api/v1/users/{user['id']}")
@pytest.fixture(scope="function")
@pytest.fixture
def active_user(client):
db_manager = get_db_service()
with db_manager.with_session() as session:
@ -372,7 +371,7 @@ async def logged_in_headers(client, active_user):
assert response.status_code == 200
tokens = response.json()
a_token = tokens["access_token"]
yield {"Authorization": f"Bearer {a_token}"}
return {"Authorization": f"Bearer {a_token}"}
@pytest.fixture
@ -395,12 +394,12 @@ def flow(client, json_flow: str, active_user):
@pytest.fixture
def json_chat_input():
return pytest.CHAT_INPUT.read_text()
return pytest.CHAT_INPUT.read_text(encoding="utf-8")
@pytest.fixture
def json_two_outputs():
return pytest.TWO_OUTPUTS.read_text()
return pytest.TWO_OUTPUTS.read_text(encoding="utf-8")
@pytest.fixture
@ -528,7 +527,8 @@ def get_starter_project(active_user):
.where(Flow.name == "Basic Prompting (Hello, World)")
).first()
if not flow:
raise ValueError("No starter project found")
msg = "No starter project found"
raise ValueError(msg)
# ensure openai api key is set
get_openai_api_key()

View file

@ -1,18 +1,14 @@
import os
from astrapy.db import AstraDB
import pytest
from astrapy.db import AstraDB
from langchain_core.documents import Document
from langflow.components.embeddings import OpenAIEmbeddingsComponent
from langflow.components.vectorstores import AstraVectorStoreComponent
from tests.api_keys import get_astradb_application_token, get_astradb_api_endpoint, get_openai_api_key
from tests.integration.components.mock_components import TextToData
from tests.integration.utils import ComponentInputHandle
from langchain_core.documents import Document
from langflow.schema.data import Data
from tests.integration.utils import run_single_component
from tests.api_keys import get_astradb_api_endpoint, get_astradb_application_token, get_openai_api_key
from tests.integration.components.mock_components import TextToData
from tests.integration.utils import ComponentInputHandle, run_single_component
BASIC_COLLECTION = "test_basic"
SEARCH_COLLECTION = "test_search"
@ -30,7 +26,7 @@ ALL_COLLECTIONS = [
]
@pytest.fixture()
@pytest.fixture
def astradb_client(request):
client = AstraDB(api_endpoint=get_astradb_api_endpoint(), token=get_astradb_application_token())
yield client
@ -139,7 +135,7 @@ def test_astra_vectorize():
@pytest.mark.api_key_required
def test_astra_vectorize_with_provider_api_key():
"""tests vectorize using an openai api key"""
"""Tests vectorize using an openai api key."""
from langchain_astradb import AstraDBVectorStore, CollectionVectorServiceOptions
application_token = get_astradb_application_token()
@ -196,7 +192,7 @@ def test_astra_vectorize_with_provider_api_key():
@pytest.mark.api_key_required
def test_astra_vectorize_passes_authentication():
"""tests vectorize using the authentication parameter"""
"""Tests vectorize using the authentication parameter."""
from langchain_astradb import AstraDBVectorStore, CollectionVectorServiceOptions
store = None

View file

@ -1,10 +1,9 @@
import pytest
from langflow.components.helpers.ParseJSONData import ParseJSONDataComponent
from langflow.components.inputs import ChatInput
from langflow.schema import Data
from tests.integration.components.mock_components import TextToData
from tests.integration.utils import run_single_component, ComponentInputHandle
from tests.integration.utils import ComponentInputHandle, run_single_component
@pytest.mark.asyncio

View file

@ -1,10 +1,9 @@
import pytest
from langflow.components.inputs import ChatInput
from langflow.memory import get_messages
from langflow.schema.message import Message
from tests.integration.utils import run_single_component
from langflow.components.inputs import ChatInput
import pytest
@pytest.mark.asyncio
async def test_default():

View file

@ -1,14 +1,12 @@
import pytest
from langflow.components.inputs import TextInputComponent
from langflow.schema.message import Message
from tests.integration.utils import run_single_component
from langflow.components.inputs import TextInputComponent
import pytest
@pytest.mark.asyncio
async def test_text_input():
outputs = await run_single_component(TextInputComponent, run_input="sample text", input_type="text")
print(outputs)
assert isinstance(outputs["text"], Message)
assert outputs["text"].text == "sample text"
assert outputs["text"].sender is None

View file

@ -1,8 +1,7 @@
import json
from typing import List
from langflow.custom import Component
from langflow.inputs import StrInput, BoolInput
from langflow.inputs import BoolInput, StrInput
from langflow.schema import Data
from langflow.template import Output
@ -21,5 +20,5 @@ class TextToData(Component):
return Data(data=json.loads(text))
return Data(text=text)
def create_data(self) -> List[Data]:
def create_data(self) -> list[Data]:
return [self._to_data(t) for t in self.text_data]

View file

@ -1,6 +1,6 @@
import os
import pytest
import pytest
from langflow.components.models.OpenAIModel import OpenAIModelComponent
from langflow.components.output_parsers.OutputParser import OutputParserComponent
from langflow.components.prompts.Prompt import PromptComponent
@ -23,7 +23,7 @@ async def test_csv_output_parser_openai():
prompt_handler = ComponentInputHandle(
clazz=PromptComponent,
inputs={
"template": "List the first five positive integers.\n\n{format_instructions}",
"template": "List the first five positive integers.\n\n{format_instructions}", # noqa: RUF027
"format_instructions": format_instructions,
},
output_name="prompt",

View file

@ -1,10 +1,9 @@
import pytest
from langflow.components.outputs import ChatOutput
from langflow.memory import get_messages
from langflow.schema.message import Message
from tests.integration.utils import run_single_component
import pytest
@pytest.mark.asyncio
async def test_string():

View file

@ -1,9 +1,8 @@
import pytest
from langflow.components.outputs import TextOutputComponent
from langflow.schema.message import Message
from tests.integration.utils import run_single_component
import pytest
@pytest.mark.asyncio
async def test():

View file

@ -1,13 +1,11 @@
import pytest
from langflow.components.prompts import PromptComponent
from langflow.schema.message import Message
from tests.integration.utils import run_single_component
import pytest
@pytest.mark.asyncio
async def test():
outputs = await run_single_component(PromptComponent, inputs={"template": "test {var1}", "var1": "from the var"})
print(outputs)
assert isinstance(outputs["prompt"], Message)
assert outputs["prompt"].text == "test from the var"

View file

@ -1,5 +1,4 @@
import pytest
from langflow.components.inputs import ChatInput
from langflow.components.outputs import ChatOutput
from langflow.components.prompts import PromptComponent

View file

@ -3,7 +3,6 @@ from uuid import uuid4
import pytest
from fastapi import status
from fastapi.testclient import TestClient
from langflow.graph.schema import RunOutputs
from langflow.initial_setup.setup import load_starter_projects
from langflow.load import run_flow_from_json
@ -80,9 +79,8 @@ async def test_run_with_inputs_and_outputs(client, starter_project, created_api_
@pytest.mark.noclient
@pytest.mark.api_key_required
def test_run_flow_from_json_object():
"""Test loading a flow from a json file and applying tweaks"""
_, projects = zip(*load_starter_projects())
project = [project for project in projects if "Basic Prompting" in project["name"]][0]
"""Test loading a flow from a json file and applying tweaks."""
project = next(project for _, project in load_starter_projects() if "Basic Prompting" in project["name"])
results = run_flow_from_json(project, input_value="test", fallback_to_env_vars=True)
assert results is not None
assert all(isinstance(result, RunOutputs) for result in results)

Some files were not shown because too many files have changed in this diff Show more