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]
|
||||
|
|
|
|||
|
|
@ -133,12 +133,12 @@ class FileMixin(BaseModel):
|
|||
def validate_file_types(cls, v):
|
||||
if not isinstance(v, list):
|
||||
msg = "file_types must be a list"
|
||||
raise ValueError(msg)
|
||||
raise ValueError(msg) # noqa: TRY004
|
||||
# types should be a list of extensions without the dot
|
||||
for file_type in v:
|
||||
if not isinstance(file_type, str):
|
||||
msg = "file_types must be a list of strings"
|
||||
raise ValueError(msg)
|
||||
raise ValueError(msg) # noqa: TRY004
|
||||
if file_type.startswith("."):
|
||||
msg = "file_types should not start with a dot"
|
||||
raise ValueError(msg)
|
||||
|
|
|
|||
|
|
@ -36,7 +36,7 @@ class TableInput(BaseInputMixin, MetadataTraceMixin, TableMixin, ListableInputMi
|
|||
# Check if value is a list of dicts
|
||||
if not isinstance(v, list):
|
||||
msg = f"TableInput value must be a list of dictionaries or Data. Value '{v}' is not a list."
|
||||
raise ValueError(msg)
|
||||
raise ValueError(msg) # noqa: TRY004
|
||||
|
||||
for item in v:
|
||||
if not isinstance(item, dict | Data):
|
||||
|
|
@ -44,7 +44,7 @@ class TableInput(BaseInputMixin, MetadataTraceMixin, TableMixin, ListableInputMi
|
|||
"TableInput value must be a list of dictionaries or Data. "
|
||||
f"Item '{item}' is not a dictionary or Data."
|
||||
)
|
||||
raise ValueError(msg)
|
||||
raise ValueError(msg) # noqa: TRY004
|
||||
return v
|
||||
|
||||
|
||||
|
|
@ -206,7 +206,7 @@ class MessageTextInput(StrInput, MetadataTraceMixin, InputTraceMixin):
|
|||
value = v
|
||||
else:
|
||||
msg = f"Invalid value type {type(v)}"
|
||||
raise ValueError(msg)
|
||||
raise ValueError(msg) # noqa: TRY004
|
||||
return value
|
||||
|
||||
|
||||
|
|
@ -462,11 +462,11 @@ class MultiselectInput(BaseInputMixin, ListableInputMixin, DropDownMixin, Metada
|
|||
# Check if value is a list of dicts
|
||||
if not isinstance(v, list):
|
||||
msg = f"MultiselectInput value must be a list. Value: '{v}'"
|
||||
raise ValueError(msg)
|
||||
raise ValueError(msg) # noqa: TRY004
|
||||
for item in v:
|
||||
if not isinstance(item, str):
|
||||
msg = f"MultiselectInput value must be a list of strings. Item: '{item}' is not a string"
|
||||
raise ValueError(msg)
|
||||
raise ValueError(msg) # noqa: TRY004
|
||||
return v
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -33,7 +33,7 @@ def create_input_schema(inputs: list["InputTypes"]) -> type[BaseModel]:
|
|||
field_type = _convert_field_type_to_type[field_type]
|
||||
else:
|
||||
msg = f"Invalid field type: {field_type}"
|
||||
raise ValueError(msg)
|
||||
raise TypeError(msg)
|
||||
if hasattr(input_model, "options") and isinstance(input_model.options, list) and input_model.options:
|
||||
literal_string = f"Literal{input_model.options}"
|
||||
# validate that the literal_string is a valid literal
|
||||
|
|
|
|||
|
|
@ -61,7 +61,7 @@ def upload_file(file_path: str, host: str, flow_id: str, components: list[str],
|
|||
tweaks[component] = {"path": response["file_path"]}
|
||||
else:
|
||||
msg = f"Component ID or name must be a string. Got {type(component)}"
|
||||
raise ValueError(msg)
|
||||
raise TypeError(msg)
|
||||
return tweaks
|
||||
msg = "Error uploading file"
|
||||
raise ValueError(msg)
|
||||
|
|
|
|||
|
|
@ -117,13 +117,13 @@ def validate_input(
|
|||
) -> list[dict[str, Any]]:
|
||||
if not isinstance(graph_data, dict) or not isinstance(tweaks, dict):
|
||||
msg = "graph_data and tweaks should be dictionaries"
|
||||
raise ValueError(msg)
|
||||
raise TypeError(msg)
|
||||
|
||||
nodes = graph_data.get("data", {}).get("nodes") or graph_data.get("nodes")
|
||||
|
||||
if not isinstance(nodes, list):
|
||||
msg = "graph_data should contain a list of nodes under 'data' key or directly under 'nodes' key"
|
||||
raise ValueError(msg)
|
||||
raise TypeError(msg)
|
||||
|
||||
return nodes
|
||||
|
||||
|
|
|
|||
|
|
@ -30,7 +30,7 @@ class Data(BaseModel):
|
|||
def validate_data(cls, values):
|
||||
if not isinstance(values, dict):
|
||||
msg = "Data must be a dictionary"
|
||||
raise ValueError(msg)
|
||||
raise ValueError(msg) # noqa: TRY004
|
||||
if not values.get("data"):
|
||||
values["data"] = {}
|
||||
# Any other keyword should be added to the data dictionary
|
||||
|
|
|
|||
|
|
@ -113,7 +113,7 @@ class CacheService(Subject, Service):
|
|||
self.add(name, obj.to_csv(), "pandas", extension="csv")
|
||||
else:
|
||||
msg = "Object is not a pandas DataFrame or Series"
|
||||
raise ValueError(msg)
|
||||
raise TypeError(msg)
|
||||
|
||||
def add_image(self, name: str, obj: Any, extension: str = "png"):
|
||||
"""
|
||||
|
|
@ -127,7 +127,7 @@ class CacheService(Subject, Service):
|
|||
self.add(name, obj, "image", extension=extension)
|
||||
else:
|
||||
msg = "Object is not a PIL Image"
|
||||
raise ValueError(msg)
|
||||
raise TypeError(msg)
|
||||
|
||||
def get(self, name: str):
|
||||
"""
|
||||
|
|
|
|||
|
|
@ -116,7 +116,7 @@ class FlowBase(SQLModel):
|
|||
return v
|
||||
if not isinstance(v, dict):
|
||||
msg = "Flow must be a valid JSON"
|
||||
raise ValueError(msg)
|
||||
raise ValueError(msg) # noqa: TRY004
|
||||
|
||||
# data must contain nodes and edges
|
||||
if "nodes" not in v:
|
||||
|
|
|
|||
|
|
@ -208,7 +208,7 @@ class OpenTelemetry(metaclass=ThreadSafeSingletonMetaUsingWeakref):
|
|||
counter.add(value, labels)
|
||||
else:
|
||||
msg = f"Metric '{metric_name}' is not a counter"
|
||||
raise ValueError(msg)
|
||||
raise TypeError(msg)
|
||||
|
||||
def up_down_counter(self, metric_name: str, value: float, labels: Mapping[str, str]):
|
||||
self.validate_labels(metric_name, labels)
|
||||
|
|
@ -217,7 +217,7 @@ class OpenTelemetry(metaclass=ThreadSafeSingletonMetaUsingWeakref):
|
|||
up_down_counter.add(value, labels)
|
||||
else:
|
||||
msg = f"Metric '{metric_name}' is not an up down counter"
|
||||
raise ValueError(msg)
|
||||
raise TypeError(msg)
|
||||
|
||||
def update_gauge(self, metric_name: str, value: float, labels: Mapping[str, str]):
|
||||
self.validate_labels(metric_name, labels)
|
||||
|
|
@ -226,7 +226,7 @@ class OpenTelemetry(metaclass=ThreadSafeSingletonMetaUsingWeakref):
|
|||
gauge.set_value(value, labels)
|
||||
else:
|
||||
msg = f"Metric '{metric_name}' is not a gauge"
|
||||
raise ValueError(msg)
|
||||
raise TypeError(msg)
|
||||
|
||||
def observe_histogram(self, metric_name: str, value: float, labels: Mapping[str, str]):
|
||||
self.validate_labels(metric_name, labels)
|
||||
|
|
@ -235,4 +235,4 @@ class OpenTelemetry(metaclass=ThreadSafeSingletonMetaUsingWeakref):
|
|||
histogram.record(value, labels)
|
||||
else:
|
||||
msg = f"Metric '{metric_name}' is not a histogram"
|
||||
raise ValueError(msg)
|
||||
raise TypeError(msg)
|
||||
|
|
|
|||
|
|
@ -138,7 +138,7 @@ class Input(BaseModel):
|
|||
def validate_file_types(cls, value):
|
||||
if not isinstance(value, list):
|
||||
msg = "file_types must be a list"
|
||||
raise ValueError(msg)
|
||||
raise ValueError(msg) # noqa: TRY004
|
||||
return [
|
||||
(f".{file_type}" if isinstance(file_type, str) and not file_type.startswith(".") else file_type)
|
||||
for file_type in value
|
||||
|
|
@ -155,7 +155,7 @@ class Input(BaseModel):
|
|||
v = format_type(v)
|
||||
elif not isinstance(v, str):
|
||||
msg = f"type must be a string or a type, not {type(v)}"
|
||||
raise ValueError(msg)
|
||||
raise ValueError(msg) # noqa: TRY004
|
||||
return v
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -37,7 +37,7 @@ class ChatOutputResponse(BaseModel):
|
|||
for file in files:
|
||||
if not isinstance(file, dict):
|
||||
msg = "Files must be a list of dictionaries."
|
||||
raise ValueError(msg)
|
||||
raise ValueError(msg) # noqa: TRY004
|
||||
|
||||
if not all(key in file for key in ["path", "name", "type"]):
|
||||
# If any of the keys are missing, we should extract the
|
||||
|
|
|
|||
|
|
@ -62,12 +62,14 @@ ignore = [
|
|||
"EXE",
|
||||
"FBT",
|
||||
"N",
|
||||
"PGH",
|
||||
"RUF006", # Store a reference to the return value of `asyncio.create_task`
|
||||
"S",
|
||||
"SLF",
|
||||
"T201",
|
||||
"TRY",
|
||||
"PGH",
|
||||
"TRY002",
|
||||
"TRY2",
|
||||
"TRY3",
|
||||
]
|
||||
|
||||
[tool.ruff.lint.per-file-ignores]
|
||||
|
|
|
|||
|
|
@ -76,8 +76,8 @@ class TestCreateStateModel:
|
|||
assert state_instance.field_two == 123
|
||||
|
||||
# Raises ValueError for invalid field type in tuple-based definitions
|
||||
def test_raise_valueerror_for_invalid_field_type_in_tuple(self):
|
||||
with pytest.raises(ValueError, match="Invalid type for field invalid_field"):
|
||||
def test_raise_typeerror_for_invalid_field_type_in_tuple(self):
|
||||
with pytest.raises(TypeError, match="Invalid type for field invalid_field"):
|
||||
create_state_model(invalid_field=("not_a_type", "default"))
|
||||
|
||||
# Raises ValueError for unsupported value types in keyword arguments
|
||||
|
|
|
|||
|
|
@ -24,17 +24,17 @@ def test_gauge(opentelemetry_instance):
|
|||
|
||||
|
||||
def test_gauge_with_counter_method(opentelemetry_instance):
|
||||
with pytest.raises(ValueError, match="Metric 'file_uploads' is not a counter"):
|
||||
with pytest.raises(TypeError, match="Metric 'file_uploads' is not a counter"):
|
||||
opentelemetry_instance.increment_counter(metric_name="file_uploads", value=1, labels=fixed_labels)
|
||||
|
||||
|
||||
def test_gauge_with_historgram_method(opentelemetry_instance):
|
||||
with pytest.raises(ValueError, match="Metric 'file_uploads' is not a histogram"):
|
||||
with pytest.raises(TypeError, match="Metric 'file_uploads' is not a histogram"):
|
||||
opentelemetry_instance.observe_histogram("file_uploads", 1, fixed_labels)
|
||||
|
||||
|
||||
def test_gauge_with_up_down_counter_method(opentelemetry_instance):
|
||||
with pytest.raises(ValueError, match="Metric 'file_uploads' is not an up down counter"):
|
||||
with pytest.raises(TypeError, match="Metric 'file_uploads' is not an up down counter"):
|
||||
opentelemetry_instance.up_down_counter("file_uploads", 1, labels=fixed_labels)
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue