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

init=False and inheritance #336

Open
robertodr opened this issue Mar 28, 2023 · 0 comments
Open

init=False and inheritance #336

robertodr opened this issue Mar 28, 2023 · 0 comments
Assignees
Labels
bug Bug report or fix

Comments

@robertodr
Copy link

I think this is related to #333. Before #334, init=False fields would end up being passed to the __init__ method during deserialization. After #334, that's no longer the case, however, when this is mixed with inheritance I see the init=False fields pop up in the subclasses.

Here is an example:

from __future__ import annotations

from abc import ABC, abstractmethod
from dataclasses import dataclass, field

from serde import serde
from serde.json import from_json, to_json


@serde
@dataclass
class FooBase(ABC):
    i: int
    j: float = field(
        init=False,
        repr=False,
        compare=False,
    )

    def __post_init__(self) -> None:
        self.j = self.i * 10

    @abstractmethod
    def compute(self, k: float) -> list[float]:
        pass


@serde
@dataclass
class ConcreteA(FooBase):
    bar: list[float]
    foo: dict[str, float] = field(
        init=False,
        default_factory=lambda: {"a": 0.25, "b": 0.43},
    )

    def __post_init__(self) -> None:
        super().__post_init__()

    def compute(self, k: float) -> list[float]:
        return [x * k for x in self.bar]


foo = ConcreteA(
    i=10,
    bar=[
        1.0,
        2.0,
        0.5,
    ],
)
print(f"{to_json(foo)=}")

bar = from_json(ConcreteA, to_json(foo))
print(bar)

here the initialization of bar fails with the familiar:

Traceback (most recent call last):
  File "/workspaces/aurora/.venv/lib/python3.10/site-packages/serde/de.py", line 400, in from_obj
    return deserializable_to_obj(c)
  File "/workspaces/aurora/.venv/lib/python3.10/site-packages/serde/de.py", line 380, in deserializable_to_obj
    res = serde_scope.funcs[func_name](cls, maybe_generic=maybe_generic, data=o, reuse_instances=reuse_instances)
  File "<string>", line 29, in from_dict
serde.compat.UserError: ConcreteA.__init__() takes 3 positional arguments but 4 were given

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/workspaces/aurora/check.py", line 54, in <module>
    bar = from_json(ConcreteA, to_json(foo))
  File "/workspaces/aurora/.venv/lib/python3.10/site-packages/serde/json.py", line 77, in from_json
    return from_dict(c, de.deserialize(s, **opts), reuse_instances=False)
  File "/workspaces/aurora/.venv/lib/python3.10/site-packages/serde/de.py", line 486, in from_dict
    return from_obj(cls, o, named=True, reuse_instances=reuse_instances)
  File "/workspaces/aurora/.venv/lib/python3.10/site-packages/serde/de.py", line 460, in from_obj
    raise e.inner
  File "<string>", line 19, in from_dict
TypeError: ConcreteA.__init__() takes 3 positional arguments but 4 were given
@yukinarit yukinarit self-assigned this Apr 19, 2023
@yukinarit yukinarit added the bug Bug report or fix label Apr 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Bug report or fix
Projects
Status: No status
Development

No branches or pull requests

2 participants