ref: Add ruff rules for perf (PERF) (#4047)
Add ruff rules for perf (PERF)
This commit is contained in:
parent
dce5523f5e
commit
ee3035be28
17 changed files with 36 additions and 67 deletions
|
|
@ -18,7 +18,6 @@ def chroma_collection_to_data(collection_dict: dict):
|
||||||
"text": doc,
|
"text": doc,
|
||||||
}
|
}
|
||||||
if ("metadatas" in collection_dict) and collection_dict["metadatas"][i]:
|
if ("metadatas" in collection_dict) and collection_dict["metadatas"][i]:
|
||||||
for key, value in collection_dict["metadatas"][i].items():
|
data_dict.update(collection_dict["metadatas"][i].items())
|
||||||
data_dict[key] = value
|
|
||||||
data.append(Data(**data_dict))
|
data.append(Data(**data_dict))
|
||||||
return data
|
return data
|
||||||
|
|
|
||||||
|
|
@ -45,18 +45,12 @@ class SplitTextComponent(Component):
|
||||||
]
|
]
|
||||||
|
|
||||||
def _docs_to_data(self, docs):
|
def _docs_to_data(self, docs):
|
||||||
data = []
|
return [Data(text=doc.page_content, data=doc.metadata) for doc in docs]
|
||||||
for doc in docs:
|
|
||||||
data.append(Data(text=doc.page_content, data=doc.metadata))
|
|
||||||
return data
|
|
||||||
|
|
||||||
def split_text(self) -> list[Data]:
|
def split_text(self) -> list[Data]:
|
||||||
separator = unescape_string(self.separator)
|
separator = unescape_string(self.separator)
|
||||||
|
|
||||||
documents = []
|
documents = [_input.to_lc_document() for _input in self.data_inputs if isinstance(_input, Data)]
|
||||||
for _input in self.data_inputs:
|
|
||||||
if isinstance(_input, Data):
|
|
||||||
documents.append(_input.to_lc_document())
|
|
||||||
|
|
||||||
splitter = CharacterTextSplitter(
|
splitter = CharacterTextSplitter(
|
||||||
chunk_overlap=self.chunk_overlap,
|
chunk_overlap=self.chunk_overlap,
|
||||||
|
|
|
||||||
|
|
@ -69,10 +69,8 @@ class CSVToDataComponent(Component):
|
||||||
msg = "No CSV data provided."
|
msg = "No CSV data provided."
|
||||||
raise ValueError(msg)
|
raise ValueError(msg)
|
||||||
|
|
||||||
result = []
|
|
||||||
csv_reader = csv.DictReader(io.StringIO(csv_data))
|
csv_reader = csv.DictReader(io.StringIO(csv_data))
|
||||||
for row in csv_reader:
|
result = [Data(data=row) for row in csv_reader]
|
||||||
result.append(Data(data=row))
|
|
||||||
|
|
||||||
if not result:
|
if not result:
|
||||||
self.status = "The CSV data is empty."
|
self.status = "The CSV data is empty."
|
||||||
|
|
|
||||||
|
|
@ -45,18 +45,12 @@ class SplitTextComponent(Component):
|
||||||
]
|
]
|
||||||
|
|
||||||
def _docs_to_data(self, docs):
|
def _docs_to_data(self, docs):
|
||||||
data = []
|
return [Data(text=doc.page_content, data=doc.metadata) for doc in docs]
|
||||||
for doc in docs:
|
|
||||||
data.append(Data(text=doc.page_content, data=doc.metadata))
|
|
||||||
return data
|
|
||||||
|
|
||||||
def split_text(self) -> list[Data]:
|
def split_text(self) -> list[Data]:
|
||||||
separator = unescape_string(self.separator)
|
separator = unescape_string(self.separator)
|
||||||
|
|
||||||
documents = []
|
documents = [_input.to_lc_document() for _input in self.data_inputs if isinstance(_input, Data)]
|
||||||
for _input in self.data_inputs:
|
|
||||||
if isinstance(_input, Data):
|
|
||||||
documents.append(_input.to_lc_document())
|
|
||||||
|
|
||||||
splitter = CharacterTextSplitter(
|
splitter = CharacterTextSplitter(
|
||||||
chunk_overlap=self.chunk_overlap,
|
chunk_overlap=self.chunk_overlap,
|
||||||
|
|
|
||||||
|
|
@ -51,9 +51,7 @@ class LangChainHubPromptComponent(Component):
|
||||||
template_messages = [HumanMessagePromptTemplate(prompt=template)]
|
template_messages = [HumanMessagePromptTemplate(prompt=template)]
|
||||||
|
|
||||||
# Extract the messages from the prompt data
|
# Extract the messages from the prompt data
|
||||||
prompt_template = []
|
prompt_template = [message_data.prompt for message_data in template_messages]
|
||||||
for message_data in template_messages:
|
|
||||||
prompt_template.append(message_data.prompt)
|
|
||||||
|
|
||||||
# Regular expression to find all instances of {<string>}
|
# Regular expression to find all instances of {<string>}
|
||||||
pattern = r"\{(.*?)\}"
|
pattern = r"\{(.*?)\}"
|
||||||
|
|
@ -77,7 +75,7 @@ class LangChainHubPromptComponent(Component):
|
||||||
build_config["langchain_hub_prompt"]["info"] = full_template
|
build_config["langchain_hub_prompt"]["info"] = full_template
|
||||||
|
|
||||||
# Remove old parameter inputs if any
|
# Remove old parameter inputs if any
|
||||||
for key, _ in build_config.copy().items():
|
for key in build_config.copy():
|
||||||
if key.startswith("param_"):
|
if key.startswith("param_"):
|
||||||
del build_config[key]
|
del build_config[key]
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -138,10 +138,7 @@ class GleanSearchAPIComponent(LCToolComponent):
|
||||||
)
|
)
|
||||||
|
|
||||||
# Build the data
|
# Build the data
|
||||||
data = []
|
data = [Data(data=result, text=result["snippets"][0]["text"]) for result in results]
|
||||||
for result in results:
|
|
||||||
data.append(Data(data=result, text=result["snippets"][0]["text"]))
|
|
||||||
|
|
||||||
self.status = data
|
self.status = data
|
||||||
|
|
||||||
return data
|
return data
|
||||||
|
|
|
||||||
|
|
@ -292,13 +292,12 @@ class PythonCodeStructuredTool(LCToolComponent):
|
||||||
return classes, functions
|
return classes, functions
|
||||||
|
|
||||||
def _find_imports(self, code: str) -> dotdict:
|
def _find_imports(self, code: str) -> dotdict:
|
||||||
imports = []
|
imports: list[str] = []
|
||||||
from_imports = []
|
from_imports = []
|
||||||
parsed_code = ast.parse(code)
|
parsed_code = ast.parse(code)
|
||||||
for node in parsed_code.body:
|
for node in parsed_code.body:
|
||||||
if isinstance(node, ast.Import):
|
if isinstance(node, ast.Import):
|
||||||
for alias in node.names:
|
imports.extend(alias.name for alias in node.names)
|
||||||
imports.append(alias.name)
|
|
||||||
elif isinstance(node, ast.ImportFrom):
|
elif isinstance(node, ast.ImportFrom):
|
||||||
from_imports.append(node)
|
from_imports.append(node)
|
||||||
return dotdict({"imports": imports, "from_imports": from_imports})
|
return dotdict({"imports": imports, "from_imports": from_imports})
|
||||||
|
|
|
||||||
|
|
@ -70,9 +70,7 @@ class SearXNGToolComponent(LCToolComponent):
|
||||||
for selected_category in build_config["categories"]["value"]:
|
for selected_category in build_config["categories"]["value"]:
|
||||||
if selected_category not in build_config["categories"]["options"]:
|
if selected_category not in build_config["categories"]["options"]:
|
||||||
build_config["categories"]["value"].remove(selected_category)
|
build_config["categories"]["value"].remove(selected_category)
|
||||||
languages = []
|
languages = list(data["locales"])
|
||||||
for language in data["locales"]:
|
|
||||||
languages.append(language)
|
|
||||||
build_config["language"]["options"] = languages.copy()
|
build_config["language"]["options"] = languages.copy()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.status = f"Failed to extract names: {e}"
|
self.status = f"Failed to extract names: {e}"
|
||||||
|
|
@ -107,11 +105,8 @@ class SearXNGToolComponent(LCToolComponent):
|
||||||
},
|
},
|
||||||
).json()
|
).json()
|
||||||
|
|
||||||
results = []
|
|
||||||
num_results = min(SearxSearch._max_results, len(response["results"]))
|
num_results = min(SearxSearch._max_results, len(response["results"]))
|
||||||
for i in range(num_results):
|
return [response["results"][i] for i in range(num_results)]
|
||||||
results.append(response["results"][i])
|
|
||||||
return results
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return [f"Failed to search: {e}"]
|
return [f"Failed to search: {e}"]
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -389,8 +389,7 @@ class CodeParser:
|
||||||
bases = []
|
bases = []
|
||||||
for base in dunder_class.__bases__:
|
for base in dunder_class.__bases__:
|
||||||
bases.append(base)
|
bases.append(base)
|
||||||
for bases_base in base.__bases__:
|
bases.extend(base.__bases__)
|
||||||
bases.append(bases_base)
|
|
||||||
return bases
|
return bases
|
||||||
|
|
||||||
def parse_code(self) -> dict[str, Any]:
|
def parse_code(self) -> dict[str, Any]:
|
||||||
|
|
|
||||||
|
|
@ -404,11 +404,13 @@ class Component(CustomComponent):
|
||||||
# Get the input object from the current component
|
# Get the input object from the current component
|
||||||
input_ = self._inputs[input_name]
|
input_ = self._inputs[input_name]
|
||||||
# Iterate over outputs to find matches based on types
|
# Iterate over outputs to find matches based on types
|
||||||
for output in outputs:
|
matching_pairs = [
|
||||||
for output_type in output.types:
|
(output, input_)
|
||||||
# Check if the output type matches the input's accepted types
|
for output in outputs
|
||||||
if input_.input_types and output_type in input_.input_types:
|
for output_type in output.types
|
||||||
matching_pairs.append((output, input_))
|
# Check if the output type matches the input's accepted types
|
||||||
|
if input_.input_types and output_type in input_.input_types
|
||||||
|
]
|
||||||
# If multiple matches are found, raise an error indicating ambiguity
|
# If multiple matches are found, raise an error indicating ambiguity
|
||||||
if len(matching_pairs) > 1:
|
if len(matching_pairs) > 1:
|
||||||
matching_pairs_str = self._build_error_string_from_matching_pairs(matching_pairs)
|
matching_pairs_str = self._build_error_string_from_matching_pairs(matching_pairs)
|
||||||
|
|
@ -593,7 +595,7 @@ class Component(CustomComponent):
|
||||||
#! works and then update this later
|
#! works and then update this later
|
||||||
field_config = self.get_template_config(self)
|
field_config = self.get_template_config(self)
|
||||||
frontend_node = ComponentFrontendNode.from_inputs(**field_config)
|
frontend_node = ComponentFrontendNode.from_inputs(**field_config)
|
||||||
for key, _value in self._inputs.items():
|
for key in self._inputs:
|
||||||
frontend_node.set_field_load_from_db_in_template(key, False)
|
frontend_node.set_field_load_from_db_in_template(key, False)
|
||||||
self._map_parameters_on_frontend_node(frontend_node)
|
self._map_parameters_on_frontend_node(frontend_node)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -64,9 +64,7 @@ def reorder_fields(frontend_node: CustomComponentFrontendNode, field_order: list
|
||||||
field_dict = {field.name: field for field in frontend_node.template.fields}
|
field_dict = {field.name: field for field in frontend_node.template.fields}
|
||||||
reordered_fields = [field_dict[name] for name in field_order if name in field_dict]
|
reordered_fields = [field_dict[name] for name in field_order if name in field_dict]
|
||||||
# Add any fields that are not in the field_order list
|
# Add any fields that are not in the field_order list
|
||||||
for field in frontend_node.template.fields:
|
reordered_fields.extend(field for field in frontend_node.template.fields if field.name not in field_order)
|
||||||
if field.name not in field_order:
|
|
||||||
reordered_fields.append(field)
|
|
||||||
frontend_node.template.fields = reordered_fields
|
frontend_node.template.fields = reordered_fields
|
||||||
frontend_node.field_order = field_order
|
frontend_node.field_order = field_order
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -118,7 +118,7 @@ def update_template(template, g_nodes):
|
||||||
Returns:
|
Returns:
|
||||||
None
|
None
|
||||||
"""
|
"""
|
||||||
for _, value in template.items():
|
for value in template.values():
|
||||||
if not value.get("proxy"):
|
if not value.get("proxy"):
|
||||||
continue
|
continue
|
||||||
proxy_dict = value["proxy"]
|
proxy_dict = value["proxy"]
|
||||||
|
|
|
||||||
|
|
@ -228,11 +228,7 @@ def get_flow_inputs(graph: Graph) -> list[Vertex]:
|
||||||
Returns:
|
Returns:
|
||||||
List[Data]: A list of input data, where each record contains the ID, name, and description of the input vertex.
|
List[Data]: A list of input data, where each record contains the ID, name, and description of the input vertex.
|
||||||
"""
|
"""
|
||||||
inputs = []
|
return [vertex for vertex in graph.vertices if vertex.is_input]
|
||||||
for vertex in graph.vertices:
|
|
||||||
if vertex.is_input:
|
|
||||||
inputs.append(vertex)
|
|
||||||
return inputs
|
|
||||||
|
|
||||||
|
|
||||||
def build_schema_from_inputs(name: str, inputs: list[Vertex]) -> type[BaseModel]:
|
def build_schema_from_inputs(name: str, inputs: list[Vertex]) -> type[BaseModel]:
|
||||||
|
|
|
||||||
|
|
@ -98,10 +98,7 @@ class SizedLogBuffer:
|
||||||
try:
|
try:
|
||||||
with self._wlock:
|
with self._wlock:
|
||||||
as_list = list(self.buffer)
|
as_list = list(self.buffer)
|
||||||
rc = {}
|
return dict(as_list[-last_idx:])
|
||||||
for ts, msg in as_list[-last_idx:]:
|
|
||||||
rc[ts] = msg
|
|
||||||
return rc
|
|
||||||
finally:
|
finally:
|
||||||
self._rsemaphore.release()
|
self._rsemaphore.release()
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -66,9 +66,7 @@ def add_messages(messages: Message | list[Message], flow_id: str | None = None):
|
||||||
msg = f"The messages must be instances of Message. Found: {types}"
|
msg = f"The messages must be instances of Message. Found: {types}"
|
||||||
raise ValueError(msg)
|
raise ValueError(msg)
|
||||||
|
|
||||||
messages_models: list[MessageTable] = []
|
messages_models = [MessageTable.from_message(msg, flow_id=flow_id) for msg in messages]
|
||||||
for msg in messages:
|
|
||||||
messages_models.append(MessageTable.from_message(msg, flow_id=flow_id))
|
|
||||||
with session_scope() as session:
|
with session_scope() as session:
|
||||||
messages_models = add_messagetables(messages_models, session)
|
messages_models = add_messagetables(messages_models, session)
|
||||||
return [Message(**message.model_dump()) for message in messages_models]
|
return [Message(**message.model_dump()) for message in messages_models]
|
||||||
|
|
|
||||||
|
|
@ -74,10 +74,13 @@ def import_all_services_into_a_dict():
|
||||||
service_name = ServiceType(service_type).value.replace("_service", "")
|
service_name = ServiceType(service_type).value.replace("_service", "")
|
||||||
module_name = f"langflow.services.{service_name}.service"
|
module_name = f"langflow.services.{service_name}.service"
|
||||||
module = importlib.import_module(module_name)
|
module = importlib.import_module(module_name)
|
||||||
for name, obj in inspect.getmembers(module, inspect.isclass):
|
services.update(
|
||||||
if issubclass(obj, Service) and obj is not Service:
|
{
|
||||||
services[name] = obj
|
name: obj
|
||||||
# break
|
for name, obj in inspect.getmembers(module, inspect.isclass)
|
||||||
|
if issubclass(obj, Service) and obj is not Service
|
||||||
|
}
|
||||||
|
)
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
logger.exception(exc)
|
logger.exception(exc)
|
||||||
msg = "Could not initialize services. Please check your settings."
|
msg = "Could not initialize services. Please check your settings."
|
||||||
|
|
|
||||||
|
|
@ -62,6 +62,7 @@ select = [
|
||||||
"LOG",
|
"LOG",
|
||||||
"NPY",
|
"NPY",
|
||||||
"PD",
|
"PD",
|
||||||
|
"PERF",
|
||||||
"PIE",
|
"PIE",
|
||||||
"PT",
|
"PT",
|
||||||
"PTH",
|
"PTH",
|
||||||
|
|
@ -82,6 +83,7 @@ select = [
|
||||||
ignore = [
|
ignore = [
|
||||||
"COM812", # Messes with the formatter
|
"COM812", # Messes with the formatter
|
||||||
"ISC001", # Messes with the formatter
|
"ISC001", # Messes with the formatter
|
||||||
|
"PERF203", # Rarely useful
|
||||||
"RUF006", # TODO (Store a reference to the return value of `asyncio.create_task`)
|
"RUF006", # TODO (Store a reference to the return value of `asyncio.create_task`)
|
||||||
"RUF012", # TODO (Mutable class attributes should be annotated with `typing.ClassVar`)
|
"RUF012", # TODO (Mutable class attributes should be annotated with `typing.ClassVar`)
|
||||||
]
|
]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue