What is the right way to convert between dataclasses in python that inherit from each other (without repetitive code)?
Example:
from dataclasses import dataclass
@dataclass
class Parent:
p1: int
# …
@dataclass
class Child(Parent):
c1: int
# …
My attempt:
# Fail: How to call the parent constructor (like in C++)?
def make_child(parent: Parent, c1: int, …) -> Child:
return Child(
p1=parent.p1,
# …
c1=c1,
# …
)
# Fail: How to implicitly cast to parent (like in C++)?
def get_parent(child: Child) -> Parent:
return Parent(
p1=child.p1,
# …
)
I know that this would be easy with composition instead of inheritance, but that's not my question.