feat: Add various ruff rules (#3974)

Add various ruff rules
This commit is contained in:
Christophe Bornet 2024-10-01 14:28:19 +02:00 • committed by GitHub
commit d92b3cb12b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
15 changed files with 51 additions and 25 deletions

View file

@ -461,7 +461,7 @@ def migration(
if not typer.confirm(
"This will delete all data necessary to fix migrations. Are you sure you want to continue?"
):
raise typer.Abort()
raise typer.Abort
initialize_services(fix_migration=fix)
db_service = get_db_service()

View file

@ -50,7 +50,6 @@ class LCAgentComponent(Component):
@abstractmethod
def build_agent(self) -> AgentExecutor:
"""Create the agent."""
pass
async def message_response(self) -> Message:
"""Run the agent and return the response."""
@ -156,4 +155,3 @@ class LCToolsAgentComponent(LCAgentComponent):
@abstractmethod
def create_agent_runnable(self) -> Runnable:
"""Create the agent."""
pass

View file

@ -140,7 +140,7 @@ def parse_text_file_to_data(file_path: str, silent_errors: bool) -> Data | None:
text = [normalize_text(item) if isinstance(item, str) else item for item in text]
text = orjson.dumps(text).decode("utf-8")
elif file_path.endswith(".yaml") or file_path.endswith(".yml"):
elif file_path.endswith((".yaml", ".yml")):
text = yaml.safe_load(text)
elif file_path.endswith(".xml"):
xml_element = ET.fromstring(text)

View file

@ -39,11 +39,9 @@ class LCDocumentTransformerComponent(Component):
"""
Get the data input.
"""
pass
@abstractmethod
def build_document_transformer(self) -> BaseDocumentTransformer:
"""
Build the text splitter.
"""
pass

View file

@ -28,11 +28,9 @@ class LCToolComponent(Component):
"""
Run model and return the output.
"""
pass
@abstractmethod
def build_tool(self) -> Tool | Sequence[Tool]:
"""
Build the tool.
"""
pass

View file

@ -26,4 +26,3 @@ class LCTextSplitterComponent(LCDocumentTransformerComponent):
"""
Build the text splitter.
"""
pass

View file

@ -58,7 +58,7 @@ class RetrievalQAComponent(LCChainComponent):
result_str = str(result.get("result", ""))
if self.return_source_documents and len(source_docs):
references_str = self.create_references_from_data(source_docs)
result_str = "\n".join([result_str, references_str])
result_str = f"{result_str}\n{references_str}"
# put the entire result to debug history, query and content
self.status = {**result, "source_documents": source_docs, "output": result_str}
return result_str

View file

@ -343,7 +343,6 @@ class CodeParser:
nodes.append(class_node)
except Exception as exc:
logger.error(f"Error finding base class node: {exc}")
pass
nodes.insert(0, node)
class_details = ClassCodeDetails(
name=node.name,

View file

@ -128,7 +128,7 @@ def get_field_properties(extra_field):
def process_type(field_type: str):
if field_type.startswith("list") or field_type.startswith("List"):
if field_type.startswith(("list", "List")):
return extract_inner_type(field_type)
# field_type is a string can be Prompt or Code too

View file

@ -18,7 +18,7 @@ from langflow.schema.table import Column, TableSchema
class FieldTypes(str, Enum):
TEXT = "str"
INTEGER = "int"
PASSWORD = "str"
PASSWORD = "str" # noqa: PIE796
FLOAT = "float"
BOOLEAN = "bool"
DICT = "dict"

View file

@ -1,3 +1,4 @@
import asyncio
from pathlib import Path
from loguru import logger
@ -33,9 +34,12 @@ class LocalStorageService(StorageService):
folder_path.mkdir(parents=True, exist_ok=True)
file_path = folder_path / file_name
try:
def write_file(file_path: Path, data: bytes) -> None:
with open(file_path, "wb") as f:
f.write(data)
try:
await asyncio.get_event_loop().run_in_executor(None, write_file, file_path, data)
logger.info(f"File {file_name} saved successfully in flow {flow_id}.")
except Exception as e:
logger.error(f"Error saving file {file_name} in flow {flow_id}: {e}")
@ -55,10 +59,14 @@ class LocalStorageService(StorageService):
logger.warning(f"File {file_name} not found in flow {flow_id}.")
raise FileNotFoundError(f"File {file_name} not found in flow {flow_id}")
def read_file(file_path: Path) -> bytes:
with open(file_path, "rb") as f:
logger.debug(f"File {file_name} retrieved successfully from flow {flow_id}.")
return f.read()
content = await asyncio.get_event_loop().run_in_executor(None, read_file, file_path)
logger.debug(f"File {file_name} retrieved successfully from flow {flow_id}.")
return content
async def list_files(self, flow_id: str):
"""
List all files in a specified flow.
@ -92,4 +100,4 @@ class LocalStorageService(StorageService):
async def teardown(self):
"""Perform any cleanup operations when the service is being torn down."""
pass # No specific teardown actions required for local
# No specific teardown actions required for local

View file

@ -86,4 +86,3 @@ class S3StorageService(StorageService):
async def teardown(self):
"""Perform any cleanup operations when the service is being torn down."""
# No specific teardown actions required for S3 storage at the moment.
pass

View file

@ -330,7 +330,7 @@ class StoreService(Service):
async def download(self, api_key: str, component_id: UUID) -> DownloadComponentResponse:
url = f"{self.components_url}/{component_id}"
params = {"fields": ",".join(["id", "name", "description", "data", "is_component", "metadata"])}
params = {"fields": "id,name,description,data,is_component,metadata"}
if not self.download_webhook_url:
raise ValueError("DOWNLOAD_WEBHOOK_URL is not set")
component, _ = await self._get(url, api_key, params)
@ -420,14 +420,14 @@ class StoreService(Service):
async def get_tags(self) -> list[dict[str, Any]]:
url = f"{self.base_url}/items/tags"
params = {"fields": ",".join(["id", "name"])}
params = {"fields": "id,name"}
tags, _ = await self._get(url, api_key=None, params=params)
return tags
async def get_user_likes(self, api_key: str) -> list[dict[str, Any]]:
url = f"{self.base_url}/users/me"
params = {
"fields": ",".join(["id", "likes"]),
"fields": "id,likes",
}
likes, _ = await self._get(url, api_key, params)
return likes
@ -436,7 +436,7 @@ class StoreService(Service):
url = f"{self.components_url}/{component_id}"
params = {
"fields": ",".join(["id", "count(liked_by)"]),
"fields": "id,count(liked_by)",
}
result, _ = await self._get(url, api_key=api_key, params=params)
if len(result) == 0:

View file

@ -148,7 +148,34 @@ exclude = ["langflow/alembic"]
line-length = 120
[tool.ruff.lint]
select = ["C4", "E", "F", "I", "UP"]
select = [
"ASYNC",
"C4",
"COM",
"DJ",
"E",
"F",
"FLY",
"FURB",
"I",
"ICN",
"INT",
"LOG",
"NPY",
"PD",
"PIE",
"Q",
"RSE",
"SLOT",
"T10",
"TID",
"UP",
"W",
"YTT"
]
ignore = [
"COM812", # Messes with the formatter
]
[build-system]
requires = ["hatchling"]