Skip to content
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
2 changes: 1 addition & 1 deletion ccflow/callable.py
Original file line number Diff line number Diff line change
Expand Up @@ -951,7 +951,7 @@ def _validate_callable_model_generic_type(cls, m, handler, info):

# Raise ValueError (not TypeError) as per https://docs.pydantic.dev/latest/errors/errors/
if not isinstance(m, CallableModel):
raise ValueError(f"{m} is not a CallableModel: {type(m)}") # noqa: TRY004
raise ValueError("Expected a CallableModel") # noqa: TRY004

subtypes = cls.__pydantic_generic_metadata__["args"]
if subtypes:
Expand Down
2 changes: 1 addition & 1 deletion ccflow/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def validate(cls, v) -> "Enum":
raise ValueError(f"Cannot convert value to {cls.__name__}: {v}") from e
elif isinstance(v, int):
return cls(v)
raise ValueError(f"Cannot convert value to enum: {v}")
raise ValueError(f"Cannot convert value to {cls.__name__}")

@staticmethod
def _serialize(value: "Enum") -> str:
Expand Down
6 changes: 3 additions & 3 deletions ccflow/exttypes/arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def _validate(cls, value: Any) -> Self:
if isinstance(value, pa.Schema):
return ArrowSchema.make(value)

raise ValueError(f"Cannot convert value to ArrowSchema, expects ArrowSchema or pa.Schema: {value}")
raise ValueError("Cannot convert value to ArrowSchema, expects ArrowSchema or pa.Schema")

@classmethod
def validate(cls, value: Any) -> Self:
Expand Down Expand Up @@ -89,7 +89,7 @@ def _validate(cls, v, schema, strict):
except TypeError:
pass
if not isinstance(v, pa.Table):
raise ValueError(f"Value of type {type(v)} cannot be converted to pyarrow.Table") # noqa: TRY004
raise ValueError("Value cannot be converted to pyarrow.Table") # noqa: TRY004

if schema:
if strict is True:
Expand Down Expand Up @@ -134,7 +134,7 @@ def _validate(cls, value) -> Self:
_ = value.datatype
return value

raise ValueError(f"ensure this value contains a valid PyarrowDatatype string: {value}")
raise ValueError("ensure this value contains a valid PyarrowDatatype string")

@classmethod
def validate(cls, value) -> Self:
Expand Down
2 changes: 1 addition & 1 deletion ccflow/exttypes/exprtk.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def _validate(cls, value: Any) -> Self:

return cls(value)

raise ValueError(f"{value} cannot be converted into an ExprTkExpression.")
raise ValueError("Value cannot be converted into an ExprTkExpression.")

@classmethod
def validate(cls, value: Any) -> Self:
Expand Down
8 changes: 4 additions & 4 deletions ccflow/exttypes/narwhals.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def validate_from_any(value: Any):
if cls.lazy is False and isinstance(value, nw.LazyFrame):
value = value.collect()
if cls.lazy is None and not isinstance(value, source_origin):
raise ValueError(f"Expected {source_origin} but got {type(value)}")
raise ValueError(f"Expected {source_origin}")

try:
source_arg_adapter.validate_python(value.to_native())
Expand Down Expand Up @@ -156,14 +156,14 @@ def validate_from_any(value: Any):
if inspect.isclass(value) and issubclass(value, nw.dtypes.DType):
value = value()
if not isinstance(value, nw.dtypes.DType):
raise ValueError(f"Expected a Narwhals DType but got {type(value)}") # noqa: TRY004
raise ValueError("Expected a Narwhals DType") # noqa: TRY004
return value

def serialize(value: Any):
if inspect.isclass(value) and issubclass(value, nw.dtypes.DType):
value = value()
if not isinstance(value, nw.dtypes.DType):
raise ValueError(f"Expected a Narwhals DType but got {type(value)}") # noqa: TRY004
raise ValueError("Expected a Narwhals DType") # noqa: TRY004
return str(value)

