Search code examples
python-3.xclasssubclassluigi

Is subclassing a Luigi task possible?


I am using the luigi workflow library. I would like to subclass its luigi.Task class to add a few more of my own methods and parameters.

The usual object pattern would be:

class MockClassA():
    pass

class MockClassB():
    def __init__(self, foo):
        super().__init__()
        self.foo = foo

>>> MockClassB(12).foo
<<< 12

However, when I do:

class Transform(luigi.Task):
    def __init__(self, foo):
        super().__init__()
        self.foo = foo

I get:

>>> Transform(12)
<<< [...] UnknownParameterException: Transform[args=(12,), kwargs={}]: takes at most 0 parameters (1 given)

Why does this occur?


Solution

  • The actual crash here is due to a metaclass running on instantiation.

    From reading the luigi docs it seems you are supposed to define parameters to your class as class attributes (luigi.Parameter) and let their metaclass machinery take care of the constructor.

    I believe your example would be written something like this:

    class Transform(luigi.Task):
        foo = luigi.Parameter()