I have 2 classes:
class A:
name = 'test'
def __init__(self):
pass
@staticmethod
def from_json(json: dict) -> object:
obj = A()
obj.name = json["name"]
return obj
class B(A):
description = "desc"
def __init__(self):
super().__init__(self) # I was originally doing: A.__init__(self) but online said to use super.
@staticnmethod
def from_json(json: dict) -> object:
obj = A.from_json(json) # As seen above, A.from_json, this returns an instance of A.
obj.description = json["description"]
return obj
I know there isnt really any casting, but I want the returned class to be of type B, so it gains all the other new properties / methods.
How to i have B::from_json return type B? I was thinking there was a way to create something like:
b = B()
and then through some python magic pass all properties from A into B and then return b
, but i wasnt sure if that is the right solution.
Here is now a functional test of the flaw:
x = A.from_json({'name': 'foo'})
z = B.from_json({ 'name': 'thor', 'description': 'god of thunder'})
type(x) == A # <class '__main__.A'>
type(z) == B # <class '__main__.A'>