from_any_schema = core_schema.no_info_plain_validator_function(validate_from_any)
Expand Down Expand Up @@ -199,7 +199,7 @@ def __post_init__(self):
def __get_pydantic_core_schema__(self, source_type: Any, handler: GetCoreSchemaHandler) -> core_schema.CoreSchema:
def validate_schema(value: Any):
if not isinstance(value, (nw.DataFrame, nw.LazyFrame)):
raise ValueError(f"Expected a Narwhals DataFrame or LazyFrame but got {type(value)}") # noqa: TRY004
raise ValueError("Expected a Narwhals DataFrame or LazyFrame") # noqa: TRY004
schema = value.collect_schema()
if self.allow_subset:
schema = nw.Schema({k: v for k, v in schema.items() if k in self.schema})
Expand Down
2 changes: 1 addition & 1 deletion ccflow/exttypes/polars.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def _validate(cls, value: Any) -> Self:
raise ValueError(f"Supplied value '{value}' does not evaluate to a Polars expression") # noqa: TRY004
return cls._coerce_from_expr(expression)

raise ValueError(f"Supplied value '{value}' cannot be converted to a Polars expression")
raise ValueError("Cannot convert value to a Polars expression")


class _PolarsSelectorPydanticAnnotation(_PolarsExprPydanticAnnotation):
Expand Down
2 changes: 1 addition & 1 deletion ccflow/exttypes/pydantic_numpy/ndarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def _validate(v):
return np.asarray(v)

except TypeError:
raise ValueError(f"Unable to convert {v} to an array.")
raise ValueError("Unable to convert value to an array.")

return core_schema.no_info_before_validator_function(
_validate,
Expand Down
2 changes: 1 addition & 1 deletion ccflow/exttypes/pyobjectpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def _path_from_object(cls, value: Any) -> "PyObjectPath":
if hasattr(value, "__module__") and hasattr(value, "__qualname__"):
return cls(_build_standard_import_path(value))

raise ValueError(f"ensure this value contains valid import path or importable object: unable to import path for {value}")
raise ValueError("ensure this value contains valid import path or importable object")

@classmethod
@cache
Expand Down
2 changes: 1 addition & 1 deletion ccflow/publishers/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ class PandasFilePublisher(BasePublisher):
def _check_data(cls, v):
pd = sys.modules.get("pandas") # a DataFrame cannot exist unless pandas is already imported
if v is not None and (pd is None or not isinstance(v, pd.DataFrame)):
raise ValueError(f"data must be a pandas DataFrame, not {type(v).__name__}")
raise ValueError("data must be a pandas DataFrame")
return v

@override
Expand Down
22 changes: 21 additions & 1 deletion ccflow/tests/test_callable.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from pickle import dumps as pdumps, loads as ploads
from typing import Generic, TypeVar
from unittest import TestCase
from unittest.mock import Mock, patch

import cloudpickle
import ray
from pydantic import Field, ValidationError
from pydantic import BaseModel, Field, ValidationError
from ray.cloudpickle import dumps as rcpdumps, loads as rcploads

from ccflow import (
Expand Down Expand Up @@ -246,6 +247,12 @@ def __call__(self, context):
return self.model(context)


class UnionBranchHost(BaseModel):
"""Host with a ``CallableModelGenericType[Ctx, R] | R`` union field."""

field: CallableModelGenericType[MyContext, MyResult] | MyResult


class MyTest(ContextBase):
c: MyContext

Expand Down Expand Up @@ -599,6 +606,19 @@ def test_wrapper_reference(self):
self.assertEqual(w.context_type, m.context_type)
self.assertEqual(w.result_type, m.result_type)

def test_union_rejection_does_not_format_value(self):
# When a union has a generic-callable branch, pydantic still attempts that branch for a
# plain-model value. The wrap validator must reject it without stringifying the value.
inst = MyResult(x=1, y="a")
str_mock = Mock(return_value="<patched str>")
repr_mock = Mock(return_value="<patched repr>")
with patch.object(MyResult, "__str__", str_mock), patch.object(MyResult, "__repr__", repr_mock):
host = UnionBranchHost(field=inst)
str_mock.assert_not_called()
repr_mock.assert_not_called()
self.assertIsInstance(host.field, MyResult)
self.assertEqual(host.field.x, 1)

def test_use_as_base_class(self):
class MyCallable(CallableModelGenericType[NullContext, GenericResult[int]]):
@Flow.call
Expand Down
Loading