Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failed to call super class __init__ from the subclass __init__ #307

Open
mokarrom opened this issue Aug 16, 2021 · 2 comments
Open

Failed to call super class __init__ from the subclass __init__ #307

mokarrom opened this issue Aug 16, 2021 · 2 comments

Comments

@mokarrom
Copy link

It looks like I can't override init method in a subclass and instantiate the subclass.
Let's assume, my base class, Data is defined as follows:

@dataclass_json(undefined=Undefined.EXCLUDE)
@dataclass
class Data:
    id: int
    text: str

And ScoredData class inherits Data class as follows:

@dataclass_json(undefined=Undefined.EXCLUDE)
@dataclass
class ScoredData(Data):
    score: float

    def __init__(self, data: Data, score: float):
        super().__init__(data.id, data.text)
        self.score = score

Now, if I try to instantiate ScoredData as follows:

my_data = Data(1, "cse")
scored_data = ScoredData(data=my_data, score=0.5)
print(str(scored_data))

it complains TypeError: __init__() missing 1 required positional argument: 'data'
Interestingly, if I remove undefined=Undefined.EXCLUDE, then it seems working :)

@mokarrom
Copy link
Author

mokarrom commented Aug 17, 2021

Without undefined=Undefined.EXCLUDE it is working but fails following:

scored_data_str = ScoredData.schema().dumps(scored_data)
saved_score_data = ScoredData.schema().loads(scored_data_str)

assert scored_data == saved_score_data

It throws TypeError: __init__() got an unexpected keyword argument 'id'

@george-zubrienko
Copy link
Collaborator

Underlined in #442 as one of the targets to take down (user code mutation) - should be fixed in v1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants