bonobo icon indicating copy to clipboard operation
bonobo copied to clipboard

A Method can provide a default in a Configurable child class but an Option cannot

Open levic opened this issue 7 years ago • 2 comments

  • Bonobo 0.6.2

  • Demonstration code:

#!/usr/bin/env python3
import bonobo


class MyTransform(bonobo.config.Configurable):
    option1 = bonobo.config.Method(positional=True, required=True)
    option2 = bonobo.config.Option(None, positional=True, default=100)

    def __call__(self, *values):
        pass


class ChildTransform(MyTransform):
    # I can set the default through the constructor
    def __init__(self, option2=20, *args, **kwargs):
        super().__init__(option2, *args, **kwargs)

    def option1(self):
        pass

    # this line causes a failure
    option2 = 99


if __name__ == '__main__':
    graph = bonobo.Graph()
    ct = ChildTransform()
    graph.add_chain(ct)

    parser = bonobo.get_argument_parser()
    with bonobo.parse_args(parser) as options:
        bonobo.run(
            graph,
            services={},
        )
  • A Method defined in a parent class can be implemented in a child class
  • An Option cannot
    • Even if you pass an option value in the constructor, the existence of that option in the child class declaration will cause an error
Traceback (most recent call last):
  File "test.py", line 27, in <module>
    ct = ChildTransform()
  File ".../.venv/lib/python3.6/site-packages/bonobo/config/configurables.py", line 144, in __new__
    missing.remove(name)
KeyError: 'option2'

(it's trying to remove option2 from a list of missing members but it was not in the list of missing members).

  • You can define a default value in a child class through overriding __init__ (see example).
  • It seems inconsistent that Method will happily read values from a child class but Option will not.
  • At minimum, the error message is confusing

levic avatar Jun 19 '18 03:06 levic

I cannot reproduce this error any more using the Demonstration code. I get the output:

  • ChildTransform in=1 [done]

sathish-t avatar Jun 02 '19 16:06 sathish-t

I'll keep the issue which is a good reminder that a lot of test cases are probably missing in this regard. We'll see what to do once the test cases exist.

hartym avatar Jun 02 '19 16:06 hartym