Skip to content

Commit

Permalink
Add ruff rules for TYPE_CHECKING (TCH)
Browse files Browse the repository at this point in the history
  • Loading branch information
cbornet committed Oct 3, 2024
1 parent a3a950c commit 7b6d66c
Show file tree
Hide file tree
Showing 51 changed files with 183 additions and 135 deletions.
8 changes: 4 additions & 4 deletions src/backend/base/langflow/api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@
from fastapi import HTTPException
from loguru import logger
from sqlalchemy import delete
from sqlmodel import Session

from langflow.graph.graph.base import Graph
from langflow.services.chat.service import ChatService
from langflow.services.database.models.flow import Flow
from langflow.services.database.models.transactions.model import TransactionTable
from langflow.services.database.models.vertex_builds.model import VertexBuildTable
from langflow.services.store.schema import StoreComponentCreate
from langflow.services.store.utils import get_lf_version_from_pypi

if TYPE_CHECKING:
from langflow.services.database.models.flow.model import Flow
from sqlmodel import Session

from langflow.services.chat.service import ChatService
from langflow.services.store.schema import StoreComponentCreate


API_WORDS = ["api", "key", "token"]
Expand Down
4 changes: 0 additions & 4 deletions src/backend/base/langflow/api/v1/api_key.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from typing import TYPE_CHECKING
from uuid import UUID

from fastapi import APIRouter, Depends, HTTPException, Response
Expand All @@ -13,9 +12,6 @@
from langflow.services.database.models.user.model import User
from langflow.services.deps import get_session, get_settings_service

if TYPE_CHECKING:
pass

router = APIRouter(tags=["APIKey"], prefix="/api_key")


Expand Down
6 changes: 4 additions & 2 deletions src/backend/base/langflow/api/v1/callback.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
from __future__ import annotations

from typing import TYPE_CHECKING, Any
from uuid import UUID

from langchain_core.agents import AgentAction, AgentFinish
from langchain_core.callbacks.base import AsyncCallbackHandler
from loguru import logger

Expand All @@ -12,6 +10,10 @@
from langflow.utils.util import remove_ansi_escape_codes

if TYPE_CHECKING:
from uuid import UUID

from langchain_core.agents import AgentAction, AgentFinish

from langflow.services.socket.service import SocketIOService


Expand Down
16 changes: 9 additions & 7 deletions src/backend/base/langflow/api/v1/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,11 @@
import time
import traceback
import typing
import uuid
from typing import TYPE_CHECKING, Annotated

from fastapi import APIRouter, BackgroundTasks, Body, Depends, HTTPException
from fastapi.responses import StreamingResponse
from loguru import logger
from starlette.background import BackgroundTask
from starlette.responses import ContentStream
from starlette.types import Receive

from langflow.api.utils import (
build_and_cache_graph_from_data,
Expand All @@ -35,18 +31,24 @@
)
from langflow.events.event_manager import EventManager, create_default_event_manager
from langflow.exceptions.component import ComponentBuildException
from langflow.graph.graph.base import Graph
from langflow.graph.utils import log_vertex_build
from langflow.schema.schema import OutputValue
from langflow.services.auth.utils import get_current_active_user
from langflow.services.chat.service import ChatService
from langflow.services.deps import get_chat_service, get_session, get_session_service, get_telemetry_service
from langflow.services.telemetry.schema import ComponentPayload, PlaygroundPayload
from langflow.services.telemetry.service import TelemetryService

if TYPE_CHECKING:
import uuid

from starlette.background import BackgroundTask
from starlette.responses import ContentStream
from starlette.types import Receive

from langflow.graph.graph.base import Graph
from langflow.graph.vertex.types import InterfaceVertex
from langflow.services.chat.service import ChatService
from langflow.services.session.service import SessionService
from langflow.services.telemetry.service import TelemetryService

router = APIRouter(tags=["Chat"])

Expand Down
21 changes: 11 additions & 10 deletions src/backend/base/langflow/api/v1/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from asyncio import Lock
from http import HTTPStatus
from typing import TYPE_CHECKING, Annotated
from uuid import UUID

