Description
Bug description
I would expect the following code to work:
from canals import Pipeline, component
@component
class A:
@component.output_types(b=int)
def run(self, a: int, b: int = 0):
return {"c": 4}
def to_dict(self):
pass
@classmethod
def from_dict(cls, _):
pass
pipeline = Pipeline()
pipeline.add_component("a", A())
print(pipeline.run({"a": {"a": 0}}))
However, a ValueError
is raised:
Traceback (most recent call last):
File "<...>", line 16, in <module>
print(pipeline.run({"a": {"a": 0}}))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<...>/canals/pipeline/pipeline.py", line 415, in run
data = _validate_pipeline_input(self.graph, input_values=data)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<...>/canals/pipeline/validation.py", line 52, in _validate_pipeline_input
_validate_input_sockets_are_connected(graph, input_values)
File "<...>/canals/pipeline/validation.py", line 75, in _validate_input_sockets_are_connected
raise ValueError(f"Missing input: {node}.{socket.name}")
ValueError: Missing input: a.b
I can annotate b as Optional
to make it work:
from typing import Optional
from canals import Pipeline, component
@component
class A:
@component.output_types(b=int)
def run(self, a: int, b: Optional[int] = 0):
return {"c": 4}
def to_dict(self):
pass
@classmethod
def from_dict(cls, _):
pass
pipeline = Pipeline()
pipeline.add_component("a", A())
print(pipeline.run({"a": {"a": 0}}))
However, this does not have the same meaning. The Python documentation says:
Optional[X]
is equivalent toX | None
(orUnion[X, None]
).Note that this is not the same concept as an optional argument, which is one that has a default.
The naming of this type is a bit confusing because it does not have anything to do with optional parameters and it's more like an option
type in languages like OCaml, Rust, Java etc.
In my example, this would mean that None
is a valid value for b
which you would not always want.
The bug also exists in the reverse case although it's probably less likely to occur. You can see this in the following somewhat realistic example which implements a NAND components using three valued logic like you might find in SQL:
from typing import Optional
from canals import Pipeline, component
@component
class ThreeValuedNAND:
@component.output_types(y=bool)
def run(self, a: Optional[bool], b: Optional[bool]):
if a == False or b == False:
return True
if a and b:
return False
return None
def to_dict(self):
pass
@classmethod
def from_dict(cls, _):
pass
pipeline = Pipeline()
pipeline.add_component("nand", ThreeValuedNAND())
print(pipeline.run({"nand": {"a": True}}))
The validation does not catch that I haven't provided b
because it considers the b
socket to be optional. This might also have further consequences with more complicated pipelines because it does not wait for input, but I haven't investigated that.
Fix
This line should not check the type, but should rather check if a default value is provided. It might also make sense to call the key has_default
instead of is_optional
.