Typing in the future#

Currently Pypeworks takes a functional approach to hinting Node return values. As this approach is rigid and verbose, its use can be tedious. Therefore, Pypeworks will be introducing a new, more concise interface offering greater efficiency, as well as better integration with Python’s typing system. As the Python typing system is still maturing, Pypeworks will not yet default to the new approach, instead opting to support both approaches until further notice.

class pypeworks.typing.future.Args#

Utility class for type hinting the return values of a Node function. This class primarily serves to name return values, so that one node’s outputs may be mapped to another node’s inputs.

Consider for example the following example, wherein a node produces pairs of x and y coordinates in an incremental lineair fashion:

class Pipeline(Pipework):

    @Pipework.connect(input = "enter")
    def init(self, _) -> Args[:, Kwargs({"x": int, "y": int})]:

        for i in range(0, 100):
            yield i, i

    @Pipework.connect(input = "init")
    @Pipework.connect(output = "exit")
    def process(self, x : int, y : int):

        print(i, i)

The class may also be used standalone to construct a dictionary from a group of arguments:

Args[:, Kwargs({"x": int, "y": int})](123, 456).kwargs # {"x": 123, "y": 456}

Fundamentals#

__init__(*args, **kwargs)#
classmethod __class_getitem__(params)#

Args takes two generics:

  1. A type hint for the first value returned by a Node’s callable. When type hinted, this first value will be passed to subsequent nodes as a unnamed, positional argument (*args). If this is not desired, pass a sentinel type hint (:):

    node_1a = Node(lambda _: 123, returns = Args[int]),
    node_1b = Node(lambda _: 123, returns = Args[:, Kwargs[{"y": int}]]),
    
    node_2 = Node(lambda x = -1, y = -1: (x, y))
    # Returns (123, -1) for node_1a's outputs, and (-1, 123) for node_1b's.
    
  2. (optionally) Type hints for return values to be passed forward as keyword arguments. These must be specified using TypedDict or its Pypeworks specific alias, Kwargs. If you do not wish to pass along keyword arguments skip definition of the second generic.

Methods#

unpack() tuple[_Arg, _Kwargs]#

Unpack wrapped args and kwargs into a tuple.