import sqlalchemy as sa
from fastapi import APIRouter, BackgroundTasks, Body, Depends, HTTPException, Request, UploadFile, status
Expand All @@ -28,18 +27,12 @@
from langflow.custom.utils import build_custom_component_template, get_instance_name
from langflow.exceptions.api import APIException, InvalidChatInputException
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
from langflow.helpers.user import get_user_by_flow_id_or_endpoint_name
from langflow.interface.initialize.loading import update_params_with_load_from_db_fields
from langflow.processing.process import process_tweaks, run_graph_internal
from langflow.schema.graph import Tweaks
from langflow.services.auth.utils import api_key_security, get_current_active_user
from langflow.services.cache.utils import save_uploaded_file
from langflow.services.database.models.flow import Flow
from langflow.services.database.models.flow.model import FlowRead
from langflow.services.database.models.flow.utils import get_all_webhook_components_in_flow
from langflow.services.database.models.user.model import User, UserRead
from langflow.services.deps import (
get_cache_service,
get_session,
Expand All @@ -48,15 +41,23 @@
get_task_service,
get_telemetry_service,
)
from langflow.services.session.service import SessionService
from langflow.services.task.service import TaskService
from langflow.services.telemetry.schema import RunPayload
from langflow.services.telemetry.service import TelemetryService
from langflow.utils.version import get_version_info

if TYPE_CHECKING:
from uuid import UUID

from langflow.graph.schema import RunOutputs
from langflow.helpers.flow import get_flow_by_id_or_endpoint_name
from langflow.helpers.user import get_user_by_flow_id_or_endpoint_name
from langflow.schema.graph import Tweaks
from langflow.services.cache.base import CacheService
from langflow.services.database.models.flow.model import FlowRead
from langflow.services.database.models.user.model import User, UserRead
from langflow.services.session.service import SessionService
from langflow.services.settings.service import SettingsService
from langflow.services.task.service import TaskService
from langflow.services.telemetry.service import TelemetryService

router = APIRouter(tags=["Base"])

Expand Down
10 changes: 7 additions & 3 deletions src/backend/base/langflow/api/v1/flows.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import re
import zipfile
from datetime import datetime, timezone
from uuid import UUID
from typing import TYPE_CHECKING

import orjson
from fastapi import APIRouter, Depends, File, HTTPException, UploadFile
Expand All @@ -23,10 +23,14 @@
from langflow.services.database.models.folder.constants import DEFAULT_FOLDER_NAME
from langflow.services.database.models.folder.model import Folder
from langflow.services.database.models.transactions.crud import get_transactions_by_flow_id
from langflow.services.database.models.user.model import User
from langflow.services.database.models.vertex_builds.crud import get_vertex_builds_by_flow_id
from langflow.services.deps import get_session, get_settings_service
from langflow.services.settings.service import SettingsService

if TYPE_CHECKING:
from uuid import UUID

from langflow.services.database.models.user.model import User
from langflow.services.settings.service import SettingsService

# build router
router = APIRouter(prefix="/flows", tags=["Flows"])
Expand Down
13 changes: 9 additions & 4 deletions src/backend/base/langflow/api/v1/login.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from __future__ import annotations

from typing import TYPE_CHECKING

from fastapi import APIRouter, Depends, HTTPException, Request, Response, status
from fastapi.security import OAuth2PasswordRequestForm
from sqlmodel import Session

