Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ref: Add ruff rules for pygrep-hooks (PGH) #4042

Merged
merged 1 commit into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/backend/base/langflow/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
import httpx
import typer
from dotenv import load_dotenv
from multiprocess import cpu_count # type: ignore
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are a lot of # type: ignore on 3rd party dependencies.
I guess those are optional ?
But shouldn't mypy be run with uv's dev deps that contain those dependencies ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This case is because multiprocess is kinda weird. I'm not sure about the other ones but they generally are related to typing variables and outputs inside components. Not a lot of people know about cast and how to type correctly.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you get these mypy errors when you don't have the dependency.
In CI dev dependencies are installed and mypy still passes when removing the ignore.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

multiprocess is a base dependency in langflow

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then. I don't know why this ignore was there. Maybe some legacy ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Process is not explicitly exported in multiprocess.init

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't raise an error in mypy for me. Does it for you ?
Maybe we can check if CI passes ?

from multiprocess.context import Process # type: ignore
from multiprocess import cpu_count
from multiprocess.context import Process
from packaging import version as pkg_version
from rich import box
from rich import print as rprint
Expand Down Expand Up @@ -530,7 +530,7 @@ def api_key(

def api_key_banner(unmasked_api_key):
is_mac = platform.system() == "Darwin"
import pyperclip # type: ignore
import pyperclip

pyperclip.copy(unmasked_api_key.api_key)
panel = Panel(
Expand Down
2 changes: 1 addition & 1 deletion src/backend/base/langflow/alembic/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from loguru import logger
from sqlalchemy import engine_from_config, pool

from langflow.services.database.models import * # noqa
from langflow.services.database.models import *
from langflow.services.database.service import SQLModel

# this is the Alembic Config object, which provides
Expand Down
6 changes: 3 additions & 3 deletions src/backend/base/langflow/api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,9 +259,9 @@ def parse_value(value: Any, input_type: str) -> Any:

async def cascade_delete_flow(session: Session, flow: Flow):
try:
session.exec(delete(TransactionTable).where(TransactionTable.flow_id == flow.id)) # type: ignore
session.exec(delete(VertexBuildTable).where(VertexBuildTable.flow_id == flow.id)) # type: ignore
session.exec(delete(Flow).where(Flow.id == flow.id)) # type: ignore
session.exec(delete(TransactionTable).where(TransactionTable.flow_id == flow.id))
session.exec(delete(VertexBuildTable).where(VertexBuildTable.flow_id == flow.id))
session.exec(delete(Flow).where(Flow.id == flow.id))
except Exception as e:
msg = f"Unable to cascade delete flow: ${flow.id}"
raise RuntimeError(msg, e) from e
2 changes: 1 addition & 1 deletion src/backend/base/langflow/api/v1/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Code(BaseModel):


class FrontendNodeRequest(FrontendNode):
template: dict # type: ignore
template: dict # type: ignore[assignment]

@model_serializer(mode="wrap")
def serialize_model(self, handler):
Expand Down
6 changes: 3 additions & 3 deletions src/backend/base/langflow/api/v1/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ async def simple_run_flow(
if input_request.output_type == "debug"
or (
vertex.is_output
and (input_request.output_type == "any" or input_request.output_type in vertex.id.lower()) # type: ignore
and (input_request.output_type == "any" or input_request.output_type in vertex.id.lower()) # type: ignore[operator]
)
]
task_result, session_id = await run_graph_internal(
Expand Down Expand Up @@ -346,7 +346,7 @@ async def webhook_run_flow(
)

logger.debug("Starting background task")
background_tasks.add_task( # type: ignore
background_tasks.add_task(
simple_run_flow_task,
flow=flow,
input_request=input_request,
Expand Down Expand Up @@ -659,7 +659,7 @@ def get_config():
try:
from langflow.services.deps import get_settings_service

settings_service: SettingsService = get_settings_service() # type: ignore
settings_service: SettingsService = get_settings_service()
return settings_service.settings.model_dump()
except Exception as exc:
logger.exception(exc)
Expand Down
10 changes: 5 additions & 5 deletions src/backend/base/langflow/api/v1/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,10 @@ async def download_profile_picture(
try:
extension = file_name.split(".")[-1]
config_dir = get_storage_service().settings_service.settings.config_dir
config_path = Path(config_dir) # type: ignore
config_path = Path(config_dir) # type: ignore[arg-type]
folder_path = config_path / "profile_pictures" / folder_name
content_type = build_content_type_from_extension(extension)
file_content = await storage_service.get_file(flow_id=folder_path, file_name=file_name) # type: ignore
file_content = await storage_service.get_file(flow_id=folder_path, file_name=file_name) # type: ignore[arg-type]
return StreamingResponse(BytesIO(file_content), media_type=content_type)

except Exception as e:
Expand All @@ -142,13 +142,13 @@ async def download_profile_picture(
async def list_profile_pictures(storage_service: Annotated[StorageService, Depends(get_storage_service)]):
try:
config_dir = get_storage_service().settings_service.settings.config_dir
config_path = Path(config_dir) # type: ignore
config_path = Path(config_dir) # type: ignore[arg-type]

people_path = config_path / "profile_pictures/People"
space_path = config_path / "profile_pictures/Space"

people = await storage_service.list_files(flow_id=people_path) # type: ignore
space = await storage_service.list_files(flow_id=space_path) # type: ignore
people = await storage_service.list_files(flow_id=people_path) # type: ignore[arg-type]
space = await storage_service.list_files(flow_id=space_path) # type: ignore[arg-type]

files = [f"People/{i}" for i in people]
files += [f"Space/{i}" for i in space]
Expand Down
18 changes: 9 additions & 9 deletions src/backend/base/langflow/api/v1/flows.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def create_flow(
# based on the highest number found
if session.exec(select(Flow).where(Flow.name == flow.name).where(Flow.user_id == current_user.id)).first():
flows = session.exec(
select(Flow).where(Flow.name.like(f"{flow.name} (%")).where(Flow.user_id == current_user.id) # type: ignore
select(Flow).where(Flow.name.like(f"{flow.name} (%")).where(Flow.user_id == current_user.id) # type: ignore[attr-defined]
).all()
if flows:
extract_number = re.compile(r"\((\d+)\)$")
Expand All @@ -74,14 +74,14 @@ def create_flow(
):
flows = session.exec(
select(Flow)
.where(Flow.endpoint_name.like(f"{flow.endpoint_name}-%")) # type: ignore
.where(Flow.endpoint_name.like(f"{flow.endpoint_name}-%")) # type: ignore[union-attr]
.where(Flow.user_id == current_user.id)
).all()
if flows:
# The endpoitn name is like "my-endpoint","my-endpoint-1", "my-endpoint-2"
# so we need to get the highest number and add 1
# we need to get the last part of the endpoint name
numbers = [int(flow.endpoint_name.split("-")[-1]) for flow in flows] # type: ignore
numbers = [int(flow.endpoint_name.split("-")[-1]) for flow in flows]
flow.endpoint_name = f"{flow.endpoint_name}-{max(numbers) + 1}"
else:
flow.endpoint_name = f"{flow.endpoint_name}-1"
Expand Down Expand Up @@ -148,16 +148,16 @@ def read_flows(
auth_settings = settings_service.auth_settings
if auth_settings.AUTO_LOGIN:
stmt = select(Flow).where(
(Flow.user_id == None) | (Flow.user_id == current_user.id) # noqa
(Flow.user_id == None) | (Flow.user_id == current_user.id) # noqa: E711
)
if components_only:
stmt = stmt.where(Flow.is_component == True) # noqa
stmt = stmt.where(Flow.is_component == True) # noqa: E712
flows = session.exec(stmt).all()

else:
flows = current_user.flows

flows = validate_is_component(flows) # type: ignore
flows = validate_is_component(flows)
if components_only:
flows = [flow for flow in flows if flow.is_component]
flow_ids = [flow.id for flow in flows]
Expand All @@ -169,7 +169,7 @@ def read_flows(
example_flows = folder.flows if folder else []
for example_flow in example_flows:
if example_flow.id not in flow_ids:
flows.append(example_flow) # type: ignore
flows.append(example_flow)
except Exception as e:
logger.exception(e)

Expand All @@ -196,7 +196,7 @@ def read_flow(
# If auto login is enable user_id can be current_user.id or None
# so write an OR
stmt = stmt.where(
(Flow.user_id == current_user.id) | (Flow.user_id == None) # noqa
(Flow.user_id == current_user.id) | (Flow.user_id == None) # noqa: E711
)
if user_flow := session.exec(stmt).first():
return user_flow
Expand Down Expand Up @@ -369,7 +369,7 @@ async def download_multiple_file(
db: Annotated[Session, Depends(get_session)],
):
"""Download all flows as a zip file."""
flows = db.exec(select(Flow).where(and_(Flow.user_id == user.id, Flow.id.in_(flow_ids)))).all() # type: ignore
flows = db.exec(select(Flow).where(and_(Flow.user_id == user.id, Flow.id.in_(flow_ids)))).all() # type: ignore[attr-defined]

if not flows:
raise HTTPException(status_code=404, detail="No flows found.")
Expand Down
20 changes: 10 additions & 10 deletions src/backend/base/langflow/api/v1/folders.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def create_folder(
).first():
folder_results = session.exec(
select(Folder).where(
Folder.name.like(f"{new_folder.name}%"), # type: ignore
Folder.name.like(f"{new_folder.name}%"), # type: ignore[attr-defined]
Folder.user_id == current_user.id,
)
)
Expand All @@ -62,14 +62,14 @@ def create_folder(

if folder.components_list:
update_statement_components = (
update(Flow).where(Flow.id.in_(folder.components_list)).values(folder_id=new_folder.id) # type: ignore
update(Flow).where(Flow.id.in_(folder.components_list)).values(folder_id=new_folder.id) # type: ignore[attr-defined]
)
session.exec(update_statement_components) # type: ignore
session.exec(update_statement_components)
session.commit()

if folder.flows_list:
update_statement_flows = update(Flow).where(Flow.id.in_(folder.flows_list)).values(folder_id=new_folder.id) # type: ignore
session.exec(update_statement_flows) # type: ignore
update_statement_flows = update(Flow).where(Flow.id.in_(folder.flows_list)).values(folder_id=new_folder.id) # type: ignore[attr-defined]
session.exec(update_statement_flows)
session.commit()

return new_folder
Expand All @@ -86,7 +86,7 @@ def read_folders(
try:
folders = session.exec(
select(Folder).where(
or_(Folder.user_id == current_user.id, Folder.user_id == None) # type: ignore # noqa: E711
or_(Folder.user_id == current_user.id, Folder.user_id == None) # noqa: E711
)
).all()
return sorted(folders, key=lambda x: x.name != DEFAULT_FOLDER_NAME)
Expand Down Expand Up @@ -152,16 +152,16 @@ def update_folder(
my_collection_folder = session.exec(select(Folder).where(Folder.name == DEFAULT_FOLDER_NAME)).first()
if my_collection_folder:
update_statement_my_collection = (
update(Flow).where(Flow.id.in_(excluded_flows)).values(folder_id=my_collection_folder.id) # type: ignore
update(Flow).where(Flow.id.in_(excluded_flows)).values(folder_id=my_collection_folder.id) # type: ignore[attr-defined]
)
session.exec(update_statement_my_collection) # type: ignore
session.exec(update_statement_my_collection)
session.commit()

if concat_folder_components:
update_statement_components = (
update(Flow).where(Flow.id.in_(concat_folder_components)).values(folder_id=existing_folder.id) # type: ignore
update(Flow).where(Flow.id.in_(concat_folder_components)).values(folder_id=existing_folder.id) # type: ignore[attr-defined]
)
session.exec(update_statement_components) # type: ignore
session.exec(update_statement_components)
session.commit()

return existing_folder
Expand Down
4 changes: 2 additions & 2 deletions src/backend/base/langflow/api/v1/monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ async def delete_messages(
current_user: Annotated[User, Depends(get_current_active_user)],
):
try:
session.exec(delete(MessageTable).where(MessageTable.id.in_(message_ids))) # type: ignore
session.exec(delete(MessageTable).where(MessageTable.id.in_(message_ids))) # type: ignore[attr-defined]
session.commit()
except Exception as e:
raise HTTPException(status_code=500, detail=str(e)) from e
Expand Down Expand Up @@ -147,7 +147,7 @@ async def delete_messages_session(
session: Annotated[Session, Depends(get_session)],
):
try:
session.exec( # type: ignore
session.exec(
delete(MessageTable)
.where(col(MessageTable.session_id) == session_id)
.execution_options(synchronize_session="fetch")
Expand Down
4 changes: 2 additions & 2 deletions src/backend/base/langflow/api/v1/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ def read_all_users(
query: SelectOfScalar = select(User).offset(skip).limit(limit)
users = session.exec(query).fetchall()

count_query = select(func.count()).select_from(User) # type: ignore
count_query = select(func.count()).select_from(User)
total_count = session.exec(count_query).first()

return UsersResponse(
total_count=total_count, # type: ignore
total_count=total_count,
users=[UserRead(**user.model_dump()) for user in users],
)

Expand Down
2 changes: 1 addition & 1 deletion src/backend/base/langflow/base/agents/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ async def run_agent(
runnable = agent
else:
runnable = AgentExecutor.from_agent_and_tools(
agent=agent, # type: ignore
agent=agent,
tools=self.tools,
handle_parsing_errors=self.handle_parsing_errors,
verbose=self.verbose,
Expand Down
4 changes: 2 additions & 2 deletions src/backend/base/langflow/base/agents/crewai/crew.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from collections.abc import Callable
from typing import cast

from crewai import Agent, Crew, Process, Task # type: ignore
from crewai.task import TaskOutput # type: ignore
from crewai import Agent, Crew, Process, Task
from crewai.task import TaskOutput
from langchain_core.agents import AgentAction, AgentFinish

from langflow.custom import Component
Expand Down
2 changes: 1 addition & 1 deletion src/backend/base/langflow/base/agents/crewai/tasks.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from crewai import Task # type: ignore
from crewai import Task


class SequentialTask(Task):
Expand Down
6 changes: 3 additions & 3 deletions src/backend/base/langflow/base/data/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def walk_level(directory: Path, max_depth: int):

def partition_file_to_data(file_path: str, silent_errors: bool) -> Data | None:
# Use the partition function to load the file
from unstructured.partition.auto import partition # type: ignore
from unstructured.partition.auto import partition

try:
elements = partition(file_path)
Expand Down Expand Up @@ -108,14 +108,14 @@ def read_text_file(file_path: str) -> str:


def read_docx_file(file_path: str) -> str:
from docx import Document # type: ignore
from docx import Document

doc = Document(file_path)
return "\n\n".join([p.text for p in doc.paragraphs])


def parse_pdf_to_text(file_path: str) -> str:
from pypdf import PdfReader # type: ignore
from pypdf import PdfReader

with Path(file_path).open("rb") as f:
reader = PdfReader(f)
Expand Down
2 changes: 1 addition & 1 deletion src/backend/base/langflow/base/io/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,4 @@ def build_with_data(
flow_id=self.graph.flow_id,
)
self.status = messages
return message_text # type: ignore
return message_text # type: ignore[return-value]
10 changes: 5 additions & 5 deletions src/backend/base/langflow/base/models/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,9 @@ def build_status_message(self, message: AIMessage):
}
}
else:
status_message = f"Response: {content}" # type: ignore
status_message = f"Response: {content}" # type: ignore[assignment]
else:
status_message = f"Response: {message.content}" # type: ignore
status_message = f"Response: {message.content}" # type: ignore[assignment]
return status_message

def get_chat_result(
Expand Down Expand Up @@ -175,16 +175,16 @@ def get_chat_result(
if self.output_parser is not None:
runnable = runnable | self.output_parser

runnable = runnable.with_config( # type: ignore
runnable = runnable.with_config(
{
"run_name": self.display_name,
"project_name": self.get_project_name(),
"callbacks": self.get_langchain_callbacks(),
}
)
if stream:
return runnable.stream(inputs) # type: ignore
message = runnable.invoke(inputs) # type: ignore
return runnable.stream(inputs)
message = runnable.invoke(inputs)
result = message.content if hasattr(message, "content") else message
if isinstance(message, AIMessage):
status_message = self.build_status_message(message)
Expand Down
2 changes: 1 addition & 1 deletion src/backend/base/langflow/base/tools/component_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def _format_tool_name(name: str):
return re.sub(r"[^a-zA-Z0-9_-]", "-", name)


class ComponentToolkit: # type: ignore
class ComponentToolkit:
def __init__(self, component: Component):
self.component = component

Expand Down
2 changes: 1 addition & 1 deletion src/backend/base/langflow/components/agents/CrewAIAgent.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from crewai import Agent # type: ignore
from crewai import Agent

from langflow.custom import Component
from langflow.io import BoolInput, DictInput, HandleInput, MultilineInput, Output
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from crewai import Crew, Process # type: ignore
from crewai import Crew, Process

from langflow.base.agents.crewai.crew import BaseCrewComponent
from langflow.io import HandleInput
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from crewai import Agent, Crew, Process, Task # type: ignore
from crewai import Agent, Crew, Process, Task

from langflow.base.agents.crewai.crew import BaseCrewComponent
from langflow.io import HandleInput
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Any

from astra_assistants import patch # type: ignore
from astra_assistants import patch
from openai import OpenAI
from openai.lib.streaming import AssistantEventHandler

Expand Down
Loading
Loading