I have Python data class created from JSON (quite a lot of them actually). I want to have a method for creating class instance from JSON.
I have something like this:
class FromJSONMixin:
@staticmethod
@abstractmethod
def from_json(json: Union[Dict, TypedDict], **kwargs):
raise NotImplementedError
class PatientJSON(TypedDict):
ID: str
Name: str
Description: str
BirthDate: str
@dataclass
class Patient(FromJSONMixin):
name: str
birth_date: str
description: str
@staticmethod
def from_json(json: PatientJSON, **kwargs) -> Patient:
return Patient(
name=json["Name"],
birth_date=json["BirthDate"],
description=raw_data["Description"])
I want to create Patient
objects from PatientJSON
(the structure is related to the existing database, I have to integrate with it; it also does a few name-attribute translations, as you can see above). I created the FromJSONMixin
to explicitly mark classes that can be created from related classes for JSONs (like PatientJSON
).
Question: I get an error with the -> Patient:
part, Unresolved reference 'Patient'
. Why? I can't type class objects in methods in the same class? Do I have to just give up on typing the return type?