from langflow.api.v1.schemas import Token
from langflow.services.auth.utils import (
Expand All @@ -14,8 +14,13 @@
from langflow.services.database.models.folder.utils import create_default_folder_if_it_doesnt_exist
from langflow.services.database.models.user.crud import get_user_by_id
from langflow.services.deps import get_session, get_settings_service, get_variable_service
from langflow.services.settings.service import SettingsService
from langflow.services.variable.service import VariableService

if TYPE_CHECKING:
from fastapi.security import OAuth2PasswordRequestForm
from sqlmodel import Session

from langflow.services.settings.service import SettingsService
from langflow.services.variable.service import VariableService

router = APIRouter(tags=["Login"])

Expand Down
5 changes: 4 additions & 1 deletion src/backend/base/langflow/api/v1/users.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from typing import TYPE_CHECKING
from uuid import UUID

from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy import func
from sqlalchemy.exc import IntegrityError
from sqlmodel import Session, select
from sqlmodel.sql.expression import SelectOfScalar

from langflow.api.v1.schemas import UsersResponse
from langflow.services.auth.utils import (
Expand All @@ -18,6 +18,9 @@
from langflow.services.database.models.user.crud import get_user_by_id, update_user
from langflow.services.deps import get_session, get_settings_service

if TYPE_CHECKING:
from sqlmodel.sql.expression import SelectOfScalar

router = APIRouter(tags=["Users"], prefix="/users")


Expand Down
6 changes: 4 additions & 2 deletions src/backend/base/langflow/base/agents/agent.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from abc import abstractmethod
from typing import cast
from typing import TYPE_CHECKING, cast

from langchain.agents import AgentExecutor, BaseMultiActionAgent, BaseSingleActionAgent
from langchain.agents.agent import RunnableAgent
from langchain_core.messages import BaseMessage
from langchain_core.runnables import Runnable

from langflow.base.agents.callback import AgentAsyncHandler
Expand All @@ -17,6 +16,9 @@
from langflow.template import Output
from langflow.utils.constants import MESSAGE_SENDER_AI

if TYPE_CHECKING:
from langchain_core.messages import BaseMessage


class LCAgentComponent(Component):
trace_type = "agent"
Expand Down
6 changes: 4 additions & 2 deletions src/backend/base/langflow/base/tools/component_tool.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
from __future__ import annotations

import re
from collections.abc import Callable
from typing import TYPE_CHECKING

from langchain_core.tools import BaseTool
from langchain_core.tools.structured import StructuredTool
from loguru import logger

from langflow.base.tools.constants import TOOL_OUTPUT_NAME
from langflow.io.schema import create_input_schema

if TYPE_CHECKING:
from collections.abc import Callable

from langchain_core.tools import BaseTool

from langflow.custom.custom_component.component import Component
from langflow.inputs.inputs import InputTypes
from langflow.io import Output
Expand Down
13 changes: 8 additions & 5 deletions src/backend/base/langflow/base/tools/flow_tool.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
from __future__ import annotations

from typing import Any
from typing import TYPE_CHECKING, Any

from langchain_core.runnables import RunnableConfig
from langchain_core.tools import BaseTool, ToolException
from loguru import logger
from pydantic.v1 import BaseModel

from langflow.base.flow_processing.utils import build_data_from_result_data, format_flow_output_data
from langflow.graph.graph.base import Graph
from langflow.graph.vertex.base import Vertex
from langflow.helpers.flow import build_schema_from_inputs, get_arg_names, get_flow_inputs, run_flow
from langflow.utils.async_helpers import run_until_complete

if TYPE_CHECKING:
from langchain_core.runnables import RunnableConfig
from pydantic.v1 import BaseModel

from langflow.graph.graph.base import Graph
from langflow.graph.vertex.base import Vertex


class FlowTool(BaseTool):
name: str
Expand Down
6 changes: 4 additions & 2 deletions src/backend/base/langflow/base/vectorstores/model.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from abc import abstractmethod
from functools import wraps
from typing import cast
from typing import TYPE_CHECKING, cast

from langchain_core.documents import Document
from loguru import logger

from langflow.custom import Component
Expand All @@ -11,6 +10,9 @@
from langflow.io import Output
from langflow.schema import Data

if TYPE_CHECKING:
from langchain_core.documents import Document


def check_cached_vector_store(f):
"""
Expand Down
6 changes: 5 additions & 1 deletion src/backend/base/langflow/components/chains/SQLGenerator.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
from typing import TYPE_CHECKING

from langchain.chains import create_sql_query_chain
from langchain_core.prompts import PromptTemplate
from langchain_core.runnables import Runnable

from langflow.base.chains.model import LCChainComponent
from langflow.field_typing import Message
from langflow.inputs import HandleInput, IntInput, MultilineInput
from langflow.template import Output

if TYPE_CHECKING:
from langchain_core.runnables import Runnable


class SQLGeneratorComponent(LCChainComponent):
display_name = "Natural Language to SQL"
Expand Down
6 changes: 4 additions & 2 deletions src/backend/base/langflow/components/deactivated/SubFlow.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
from typing import Any
from typing import TYPE_CHECKING, Any

from loguru import logger

from langflow.base.flow_processing.utils import build_data_from_result_data
from langflow.custom import CustomComponent
from langflow.graph.graph.base import Graph
from langflow.graph.schema import RunOutputs
from langflow.graph.vertex.base import Vertex
from langflow.helpers.flow import get_flow_inputs
from langflow.schema import Data
from langflow.schema.dotdict import dotdict
from langflow.template.field.base import Input

if TYPE_CHECKING:
from langflow.graph.schema import RunOutputs


class SubFlowComponent(CustomComponent):
display_name = "Sub Flow"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
from typing import TYPE_CHECKING

from langflow.custom import Component
from langflow.field_typing import Embeddings
from langflow.io import HandleInput, MessageInput, Output
from langflow.schema import Data
from langflow.schema.message import Message

if TYPE_CHECKING:
from langflow.field_typing import Embeddings
from langflow.schema.message import Message


class TextEmbedderComponent(Component):
Expand Down
6 changes: 4 additions & 2 deletions src/backend/base/langflow/components/prototypes/RunFlow.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from typing import Any
from typing import TYPE_CHECKING, Any

from langflow.base.flow_processing.utils import build_data_from_run_outputs
from langflow.custom import Component
from langflow.graph.schema import RunOutputs
from langflow.io import DropdownInput, MessageTextInput, NestedDictInput, Output
from langflow.schema import Data, dotdict

if TYPE_CHECKING:
from langflow.graph.schema import RunOutputs


class RunFlowComponent(Component):
display_name = "Run Flow"
Expand Down
Loading

0 comments on commit 7b6d66c

Please sign in to comment.