From bdb4106ac0dbc21c6408df1e58636eb35265c13f Mon Sep 17 00:00:00 2001 From: Christophe Bornet Date: Sat, 19 Oct 2024 17:52:04 +0200 Subject: [PATCH] ref: Add ruff rules for naming (N) (#4187) Add ruff rules for naming (N) --- src/backend/base/langflow/api/v1/chat.py | 66 +++++++++---------- src/backend/base/langflow/api/v1/endpoints.py | 38 ++++++----- src/backend/base/langflow/api/v1/files.py | 2 +- src/backend/base/langflow/api/v1/schemas.py | 6 +- src/backend/base/langflow/api/v1/store.py | 12 ++-- .../langchain_utilities/FirecrawlCrawlApi.py | 4 +- .../langchain_utilities/FirecrawlScrapeApi.py | 4 +- .../components/tools/AstraDBCQLTool.py | 4 +- .../tools/PythonCodeStructuredTool.py | 6 +- .../langflow/components/tools/SearXNGTool.py | 4 +- .../custom/custom_component/component.py | 4 +- .../base/langflow/custom/tree_visitor.py | 3 + src/backend/base/langflow/exceptions/api.py | 2 +- .../base/langflow/exceptions/component.py | 2 +- .../base/langflow/graph/graph/ascii.py | 20 +++--- src/backend/base/langflow/graph/graph/base.py | 20 +++--- .../base/langflow/graph/graph/constants.py | 2 +- src/backend/base/langflow/graph/schema.py | 10 --- .../base/langflow/graph/vertex/base.py | 6 +- .../base/langflow/graph/vertex/exceptions.py | 2 +- .../base/langflow/graph/vertex/types.py | 6 +- src/backend/base/langflow/inputs/utils.py | 6 +- .../base/langflow/interface/listing.py | 4 -- src/backend/base/langflow/schema/dotdict.py | 2 +- src/backend/base/langflow/schema/message.py | 4 +- .../base/langflow/services/auth/utils.py | 4 +- .../services/database/models/flow/model.py | 8 ++- .../base/langflow/services/settings/base.py | 1 + .../langflow/services/store/exceptions.py | 10 +-- .../base/langflow/services/task/service.py | 9 ++- .../langflow/services/telemetry/schema.py | 34 +++++----- .../langflow/services/telemetry/service.py | 10 +-- .../base/langflow/template/field/base.py | 1 + src/backend/base/langflow/utils/schemas.py | 1 + src/backend/base/pyproject.toml | 4 +- 35 files changed, 159 insertions(+), 162 deletions(-) diff --git a/src/backend/base/langflow/api/v1/chat.py b/src/backend/base/langflow/api/v1/chat.py index 51bc435d6..11e7dd2e0 100644 --- a/src/backend/base/langflow/api/v1/chat.py +++ b/src/backend/base/langflow/api/v1/chat.py @@ -34,7 +34,7 @@ from langflow.api.v1.schemas import ( VerticesOrderResponse, ) from langflow.events.event_manager import EventManager, create_default_event_manager -from langflow.exceptions.component import ComponentBuildException +from langflow.exceptions.component import ComponentBuildError from langflow.graph.graph.base import Graph from langflow.graph.utils import log_vertex_build from langflow.schema.schema import OutputValue @@ -117,9 +117,9 @@ async def retrieve_vertices_order( background_tasks.add_task( telemetry_service.log_package_playground, PlaygroundPayload( - playgroundSeconds=int(time.perf_counter() - start_time), - playgroundComponentCount=components_count, - playgroundSuccess=True, + playground_seconds=int(time.perf_counter() - start_time), + playground_component_count=components_count, + playground_success=True, ), ) return VerticesOrderResponse(ids=graph.first_layer, run_id=graph.run_id, vertices_to_run=vertices_to_run) @@ -127,10 +127,10 @@ async def retrieve_vertices_order( background_tasks.add_task( telemetry_service.log_package_playground, PlaygroundPayload( - playgroundSeconds=int(time.perf_counter() - start_time), - playgroundComponentCount=components_count, - playgroundSuccess=False, - playgroundErrorMessage=str(exc), + playground_seconds=int(time.perf_counter() - start_time), + playground_component_count=components_count, + playground_success=False, + playground_error_message=str(exc), ), ) if "stream or streaming set to True" in str(exc): @@ -188,19 +188,19 @@ async def build_flow( background_tasks.add_task( telemetry_service.log_package_playground, PlaygroundPayload( - playgroundSeconds=int(time.perf_counter() - start_time), - playgroundComponentCount=components_count, - playgroundSuccess=True, + playground_seconds=int(time.perf_counter() - start_time), + playground_component_count=components_count, + playground_success=True, ), ) except Exception as exc: background_tasks.add_task( telemetry_service.log_package_playground, PlaygroundPayload( - playgroundSeconds=int(time.perf_counter() - start_time), - playgroundComponentCount=components_count, - playgroundSuccess=False, - playgroundErrorMessage=str(exc), + playground_seconds=int(time.perf_counter() - start_time), + playground_component_count=components_count, + playground_success=False, + playground_error_message=str(exc), ), ) if "stream or streaming set to True" in str(exc): @@ -239,7 +239,7 @@ async def build_flow( result_data_response = ResultDataResponse.model_validate(result_dict, from_attributes=True) except Exception as exc: # noqa: BLE001 - if isinstance(exc, ComponentBuildException): + if isinstance(exc, ComponentBuildError): params = exc.message tb = exc.formatted_traceback else: @@ -301,20 +301,20 @@ async def build_flow( background_tasks.add_task( telemetry_service.log_package_component, ComponentPayload( - componentName=vertex_id.split("-")[0], - componentSeconds=int(time.perf_counter() - start_time), - componentSuccess=valid, - componentErrorMessage=error_message, + component_name=vertex_id.split("-")[0], + component_seconds=int(time.perf_counter() - start_time), + component_success=valid, + component_error_message=error_message, ), ) except Exception as exc: background_tasks.add_task( telemetry_service.log_package_component, ComponentPayload( - componentName=vertex_id.split("-")[0], - componentSeconds=int(time.perf_counter() - start_time), - componentSuccess=False, - componentErrorMessage=str(exc), + component_name=vertex_id.split("-")[0], + component_seconds=int(time.perf_counter() - start_time), + component_success=False, + component_error_message=str(exc), ), ) logger.exception("Error building Component") @@ -524,7 +524,7 @@ async def build_vertex( top_level_vertices = graph.get_top_level_vertices(next_runnable_vertices) result_data_response = ResultDataResponse.model_validate(result_dict, from_attributes=True) except Exception as exc: # noqa: BLE001 - if isinstance(exc, ComponentBuildException): + if isinstance(exc, ComponentBuildError): params = exc.message tb = exc.formatted_traceback else: @@ -590,20 +590,20 @@ async def build_vertex( background_tasks.add_task( telemetry_service.log_package_component, ComponentPayload( - componentName=vertex_id.split("-")[0], - componentSeconds=int(time.perf_counter() - start_time), - componentSuccess=valid, - componentErrorMessage=error_message, + component_name=vertex_id.split("-")[0], + component_seconds=int(time.perf_counter() - start_time), + component_success=valid, + component_error_message=error_message, ), ) except Exception as exc: background_tasks.add_task( telemetry_service.log_package_component, ComponentPayload( - componentName=vertex_id.split("-")[0], - componentSeconds=int(time.perf_counter() - start_time), - componentSuccess=False, - componentErrorMessage=str(exc), + component_name=vertex_id.split("-")[0], + component_seconds=int(time.perf_counter() - start_time), + component_success=False, + component_error_message=str(exc), ), ) logger.exception("Error building Component") diff --git a/src/backend/base/langflow/api/v1/endpoints.py b/src/backend/base/langflow/api/v1/endpoints.py index 5d442fe65..e9a02712f 100644 --- a/src/backend/base/langflow/api/v1/endpoints.py +++ b/src/backend/base/langflow/api/v1/endpoints.py @@ -26,7 +26,7 @@ from langflow.api.v1.schemas import ( ) from langflow.custom.custom_component.component import Component from langflow.custom.utils import build_custom_component_template, get_instance_name -from langflow.exceptions.api import APIException, InvalidChatInputException +from langflow.exceptions.api import APIException, InvalidChatInputError from langflow.graph.graph.base import Graph from langflow.graph.schema import RunOutputs from langflow.helpers.flow import get_flow_by_id_or_endpoint_name @@ -87,13 +87,13 @@ def validate_input_and_tweaks(input_request: SimplifiedAPIRequest): input_value_is_chat = input_request.input_value is not None and input_request.input_type == "chat" if has_input_value and input_value_is_chat: msg = "If you pass an input_value to the chat input, you cannot pass a tweak with the same name." - raise InvalidChatInputException(msg) + raise InvalidChatInputError(msg) elif ("Text Input" in key or "TextInput" in key) and isinstance(value, dict): has_input_value = value.get("input_value") is not None input_value_is_text = input_request.input_value is not None and input_request.input_type == "text" if has_input_value and input_value_is_text: msg = "If you pass an input_value to the text input, you cannot pass a tweak with the same name." - raise InvalidChatInputException(msg) + raise InvalidChatInputError(msg) async def simple_run_flow( @@ -253,17 +253,19 @@ async def simplified_run_flow( end_time = time.perf_counter() background_tasks.add_task( telemetry_service.log_package_run, - RunPayload(runIsWebhook=False, runSeconds=int(end_time - start_time), runSuccess=True, runErrorMessage=""), + RunPayload( + run_is_webhook=False, run_seconds=int(end_time - start_time), run_success=True, run_error_message="" + ), ) except ValueError as exc: background_tasks.add_task( telemetry_service.log_package_run, RunPayload( - runIsWebhook=False, - runSeconds=int(time.perf_counter() - start_time), - runSuccess=False, - runErrorMessage=str(exc), + run_is_webhook=False, + run_seconds=int(time.perf_counter() - start_time), + run_success=False, + run_error_message=str(exc), ), ) if "badly formed hexadecimal UUID string" in str(exc): @@ -272,16 +274,16 @@ async def simplified_run_flow( if "not found" in str(exc): raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=str(exc)) from exc raise APIException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, exception=exc, flow=flow) from exc - except InvalidChatInputException as exc: + except InvalidChatInputError as exc: raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(exc)) from exc except Exception as exc: background_tasks.add_task( telemetry_service.log_package_run, RunPayload( - runIsWebhook=False, - runSeconds=int(time.perf_counter() - start_time), - runSuccess=False, - runErrorMessage=str(exc), + run_is_webhook=False, + run_seconds=int(time.perf_counter() - start_time), + run_success=False, + run_error_message=str(exc), ), ) raise APIException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, exception=exc, flow=flow) from exc @@ -355,10 +357,10 @@ async def webhook_run_flow( background_tasks.add_task( telemetry_service.log_package_run, RunPayload( - runIsWebhook=True, - runSeconds=int(time.perf_counter() - start_time), - runSuccess=error_msg == "", - runErrorMessage=error_msg, + run_is_webhook=True, + run_seconds=int(time.perf_counter() - start_time), + run_success=error_msg == "", + run_error_message=error_msg, ), ) @@ -548,7 +550,7 @@ async def create_upload_file( file_path = save_uploaded_file(file, folder_name=flow_id_str) return UploadFileResponse( - flowId=flow_id_str, + flow_id=flow_id_str, file_path=file_path, ) except Exception as exc: diff --git a/src/backend/base/langflow/api/v1/files.py b/src/backend/base/langflow/api/v1/files.py index 59196982a..105e66add 100644 --- a/src/backend/base/langflow/api/v1/files.py +++ b/src/backend/base/langflow/api/v1/files.py @@ -71,7 +71,7 @@ async def upload_file( full_file_name = f"{timestamp}_{file_name}" folder = flow_id_str await storage_service.save_file(flow_id=folder, file_name=full_file_name, data=file_content) - return UploadFileResponse(flowId=flow_id_str, file_path=f"{folder}/{full_file_name}") + return UploadFileResponse(flow_id=flow_id_str, file_path=f"{folder}/{full_file_name}") except Exception as e: raise HTTPException(status_code=500, detail=str(e)) from e diff --git a/src/backend/base/langflow/api/v1/schemas.py b/src/backend/base/langflow/api/v1/schemas.py index c909038d6..d74308e5a 100644 --- a/src/backend/base/langflow/api/v1/schemas.py +++ b/src/backend/base/langflow/api/v1/schemas.py @@ -93,7 +93,7 @@ class ChatMessage(BaseModel): is_bot: bool = False message: str | None | dict = None - chatKey: str | None = None + chat_key: str | None = Field(None, serialization_alias="chatKey") type: str = "human" @@ -159,7 +159,7 @@ class FlowListReadWithFolderName(BaseModel): class InitResponse(BaseModel): - flowId: str + flow_id: str = Field(serialization_alias="flowId") class BuiltResponse(BaseModel): @@ -169,7 +169,7 @@ class BuiltResponse(BaseModel): class UploadFileResponse(BaseModel): """Upload file response schema.""" - flowId: str + flow_id: str = Field(serialization_alias="flowId") file_path: Path diff --git a/src/backend/base/langflow/api/v1/store.py b/src/backend/base/langflow/api/v1/store.py index e7889f967..9fd8141f2 100644 --- a/src/backend/base/langflow/api/v1/store.py +++ b/src/backend/base/langflow/api/v1/store.py @@ -8,7 +8,7 @@ from langflow.api.utils import check_langflow_version from langflow.services.auth import utils as auth_utils from langflow.services.database.models.user.model import User from langflow.services.deps import get_settings_service, get_store_service -from langflow.services.store.exceptions import CustomException +from langflow.services.store.exceptions import CustomError from langflow.services.store.schema import ( CreateComponentResponse, DownloadComponentResponse, @@ -131,7 +131,7 @@ async def get_components( limit=limit, store_api_key=store_api_key, ) - except CustomException as exc: + except CustomError as exc: raise HTTPException(status_code=exc.status_code, detail=str(exc)) from exc except Exception as exc: raise HTTPException(status_code=500, detail=str(exc)) from exc @@ -145,7 +145,7 @@ async def download_component( ): try: component = await store_service.download(store_api_key, component_id) - except CustomException as exc: + except CustomError as exc: raise HTTPException(status_code=400, detail=str(exc)) from exc except Exception as exc: raise HTTPException(status_code=500, detail=str(exc)) from exc @@ -162,7 +162,7 @@ async def get_tags( ): try: return await store_service.get_tags() - except CustomException as exc: + except CustomError as exc: raise HTTPException(status_code=400, detail=str(exc)) from exc except Exception as exc: raise HTTPException(status_code=500, detail=str(exc)) from exc @@ -175,7 +175,7 @@ async def get_list_of_components_liked_by_user( ): try: return await store_service.get_user_likes(store_api_key) - except CustomException as exc: + except CustomError as exc: raise HTTPException(status_code=400, detail=str(exc)) from exc except Exception as exc: raise HTTPException(status_code=500, detail=str(exc)) from exc @@ -192,7 +192,7 @@ async def like_component( likes_count = await store_service.get_component_likes_count(str(component_id), store_api_key) return UsersLikesResponse(likes_count=likes_count, liked_by_user=result) - except CustomException as exc: + except CustomError as exc: raise HTTPException(status_code=exc.status_code, detail=str(exc)) from exc except Exception as exc: raise HTTPException(status_code=500, detail=str(exc)) from exc diff --git a/src/backend/base/langflow/components/langchain_utilities/FirecrawlCrawlApi.py b/src/backend/base/langflow/components/langchain_utilities/FirecrawlCrawlApi.py index e199837c6..f1a25a89a 100644 --- a/src/backend/base/langflow/components/langchain_utilities/FirecrawlCrawlApi.py +++ b/src/backend/base/langflow/components/langchain_utilities/FirecrawlCrawlApi.py @@ -50,8 +50,8 @@ class FirecrawlCrawlApi(CustomComponent): api_key: str, url: str, timeout: int = 30000, - crawlerOptions: Data | None = None, - pageOptions: Data | None = None, + crawlerOptions: Data | None = None, # noqa: N803 + pageOptions: Data | None = None, # noqa: N803 idempotency_key: str | None = None, ) -> Data: try: diff --git a/src/backend/base/langflow/components/langchain_utilities/FirecrawlScrapeApi.py b/src/backend/base/langflow/components/langchain_utilities/FirecrawlScrapeApi.py index f03c273fd..05ecb3cef 100644 --- a/src/backend/base/langflow/components/langchain_utilities/FirecrawlScrapeApi.py +++ b/src/backend/base/langflow/components/langchain_utilities/FirecrawlScrapeApi.py @@ -44,8 +44,8 @@ class FirecrawlScrapeApi(CustomComponent): api_key: str, url: str, timeout: int = 10000, - pageOptions: Data | None = None, - extractorOptions: Data | None = None, + pageOptions: Data | None = None, # noqa: N803 + extractorOptions: Data | None = None, # noqa: N803 ) -> Data: try: from firecrawl.firecrawl import FirecrawlApp diff --git a/src/backend/base/langflow/components/tools/AstraDBCQLTool.py b/src/backend/base/langflow/components/tools/AstraDBCQLTool.py index f5da5f548..384bce4eb 100644 --- a/src/backend/base/langflow/components/tools/AstraDBCQLTool.py +++ b/src/backend/base/langflow/components/tools/AstraDBCQLTool.py @@ -89,7 +89,7 @@ class AstraDBCQLToolComponent(LCToolComponent): def astra_rest(self, args): headers = {"Accept": "application/json", "X-Cassandra-Token": f"{self.token}"} - ASTRA_URL = f"{self.api_endpoint}/api/rest/v2/keyspaces/{self.keyspace}/{self.table_name}/" + astra_url = f"{self.api_endpoint}/api/rest/v2/keyspaces/{self.keyspace}/{self.table_name}/" key = [] # Partition keys are mandatory for k in self.partition_keys: @@ -108,7 +108,7 @@ class AstraDBCQLToolComponent(LCToolComponent): elif self.static_filters[k] is not None: key.append(self.static_filters[k]) - url = f'{ASTRA_URL}{"/".join(key)}?page-size={self.number_of_results}' + url = f'{astra_url}{"/".join(key)}?page-size={self.number_of_results}' if self.projection_fields != "*": url += f'&fields={urllib.parse.quote(self.projection_fields.replace(" ","")) }' diff --git a/src/backend/base/langflow/components/tools/PythonCodeStructuredTool.py b/src/backend/base/langflow/components/tools/PythonCodeStructuredTool.py index 3ee4474f3..27bea1f5e 100644 --- a/src/backend/base/langflow/components/tools/PythonCodeStructuredTool.py +++ b/src/backend/base/langflow/components/tools/PythonCodeStructuredTool.py @@ -198,13 +198,13 @@ class PythonCodeStructuredTool(LCToolComponent): if "temp_annotation_type" in _globals: _globals.pop("temp_annotation_type") - PythonCodeToolSchema = None + python_code_tool_schema = None if schema_fields: - PythonCodeToolSchema = create_model("PythonCodeToolSchema", **schema_fields) + python_code_tool_schema = create_model("PythonCodeToolSchema", **schema_fields) return StructuredTool.from_function( func=_local[self.tool_function].run, - args_schema=PythonCodeToolSchema, + args_schema=python_code_tool_schema, name=self.tool_name, description=self.tool_description, return_direct=self.return_direct, diff --git a/src/backend/base/langflow/components/tools/SearXNGTool.py b/src/backend/base/langflow/components/tools/SearXNGTool.py index 5ca03e96a..ef2935b7f 100644 --- a/src/backend/base/langflow/components/tools/SearXNGTool.py +++ b/src/backend/base/langflow/components/tools/SearXNGTool.py @@ -129,11 +129,11 @@ class SearXNGToolComponent(LCToolComponent): "categories": (list[str], Field(default=[], description="The categories to search in.")), } - SearxSearchSchema = create_model("SearxSearchSchema", **schema_fields) + searx_search_schema = create_model("SearxSearchSchema", **schema_fields) return StructuredTool.from_function( func=_local["SearxSearch"].search, - args_schema=SearxSearchSchema, + args_schema=searx_search_schema, name="searxng_search_tool", description="A tool that searches for tools using SearXNG.\nThe available categories are: " + ", ".join(self.categories), diff --git a/src/backend/base/langflow/custom/custom_component/component.py b/src/backend/base/langflow/custom/custom_component/component.py index 7f4793d60..fa9a0bb87 100644 --- a/src/backend/base/langflow/custom/custom_component/component.py +++ b/src/backend/base/langflow/custom/custom_component/component.py @@ -801,8 +801,8 @@ class Component(CustomComponent): return Input(**kwargs) def to_toolkit(self) -> list[Tool]: - ComponentToolkit = _get_component_toolkit() - return ComponentToolkit(component=self).get_tools() + component_toolkit = _get_component_toolkit() + return component_toolkit(component=self).get_tools() def get_project_name(self): if hasattr(self, "_tracing_service") and self._tracing_service: diff --git a/src/backend/base/langflow/custom/tree_visitor.py b/src/backend/base/langflow/custom/tree_visitor.py index 48ef5f852..8bd14526f 100644 --- a/src/backend/base/langflow/custom/tree_visitor.py +++ b/src/backend/base/langflow/custom/tree_visitor.py @@ -1,11 +1,14 @@ import ast +from typing_extensions import override + class RequiredInputsVisitor(ast.NodeVisitor): def __init__(self, inputs): self.inputs = inputs self.required_inputs = set() + @override def visit_Attribute(self, node): if isinstance(node.value, ast.Name) and node.value.id == "self" and node.attr in self.inputs: self.required_inputs.add(node.attr) diff --git a/src/backend/base/langflow/exceptions/api.py b/src/backend/base/langflow/exceptions/api.py index eab4e95ed..1997ad157 100644 --- a/src/backend/base/langflow/exceptions/api.py +++ b/src/backend/base/langflow/exceptions/api.py @@ -6,7 +6,7 @@ from langflow.services.database.models.flow.model import Flow from langflow.services.database.models.flow.utils import get_outdated_components -class InvalidChatInputException(Exception): +class InvalidChatInputError(Exception): pass diff --git a/src/backend/base/langflow/exceptions/component.py b/src/backend/base/langflow/exceptions/component.py index a4a557565..65f8f80ff 100644 --- a/src/backend/base/langflow/exceptions/component.py +++ b/src/backend/base/langflow/exceptions/component.py @@ -1,5 +1,5 @@ # Create an exception class that receives the message and the formatted traceback -class ComponentBuildException(Exception): +class ComponentBuildError(Exception): def __init__(self, message: str, formatted_traceback: str): self.message = message self.formatted_traceback = formatted_traceback diff --git a/src/backend/base/langflow/graph/graph/ascii.py b/src/backend/base/langflow/graph/graph/ascii.py index feed7ccd3..2bee1f119 100644 --- a/src/backend/base/langflow/graph/graph/ascii.py +++ b/src/backend/base/langflow/graph/graph/ascii.py @@ -138,22 +138,22 @@ def draw_graph(vertexes, edges, *, return_ascii=True): """Build a DAG and draw it in ASCII.""" sug = build_sugiyama_layout(vertexes, edges) - Xs = [] - Ys = [] + xlist = [] + ylist = [] for vertex in sug.g.sV: - Xs.extend([vertex.view.xy[0] - vertex.view.w / 2.0, vertex.view.xy[0] + vertex.view.w / 2.0]) - Ys.extend([vertex.view.xy[1], vertex.view.xy[1] + vertex.view.h]) + xlist.extend([vertex.view.xy[0] - vertex.view.w / 2.0, vertex.view.xy[0] + vertex.view.w / 2.0]) + ylist.extend([vertex.view.xy[1], vertex.view.xy[1] + vertex.view.h]) for edge in sug.g.sE: for x, y in edge.view._pts: - Xs.append(x) - Ys.append(y) + xlist.append(x) + ylist.append(y) - minx = min(Xs) - miny = min(Ys) - maxx = max(Xs) - maxy = max(Ys) + minx = min(xlist) + miny = min(ylist) + maxx = max(xlist) + maxy = max(ylist) canvas_cols = int(math.ceil(maxx - minx)) + 1 canvas_lines = int(round(maxy - miny)) diff --git a/src/backend/base/langflow/graph/graph/base.py b/src/backend/base/langflow/graph/graph/base.py index 63dedbfb3..717b8b025 100644 --- a/src/backend/base/langflow/graph/graph/base.py +++ b/src/backend/base/langflow/graph/graph/base.py @@ -15,7 +15,7 @@ from typing import TYPE_CHECKING, Any, cast import nest_asyncio from loguru import logger -from langflow.exceptions.component import ComponentBuildException +from langflow.exceptions.component import ComponentBuildError from langflow.graph.edge.base import CycleEdge, Edge from langflow.graph.graph.constants import Finish, lazy_load_vertex_dict from langflow.graph.graph.runnable_vertices_manager import RunnableVerticesManager @@ -1394,7 +1394,7 @@ class Graph: await set_cache(key=vertex.id, data=vertex_dict) except Exception as exc: - if not isinstance(exc, ComponentBuildException): + if not isinstance(exc, ComponentBuildError): logger.exception("Error building Component") raise @@ -1699,13 +1699,13 @@ class Graph: return InterfaceVertex if node_name in {"SharedState", "Notify", "Listen"}: return StateVertex - if node_base_type in lazy_load_vertex_dict.VERTEX_TYPE_MAP: - return lazy_load_vertex_dict.VERTEX_TYPE_MAP[node_base_type] - if node_name in lazy_load_vertex_dict.VERTEX_TYPE_MAP: - return lazy_load_vertex_dict.VERTEX_TYPE_MAP[node_name] + if node_base_type in lazy_load_vertex_dict.vertex_type_map: + return lazy_load_vertex_dict.vertex_type_map[node_base_type] + if node_name in lazy_load_vertex_dict.vertex_type_map: + return lazy_load_vertex_dict.vertex_type_map[node_name] - if node_type in lazy_load_vertex_dict.VERTEX_TYPE_MAP: - return lazy_load_vertex_dict.VERTEX_TYPE_MAP[node_type] + if node_type in lazy_load_vertex_dict.vertex_type_map: + return lazy_load_vertex_dict.vertex_type_map[node_type] return Vertex def _build_vertices(self) -> list[Vertex]: @@ -1730,9 +1730,9 @@ class Graph: msg = f"Vertex data for {vertex_data['display_name']} does not contain an id" raise ValueError(msg) - VertexClass = self._get_vertex_class(vertex_type, vertex_base_type, vertex_data["id"]) + vertex_class = self._get_vertex_class(vertex_type, vertex_base_type, vertex_data["id"]) - vertex_instance = VertexClass(frontend_data, graph=self) + vertex_instance = vertex_class(frontend_data, graph=self) vertex_instance.set_top_level(self.top_level_vertices) return vertex_instance diff --git a/src/backend/base/langflow/graph/graph/constants.py b/src/backend/base/langflow/graph/graph/constants.py index 1ec2894f7..90395bdef 100644 --- a/src/backend/base/langflow/graph/graph/constants.py +++ b/src/backend/base/langflow/graph/graph/constants.py @@ -22,7 +22,7 @@ class VertexTypesDict(LazyLoadDictBase): self._types = _import_vertex_types @property - def VERTEX_TYPE_MAP(self): + def vertex_type_map(self): return self.all_types_dict def _build_dict(self): diff --git a/src/backend/base/langflow/graph/schema.py b/src/backend/base/langflow/graph/schema.py index 990b54e7b..dc643d647 100644 --- a/src/backend/base/langflow/graph/schema.py +++ b/src/backend/base/langflow/graph/schema.py @@ -48,8 +48,6 @@ class ResultData(BaseModel): class InterfaceComponentTypes(str, Enum, metaclass=ContainsEnumMeta): - # ChatInput and ChatOutput are the only ones that are - # power components ChatInput = "ChatInput" ChatOutput = "ChatOutput" TextInput = "TextInput" @@ -57,14 +55,6 @@ class InterfaceComponentTypes(str, Enum, metaclass=ContainsEnumMeta): DataOutput = "DataOutput" WebhookInput = "Webhook" - def __contains__(cls, item): - try: - cls(item) - except ValueError: - return False - else: - return True - CHAT_COMPONENTS = [InterfaceComponentTypes.ChatInput, InterfaceComponentTypes.ChatOutput] RECORDS_COMPONENTS = [InterfaceComponentTypes.DataOutput] diff --git a/src/backend/base/langflow/graph/vertex/base.py b/src/backend/base/langflow/graph/vertex/base.py index 75dee5752..be327c1fe 100644 --- a/src/backend/base/langflow/graph/vertex/base.py +++ b/src/backend/base/langflow/graph/vertex/base.py @@ -13,7 +13,7 @@ from typing import TYPE_CHECKING, Any import pandas as pd from loguru import logger -from langflow.exceptions.component import ComponentBuildException +from langflow.exceptions.component import ComponentBuildError from langflow.graph.schema import INPUT_COMPONENTS, OUTPUT_COMPONENTS, InterfaceComponentTypes, ResultData from langflow.graph.utils import UnbuiltObject, UnbuiltResult, log_transaction from langflow.interface import initialize @@ -256,7 +256,7 @@ class Vertex: ) if self.base_type is None: - for base_type, value in lazy_load_dict.ALL_TYPES_DICT.items(): + for base_type, value in lazy_load_dict.all_types_dict.items(): if self.vertex_type in value: self.base_type = base_type break @@ -720,7 +720,7 @@ class Vertex: tb = traceback.format_exc() logger.exception(exc) msg = f"Error building Component {self.display_name}: \n\n{exc}" - raise ComponentBuildException(msg, tb) from exc + raise ComponentBuildError(msg, tb) from exc def _update_built_object_and_artifacts(self, result: Any | tuple[Any, dict] | tuple[Component, Any, dict]): """Updates the built object and its artifacts.""" diff --git a/src/backend/base/langflow/graph/vertex/exceptions.py b/src/backend/base/langflow/graph/vertex/exceptions.py index 2d610f027..5b773bad1 100644 --- a/src/backend/base/langflow/graph/vertex/exceptions.py +++ b/src/backend/base/langflow/graph/vertex/exceptions.py @@ -1,4 +1,4 @@ -class NoComponentInstance(Exception): +class NoComponentInstanceError(Exception): def __init__(self, vertex_id: str): message = f"Vertex {vertex_id} does not have a component instance." super().__init__(message) diff --git a/src/backend/base/langflow/graph/vertex/types.py b/src/backend/base/langflow/graph/vertex/types.py index ebd505705..4decf39b3 100644 --- a/src/backend/base/langflow/graph/vertex/types.py +++ b/src/backend/base/langflow/graph/vertex/types.py @@ -12,7 +12,7 @@ from loguru import logger from langflow.graph.schema import CHAT_COMPONENTS, RECORDS_COMPONENTS, InterfaceComponentTypes, ResultData from langflow.graph.utils import UnbuiltObject, log_vertex_build, rewrite_file_path, serialize_field from langflow.graph.vertex.base import Vertex -from langflow.graph.vertex.exceptions import NoComponentInstance +from langflow.graph.vertex.exceptions import NoComponentInstanceError from langflow.schema import Data from langflow.schema.artifact import ArtifactType from langflow.schema.message import Message @@ -49,7 +49,7 @@ class ComponentVertex(Vertex): def get_output(self, name: str) -> Output: if self._custom_component is None: - raise NoComponentInstance(self.id) + raise NoComponentInstanceError(self.id) return self._custom_component.get_output(name) def _built_object_repr(self): @@ -130,7 +130,7 @@ class ComponentVertex(Vertex): result = self.results[edge.source_handle.name] else: result = cast(Any, output.value) - except NoComponentInstance: + except NoComponentInstanceError: result = self.results[edge.source_handle.name] break if result is UNDEFINED: diff --git a/src/backend/base/langflow/inputs/utils.py b/src/backend/base/langflow/inputs/utils.py index 50e8300b1..8bca38efe 100644 --- a/src/backend/base/langflow/inputs/utils.py +++ b/src/backend/base/langflow/inputs/utils.py @@ -10,7 +10,7 @@ else: _InputTypesMap: dict[str, type["InputTypes"]] | None = None -def get_InputTypesMap(): +def get_input_types_map(): global _InputTypesMap # noqa: PLW0603 if _InputTypesMap is None: from langflow.inputs.inputs import InputTypesMap @@ -20,9 +20,9 @@ def get_InputTypesMap(): def instantiate_input(input_type: str, data: dict) -> InputTypes: - InputTypesMap = get_InputTypesMap() + input_types_map = get_input_types_map() - input_type_class = InputTypesMap.get(input_type) + input_type_class = input_types_map.get(input_type) if "type" in data: # Replace with field_type data["field_type"] = data.pop("type") diff --git a/src/backend/base/langflow/interface/listing.py b/src/backend/base/langflow/interface/listing.py index a51e676db..2a950d4c0 100644 --- a/src/backend/base/langflow/interface/listing.py +++ b/src/backend/base/langflow/interface/listing.py @@ -6,10 +6,6 @@ class AllTypesDict(LazyLoadDictBase): def __init__(self): self._all_types_dict = None - @property - def ALL_TYPES_DICT(self): - return self.all_types_dict - def _build_dict(self): langchain_types_dict = self.get_type_dict() return { diff --git a/src/backend/base/langflow/schema/dotdict.py b/src/backend/base/langflow/schema/dotdict.py index 235fa9964..f7e30ee61 100644 --- a/src/backend/base/langflow/schema/dotdict.py +++ b/src/backend/base/langflow/schema/dotdict.py @@ -1,4 +1,4 @@ -class dotdict(dict): +class dotdict(dict): # noqa: N801 """dotdict allows accessing dictionary elements using dot notation (e.g., dict.key instead of dict['key']). It automatically converts nested dictionaries into dotdict instances, enabling dot notation on them as well. diff --git a/src/backend/base/langflow/schema/message.py b/src/backend/base/langflow/schema/message.py index 6d9e398ad..fd2a2ab7a 100644 --- a/src/backend/base/langflow/schema/message.py +++ b/src/backend/base/langflow/schema/message.py @@ -65,13 +65,13 @@ class Message(Data): return value @field_serializer("flow_id") - def serialize_flow_id(value): + def serialize_flow_id(self, value): if isinstance(value, UUID): return str(value) return value @field_serializer("timestamp") - def serialize_timestamp(value): + def serialize_timestamp(self, value): return datetime.strptime(value, "%Y-%m-%d %H:%M:%S").astimezone(timezone.utc) @field_validator("files", mode="before") diff --git a/src/backend/base/langflow/services/auth/utils.py b/src/backend/base/langflow/services/auth/utils.py index 956e1c8e7..98f3e3efe 100644 --- a/src/backend/base/langflow/services/auth/utils.py +++ b/src/backend/base/langflow/services/auth/utils.py @@ -360,8 +360,8 @@ def ensure_valid_key(s: str) -> bytes: def get_fernet(settings_service=Depends(get_settings_service)): - SECRET_KEY: str = settings_service.auth_settings.SECRET_KEY.get_secret_value() - valid_key = ensure_valid_key(SECRET_KEY) + secret_key: str = settings_service.auth_settings.SECRET_KEY.get_secret_value() + valid_key = ensure_valid_key(secret_key) return Fernet(valid_key) diff --git a/src/backend/base/langflow/services/database/models/flow/model.py b/src/backend/base/langflow/services/database/models/flow/model.py index 643cde0cb..793db9264 100644 --- a/src/backend/base/langflow/services/database/models/flow/model.py +++ b/src/backend/base/langflow/services/database/models/flow/model.py @@ -56,6 +56,7 @@ class FlowBase(SQLModel): return v @field_validator("icon_bg_color") + @classmethod def validate_icon_bg_color(cls, v): if v is not None and not isinstance(v, str): msg = "Icon background color must be a string" @@ -72,6 +73,7 @@ class FlowBase(SQLModel): return v @field_validator("icon") + @classmethod def validate_icon_atr(cls, v): # const emojiRegex = /\p{Emoji}/u; # const isEmoji = emojiRegex.test(data?.node?.icon!); @@ -111,7 +113,8 @@ class FlowBase(SQLModel): return v @field_validator("data") - def validate_json(v): + @classmethod + def validate_json(cls, v): if not v: return v if not isinstance(v, dict): @@ -130,7 +133,7 @@ class FlowBase(SQLModel): # updated_at can be serialized to JSON @field_serializer("updated_at") - def serialize_datetime(value): + def serialize_datetime(self, value): if isinstance(value, datetime): # I'm getting 2024-05-29T17:57:17.631346 # and I want 2024-05-29T17:57:17-05:00 @@ -141,6 +144,7 @@ class FlowBase(SQLModel): return value @field_validator("updated_at", mode="before") + @classmethod def validate_dt(cls, v): if v is None: return v diff --git a/src/backend/base/langflow/services/settings/base.py b/src/backend/base/langflow/services/settings/base.py index 2229fe809..6ef4ae425 100644 --- a/src/backend/base/langflow/services/settings/base.py +++ b/src/backend/base/langflow/services/settings/base.py @@ -292,6 +292,7 @@ class Settings(BaseSettings): return value @field_validator("components_path", mode="before") + @classmethod def set_components_path(cls, value): if os.getenv("LANGFLOW_COMPONENTS_PATH"): logger.debug("Adding LANGFLOW_COMPONENTS_PATH to components_path") diff --git a/src/backend/base/langflow/services/store/exceptions.py b/src/backend/base/langflow/services/store/exceptions.py index 5cd36de39..60ee08b49 100644 --- a/src/backend/base/langflow/services/store/exceptions.py +++ b/src/backend/base/langflow/services/store/exceptions.py @@ -1,25 +1,25 @@ -class CustomException(Exception): +class CustomError(Exception): def __init__(self, detail, status_code): super().__init__(detail) self.status_code = status_code # Define custom exceptions with status codes -class UnauthorizedError(CustomException): +class UnauthorizedError(CustomError): def __init__(self, detail="Unauthorized access"): super().__init__(detail, 401) -class ForbiddenError(CustomException): +class ForbiddenError(CustomError): def __init__(self, detail="Forbidden"): super().__init__(detail, 403) -class APIKeyError(CustomException): +class APIKeyError(CustomError): def __init__(self, detail="API key error"): super().__init__(detail, 400) # ! Should be 401 -class FilterError(CustomException): +class FilterError(CustomError): def __init__(self, detail="Filter error"): super().__init__(detail, 400) diff --git a/src/backend/base/langflow/services/task/service.py b/src/backend/base/langflow/services/task/service.py index a829cc6da..b113cdc5d 100644 --- a/src/backend/base/langflow/services/task/service.py +++ b/src/backend/base/langflow/services/task/service.py @@ -33,16 +33,15 @@ class TaskService(Service): self.settings_service = settings_service try: if self.settings_service.settings.celery_enabled: - USE_CELERY = True status = check_celery_availability() - USE_CELERY = status.get("availability") is not None + use_celery = status.get("availability") is not None else: - USE_CELERY = False + use_celery = False except ImportError: - USE_CELERY = False + use_celery = False - self.use_celery = USE_CELERY + self.use_celery = use_celery self.backend = self.get_backend() @property diff --git a/src/backend/base/langflow/services/telemetry/schema.py b/src/backend/base/langflow/services/telemetry/schema.py index a78629d5c..15b753db9 100644 --- a/src/backend/base/langflow/services/telemetry/schema.py +++ b/src/backend/base/langflow/services/telemetry/schema.py @@ -1,15 +1,15 @@ -from pydantic import BaseModel +from pydantic import BaseModel, Field class RunPayload(BaseModel): - runIsWebhook: bool = False - runSeconds: int - runSuccess: bool - runErrorMessage: str = "" + run_is_webhook: bool = Field(default=False, serialization_alias="runIsWebhook") + run_seconds: int = Field(serialization_alias="runSeconds") + run_success: bool = Field(serialization_alias="runSuccess") + run_error_message: str = Field("", serialization_alias="runErrorMessage") class ShutdownPayload(BaseModel): - timeRunning: int + time_running: int = Field(serialization_alias="timeRunning") class VersionPayload(BaseModel): @@ -18,20 +18,20 @@ class VersionPayload(BaseModel): platform: str python: str arch: str - autoLogin: bool - cacheType: str - backendOnly: bool + auto_login: bool = Field(serialization_alias="autoLogin") + cache_type: str = Field(serialization_alias="cacheType") + backend_only: bool = Field(serialization_alias="backendOnly") class PlaygroundPayload(BaseModel): - playgroundSeconds: int - playgroundComponentCount: int | None = None - playgroundSuccess: bool - playgroundErrorMessage: str = "" + playground_seconds: int = Field(serialization_alias="playgroundSeconds") + playground_component_count: int | None = Field(None, serialization_alias="playgroundComponentCount") + playground_success: bool = Field(serialization_alias="playgroundSuccess") + playground_error_message: str = Field("", serialization_alias="playgroundErrorMessage") class ComponentPayload(BaseModel): - componentName: str - componentSeconds: int - componentSuccess: bool - componentErrorMessage: str | None = None + component_name: str = Field(serialization_alias="componentName") + component_seconds: int = Field(serialization_alias="componentSeconds") + component_success: bool = Field(serialization_alias="componentSuccess") + component_error_message: str | None = Field(serialization_alias="componentErrorMessage") diff --git a/src/backend/base/langflow/services/telemetry/service.py b/src/backend/base/langflow/services/telemetry/service.py index 6fe7a5d75..65fc851f4 100644 --- a/src/backend/base/langflow/services/telemetry/service.py +++ b/src/backend/base/langflow/services/telemetry/service.py @@ -65,7 +65,7 @@ class TelemetryService(Service): if path: url = f"{url}/{path}" try: - payload_dict = payload.model_dump(exclude_none=True, exclude_unset=True) + payload_dict = payload.model_dump(by_alias=True, exclude_none=True, exclude_unset=True) response = await self.client.get(url, params=payload_dict) if response.status_code != httpx.codes.OK: logger.error(f"Failed to send telemetry data: {response.status_code} {response.text}") @@ -82,7 +82,7 @@ class TelemetryService(Service): await self._queue_event((self.send_telemetry_data, payload, "run")) async def log_package_shutdown(self): - payload = ShutdownPayload(timeRunning=(datetime.now(timezone.utc) - self._start_time).seconds) + payload = ShutdownPayload(time_running=(datetime.now(timezone.utc) - self._start_time).seconds) await self._queue_event(payload) async def _queue_event(self, payload): @@ -99,10 +99,10 @@ class TelemetryService(Service): version=version_info["version"], platform=platform.platform(), python=python_version, - cacheType=self.settings_service.settings.cache_type, - backendOnly=self.settings_service.settings.backend_only, + cache_type=self.settings_service.settings.cache_type, + backend_only=self.settings_service.settings.backend_only, arch=architecture, - autoLogin=self.settings_service.auth_settings.AUTO_LOGIN, + auto_login=self.settings_service.auth_settings.AUTO_LOGIN, ) await self._queue_event((self.send_telemetry_data, payload, None)) diff --git a/src/backend/base/langflow/template/field/base.py b/src/backend/base/langflow/template/field/base.py index c676c3fdd..6bc2036a5 100644 --- a/src/backend/base/langflow/template/field/base.py +++ b/src/backend/base/langflow/template/field/base.py @@ -137,6 +137,7 @@ class Input(BaseModel): return value @field_validator("file_types") + @classmethod def validate_file_types(cls, value): if not isinstance(value, list): msg = "file_types must be a list" diff --git a/src/backend/base/langflow/utils/schemas.py b/src/backend/base/langflow/utils/schemas.py index 58f9f0074..7b9c21e1c 100644 --- a/src/backend/base/langflow/utils/schemas.py +++ b/src/backend/base/langflow/utils/schemas.py @@ -29,6 +29,7 @@ class ChatOutputResponse(BaseModel): type: str @field_validator("files", mode="before") + @classmethod def validate_files(cls, files): """Validate files.""" if not files: diff --git a/src/backend/base/pyproject.toml b/src/backend/base/pyproject.toml index fd56ce526..b2dc802ec 100644 --- a/src/backend/base/pyproject.toml +++ b/src/backend/base/pyproject.toml @@ -56,9 +56,9 @@ ignore = [ "TRY301", # A bit too harsh (Abstract `raise` to an inner function) # Rules that are TODOs - "ANN", + "ANN", # Missing type annotations "D1", # Missing docstrings - "N", + "N999", # Invalid module names "S", "SLF", ]