audeering / audobject

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

Decoding non-keyword arguments fails in derived class #57

Closed frankenjoe closed 1 year ago

frankenjoe commented 1 year ago
class Base(audobject.Object):
    @audobject.init_decorator(
        resolvers={
            't': audobject.resolver.Tuple,
        }
    )
    def __init__(
            self,
            t: typing.Tuple,
    ):
        self.t = t

s = Base(t).to_yaml_s()
obj = audobject.from_yaml_s(s)
type(obj.t)
<class 'tuple'>

but:

class Child(Base):
    def __init__(
            self,
            t: typing.Tuple,
    ):
        super().__init__(t)

s = Child(t).to_yaml_s()
obj = audobject.from_yaml_s(s)
type(obj.t)
<class 'list'>

In the second case, t is not properly decoded to a tuple. This is because we only decode keyword arguments. Hence, it works if we call the constructor of the base class using keyword arguments, but it would be nicer if this was not necessary:

class Child(Base):
    def __init__(
            self,
            t: typing.Tuple,
    ):
        super().__init__(t=t)

s = Child(t).to_yaml_s()
obj = audobject.from_yaml_s(s)
type(obj.t)
<class 'tuple'>