audeering / audobject

Generic Python interface for serializing objects to YAML
https://audeering.github.io/audobject/
Other
1 stars 0 forks source link

Default arguments are not preserved with serialized functions #24

Closed frankenjoe closed 2 years ago

frankenjoe commented 2 years ago

The following is working as expected:

class MyObjectWithFunction(audobject.Object):

    @audobject.init_decorator(
        resolvers={
            'func': audobject.resolver.Function,
        }
    )
    def __init__(
            self,
            func: typing.Callable,
    ):
        self.func = func

    def __call__(self, *args, **kwargs):
        return self.func(*args, **kwargs)

def add(a, b=1):
    return a + b

o = MyObjectWithFunction(add)
o(1)  # uses b=1
2

But after serialization the default argument for b is lost:

o_yaml = o.to_yaml_s()
o2 = audobject.from_yaml_s(o_yaml)
o2(1)
TypeError: add() missing 1 required positional argument: 'b'