i2mint / know

Funnel live streams of data into storage and other processes
Apache License 2.0
0 stars 0 forks source link

SlabsIter has weak error messages #6

Closed andeaseme closed 1 year ago

andeaseme commented 1 year ago

SlabsIter will throw errors related to code deep in its own functions without mentioning what inputs could be wrong. Debugging is hard for new users as shown by the following example.

I've written some code and expect it to print the contents

def item_generator():
    for i in range(3):
        yield {'index': i}

def print_item(item):
    print(item)

with SlabsIter(item=item_generator(), print_item=print_item) as sit:
    for _ in sit:
        pass

But instead I'm met with an error

Traceback (most recent call last):
  File "/Users/Andie.Phan/Projects/otosense/plunk/plunk/ap/pain/context_func.py", line 105, in <module>
    _weak_error_message()
  File "/Users/Andie.Phan/Projects/otosense/plunk/plunk/ap/pain/context_func.py", line 96, in _weak_error_message
    for _ in sit:
  File "/Users/Andie.Phan/Projects/otosense/know/know/base.py", line 341, in __iter__
    yield next(self)
  File "/Users/Andie.Phan/Projects/otosense/know/know/base.py", line 334, in __next__
    return self._call_on_scope(scope={})
  File "/Users/Andie.Phan/Projects/otosense/know/know/base.py", line 326, in _call_on_scope
    scope[name] = _call_from_dict(scope, component, self.sigs[name])
  File "/Users/Andie.Phan/Projects/otosense/know/know/base.py", line 164, in _call_from_dict
    args, kwargs = sig.args_and_kwargs_from_kwargs(
AttributeError: 'NoneType' object has no attribute 'args_and_kwargs_from_kwargs'

The problem has something to do with None object but it doesn't tell me what's wrong or where to look. There is really no hints at all.

andeaseme commented 1 year ago

Not a solution to the error message issue.

The way to fix the code in the example is to wrap item_generator.

with SlabsIter(item=iter(item_generator()).__next__, print_item=print_item) as sit:
    for _ in sit:
        pass
thorwhalen commented 1 year ago

I added a validation step in the SlabIter init so that you'll now get the precise error message

TypeError: This component is not callable: item

before you go down the layers to fail in some obscure place that has little resemblance to your context.

There was, in fact, a simple assertion in there already, but for some reason it was commented out. I hope it wasn't a good reason, because now we have validation again.

But I profited of the occasion to do a few things: