ref: Use TypeError instead of ValueError when relevant (TRY004) (#4045)
Use TypeError instead of ValueError when relevant (TRY004)
This commit is contained in:
parent
12a381a3fa
commit
ba6838c13c
34 changed files with 56 additions and 54 deletions
|
|
@ -61,7 +61,7 @@ class ChatComponent(Component):
|
|||
iterator = message.text
|
||||
if not isinstance(iterator, AsyncIterator | Iterator):
|
||||
msg = "The message must be an iterator or an async iterator."
|
||||
raise ValueError(msg)
|
||||
raise TypeError(msg)
|
||||
|
||||
if isinstance(iterator, AsyncIterator):
|
||||
return run_until_complete(self._handle_async_iterator(iterator, message, message_id))
|
||||
|
|
|
|||
|
|
@ -27,7 +27,7 @@ class MessageToDataComponent(Component):
|
|||
try:
|
||||
if not isinstance(self.message, Message):
|
||||
msg = "Input must be a Message object"
|
||||
raise ValueError(msg)
|
||||
raise TypeError(msg)
|
||||
|
||||
# Convert Message to Data
|
||||
data = Data(data=self.message.data)
|
||||
|
|
|
|||
|
|
@ -119,7 +119,7 @@ class RunnableExecComponent(Component):
|
|||
input_dict, status = self.get_input_dict(self.runnable, self.input_key, self.input_value)
|
||||
if not isinstance(self.runnable, AgentExecutor):
|
||||
msg = "The runnable must be an AgentExecutor"
|
||||
raise ValueError(msg)
|
||||
raise TypeError(msg)
|
||||
|
||||
if self.use_stream:
|
||||
return self.astream_events(input_dict)
|
||||
|
|
|
|||
|
|
@ -67,7 +67,7 @@ class SelfQueryRetrieverComponent(Component):
|
|||
input_text = self.query
|
||||
else:
|
||||
msg = f"Query type {type(self.query)} not supported."
|
||||
raise ValueError(msg)
|
||||
raise TypeError(msg)
|
||||
|
||||
documents = self_query_retriever.invoke(input=input_text, config={"callbacks": self.get_langchain_callbacks()})
|
||||
data = [Data.from_document(document) for document in documents]
|
||||
|
|
|
|||
|
|
@ -55,7 +55,7 @@ class PythonREPLToolComponent(LCToolComponent):
|
|||
modules = global_imports
|
||||
else:
|
||||
msg = "global_imports must be either a string or a list"
|
||||
raise ValueError(msg)
|
||||
raise TypeError(msg)
|
||||
|
||||
for module in modules:
|
||||
try:
|
||||
|
|
|
|||
|
|
@ -441,7 +441,7 @@ class AstraVectorStoreComponent(LCVectorStoreComponent):
|
|||
documents.append(_input.to_lc_document())
|
||||
else:
|
||||
msg = "Vector Store Inputs must be Data objects."
|
||||
raise ValueError(msg)
|
||||
raise TypeError(msg)
|
||||
|
||||
if documents:
|
||||
logger.debug(f"Adding {len(documents)} documents to the Vector Store.")
|
||||
|
|
|
|||
|
|
@ -156,7 +156,7 @@ class ChromaVectorStoreComponent(LCVectorStoreComponent):
|
|||
documents.append(_input.to_lc_document())
|
||||
else:
|
||||
msg = "Vector Store Inputs must be Data objects."
|
||||
raise ValueError(msg)
|
||||
raise TypeError(msg)
|
||||
|
||||
if documents and self.embedding is not None:
|
||||
logger.debug(f"Adding {len(documents)} documents to the Vector Store.")
|
||||
|
|
|
|||
|
|
@ -162,7 +162,7 @@ class ElasticsearchVectorStoreComponent(LCVectorStoreComponent):
|
|||
else:
|
||||
error_message = "Vector Store Inputs must be Data objects."
|
||||
logger.error(error_message)
|
||||
raise ValueError(error_message)
|
||||
raise TypeError(error_message)
|
||||
return documents
|
||||
|
||||
def _add_documents_to_vector_store(self, vector_store: "ElasticsearchStore") -> None:
|
||||
|
|
|
|||
|
|
@ -260,7 +260,7 @@ class HCDVectorStoreComponent(LCVectorStoreComponent):
|
|||
documents.append(_input.to_lc_document())
|
||||
else:
|
||||
msg = "Vector Store Inputs must be Data objects."
|
||||
raise ValueError(msg)
|
||||
raise TypeError(msg)
|
||||
|
||||
if documents:
|
||||
logger.debug(f"Adding {len(documents)} documents to the Vector Store.")
|
||||
|
|
|
|||
|
|
@ -159,7 +159,7 @@ class OpenSearchVectorStoreComponent(LCVectorStoreComponent):
|
|||
else:
|
||||
error_message = f"Expected Data object, got {type(_input)}"
|
||||
logger.error(error_message)
|
||||
raise ValueError(error_message)
|
||||
raise TypeError(error_message)
|
||||
|
||||
if documents and self.embedding is not None:
|
||||
logger.debug(f"Adding {len(documents)} documents to the Vector Store.")
|
||||
|
|
|
|||
|
|
@ -87,7 +87,7 @@ class QdrantVectorStoreComponent(LCVectorStoreComponent):
|
|||
|
||||
if not isinstance(self.embedding, Embeddings):
|
||||
msg = "Invalid embedding object"
|
||||
raise ValueError(msg)
|
||||
raise TypeError(msg)
|
||||
|
||||
if documents:
|
||||
qdrant = Qdrant.from_documents(documents, embedding=self.embedding, **qdrant_kwargs, **server_kwargs)
|
||||
|
|
|
|||
|
|
@ -430,7 +430,7 @@ class Component(CustomComponent):
|
|||
# Ensure that the output method is a valid method name (string)
|
||||
if not isinstance(output.method, str):
|
||||
msg = f"Method {output.method} is not a valid output of {value.__class__.__name__}"
|
||||
raise ValueError(msg)
|
||||
raise TypeError(msg)
|
||||
return getattr(value, output.method)
|
||||
|
||||
def _process_connection_or_parameter(self, key, value):
|
||||
|
|
@ -503,7 +503,7 @@ class Component(CustomComponent):
|
|||
f"You set {value.display_name} as value for `{key}`. "
|
||||
f"You should pass one of the following: {methods}"
|
||||
)
|
||||
raise ValueError(msg)
|
||||
raise TypeError(msg)
|
||||
self._set_input_value(key, value)
|
||||
self._parameters[key] = value
|
||||
self._attributes[key] = value
|
||||
|
|
|
|||
|
|
@ -288,7 +288,7 @@ class CustomComponent(BaseComponent):
|
|||
data_dict = item.copy()
|
||||
else:
|
||||
msg = f"Invalid data type: {type(item)}"
|
||||
raise ValueError(msg)
|
||||
raise TypeError(msg)
|
||||
|
||||
data_objects.append(Data(data=data_dict))
|
||||
|
||||
|
|
|
|||
|
|
@ -268,7 +268,7 @@ def get_component_instance(custom_component: CustomComponent, user_id: str | UUI
|
|||
custom_class = eval_custom_component_code(custom_component._code)
|
||||
else:
|
||||
msg = "Invalid code type"
|
||||
raise ValueError(msg)
|
||||
raise TypeError(msg)
|
||||
except Exception as exc:
|
||||
logger.exception("Error while evaluating custom component code")
|
||||
raise HTTPException(
|
||||
|
|
@ -303,7 +303,7 @@ def run_build_config(
|
|||
custom_class = eval_custom_component_code(custom_component._code)
|
||||
else:
|
||||
msg = "Invalid code type"
|
||||
raise ValueError(msg)
|
||||
raise TypeError(msg)
|
||||
except Exception as exc:
|
||||
logger.exception("Error while evaluating custom component code")
|
||||
raise HTTPException(
|
||||
|
|
|
|||
|
|
@ -27,7 +27,7 @@ class EventManager:
|
|||
def _validate_callback(callback: EventCallback):
|
||||
if not callable(callback):
|
||||
msg = "Callback must be callable"
|
||||
raise ValueError(msg)
|
||||
raise TypeError(msg)
|
||||
# Check if it has `self, event_type and data`
|
||||
sig = inspect.signature(callback)
|
||||
parameters = ["manager", "event_type", "data"]
|
||||
|
|
|
|||
|
|
@ -248,11 +248,11 @@ class Graph:
|
|||
source_vertex = self.get_vertex(source_id)
|
||||
if not isinstance(source_vertex, ComponentVertex):
|
||||
msg = f"Source vertex {source_id} is not a component vertex."
|
||||
raise ValueError(msg)
|
||||
raise TypeError(msg)
|
||||
target_vertex = self.get_vertex(target_id)
|
||||
if not isinstance(target_vertex, ComponentVertex):
|
||||
msg = f"Target vertex {target_id} is not a component vertex."
|
||||
raise ValueError(msg)
|
||||
raise TypeError(msg)
|
||||
output_name, input_name = output_input_tuple
|
||||
if source_vertex._custom_component is None:
|
||||
msg = f"Source vertex {source_id} does not have a custom component."
|
||||
|
|
@ -661,7 +661,7 @@ class Graph:
|
|||
|
||||
if not isinstance(inputs.get(INPUT_FIELD_NAME, ""), str):
|
||||
msg = f"Invalid input value: {inputs.get(INPUT_FIELD_NAME)}. Expected string"
|
||||
raise ValueError(msg)
|
||||
raise TypeError(msg)
|
||||
if inputs:
|
||||
self._set_inputs(input_components, inputs, input_type)
|
||||
# Update all the vertices with the session_id
|
||||
|
|
@ -1529,7 +1529,7 @@ class Graph:
|
|||
vertices.append(result.vertex)
|
||||
else:
|
||||
msg = f"Invalid result from task {task_name}: {result}"
|
||||
raise ValueError(msg)
|
||||
raise TypeError(msg)
|
||||
|
||||
for v in vertices:
|
||||
# set all executed vertices as non-runnable to not run them again.
|
||||
|
|
|
|||
|
|
@ -230,7 +230,7 @@ def create_state_model(model_name: str = "State", validate: bool = True, **kwarg
|
|||
# typing.Annotated[<type>, Field(...)]
|
||||
if not isinstance(value[0], type):
|
||||
msg = f"Invalid type for field {name}: {type(value[0])}"
|
||||
raise ValueError(msg)
|
||||
raise TypeError(msg)
|
||||
fields[name] = (value[0], value[1])
|
||||
else:
|
||||
msg = f"Invalid value type {type(value)} for field {name}"
|
||||
|
|
|
|||
|
|
@ -750,7 +750,7 @@ class Vertex:
|
|||
"""
|
||||
if isinstance(self._built_object, UnbuiltObject):
|
||||
msg = f"{self.display_name}: {self._built_object_repr()}"
|
||||
raise ValueError(msg)
|
||||
raise TypeError(msg)
|
||||
if self._built_object is None:
|
||||
message = f"{self.display_name} returned None."
|
||||
if self.base_type == "custom_components":
|
||||
|
|
|
|||
|
|
@ -382,7 +382,7 @@ class InterfaceVertex(ComponentVertex):
|
|||
iterator = self.params.get(INPUT_FIELD_NAME, None)
|
||||
if not isinstance(iterator, AsyncIterator | Iterator):
|
||||
msg = "The message must be an iterator or an async iterator."
|
||||
raise ValueError(msg)
|
||||
raise TypeError(msg)
|
||||
is_async = isinstance(iterator, AsyncIterator)
|
||||
complete_message = ""
|
||||
if is_async:
|
||||
|
|
|
|||
|
|
@ -58,7 +58,7 @@ def messages_to_text(template: str, messages: Message | list[Message]) -> str:
|
|||
# If it is not a message, create one with the key "text"
|
||||
if not isinstance(message, Message):
|
||||
msg = "All elements in the list must be of type Message."
|
||||
raise ValueError(msg)
|
||||
raise TypeError(msg)
|
||||
_messages.append(message)
|
||||
|
||||
formated_messages = [template.format(data=message.model_dump(), **message.model_dump()) for message in _messages]
|
||||
|
|
|
|||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Reference in a new issue