Skip to content
This repository has been archived by the owner on Nov 28, 2023. It is now read-only.

feat: Add Component inputs/outputs functions #158

Merged
merged 4 commits into from
Nov 14, 2023
Merged

Conversation

vblagoje
Copy link
Member

@vblagoje vblagoje commented Nov 8, 2023

Why:

To enable introspection of Canals components, namely the ability to retrieve information about component inputs and outputs programmatically.

What:

Two methods, component_inputs and component_outputs, were added in canals/component/descriptions.py

def component_inputs(component: Any) -> Dict[str, Dict[str, Any]]:
...
def component_outputs(component: Any) -> Dict[str, Dict[str, Any]]:

How can it be used:

Once this PR is merged, Canals component instances could be inspected for inputs/outputs:

from canals import component
from canals.component.descriptions import component_inputs, component_outputs

@component
class MockComponent:
    def run(self, value: int):
        return {"value": 1}

comp = MockComponent()
assert component_inputs(comp) == {"value": {"is_optional": False, "type": int}}



@component
class MockComponent:
    @component.output_types(value1=int, value2=str)
    def run(self):
        return {"value1": 1, "value2": "test"}

comp = MockComponent()
assert component_outputs(comp) == {"value1": {"type": int}, "value2": {"type": str}}

The component_inputs method returns a dictionary where each key is an input socket name while values are its type and optionality, and the component_outputs method similarly returns a dictionary mapping output socket names to their types.

How did you test it:

Unit tests have been added to test/component/test_component.py to verify that the component_inputs and component_outputs methods function as expected, providing the correct details for each socket.

Notes for reviewer:

I first added these methods to the component itself dynamically and then realized that outputs and inputs could be a field declared by the creator of the component class. This happened even in some of our test classes, causing massive test failures. I then realized it is better and safer to create class external methods - as described above. This approach is nicely symmetrical to find_pipeline_inputs and find_pipeline_outputs.

Copy link
Contributor

@ZanSara ZanSara left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is ready to go imho

@vblagoje
Copy link
Member Author

vblagoje commented Nov 9, 2023

Yeah, no rush for this one, let's have @silvanocerza give 👍 as well

@vblagoje
Copy link
Member Author

@silvanocerza ok with this one? It doesn't affect any ongoing work, let's clean up the PR pile

@vblagoje
Copy link
Member Author

There don't seem to be objections on this PR, I'll go ahead and merge it

@masci masci linked an issue Nov 14, 2023 that may be closed by this pull request
@vblagoje vblagoje merged commit dace4a3 into main Nov 14, 2023
@vblagoje vblagoje deleted the component_inputs_outputs branch November 14, 2023 20:50
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add function to list single component's inputs and outputs
2 participants