smarie/python-autoclass

Inheritance of @autoclass tagged classes (@autodict inheritance issues)

smarie opened this issue · 1 comments

As of today inheritance is not intuitive:

from autoclass import autoclass

@autoclass
class Foo:
    def __init__(self, foo):
        pass

@autoclass
class Bar(Foo):
    def __init__(self, bar):
        pass

yields

> ValueError: @autodict can not be set on classes that are already subclasses of Mapping, and therefore already behave like dict

In order for this to work, you have to use @autoclass(autodict=False) in the parent class. But this is not user-friendly. The proposal would be to remove this exception, and make all dict implemented methods call the corresponding super() method where appropriate

For the record, the correct way to write the above example is:

from autoclass import autoclass

@autoclass
class Foo:
    def __init__(self, foo):
        pass

@autoclass
class Bar(Foo):
    def __init__(self, bar, foo):
        super(Bar, self).__init__(foo)  # note that the constructor is optional in this case.