10
10
11
11
12
12
class NotADataClassInstance (Exception ):
13
- """ Raised when trying to serialize a class without the __dataclass_fields__ attribute """
13
+ """
14
+ Raised when trying to serialize a
15
+ class without the __dataclass_fields__ attribute
16
+ """
14
17
pass
15
18
16
19
@@ -20,11 +23,13 @@ def to_dict(self):
20
23
""" Class function allowing conversion of an object to a dictionary"""
21
24
if hasattr (self , '__serialize__' ):
22
25
return self .__serialize__ ()
23
- raise NotSerializableInstance (f"Could not serialize { type (self )} object." )
26
+ raise NotSerializableInstance (
27
+ f"Could not serialize { type (self )} object."
28
+ )
24
29
25
30
@abc .abstractmethod
26
31
def __serialize__ (self ):
27
- raise NotImplemented ("abstract method" )
32
+ raise NotImplementedError ("abstract method" )
28
33
29
34
30
35
class _SerializableDataclassMixin (_Serializable ):
@@ -36,7 +41,8 @@ def __serialize__(self):
36
41
for name , field in self .__dataclass_fields__ .items ():
37
42
result [name ] = serializer (getattr (self , name ), name , field )
38
43
return result
39
- raise NotADataClassInstance (f"Could not infer dataclass fields from { type (self )} object." )
44
+ raise NotADataClassInstance (
45
+ f"Could not infer dataclass fields from { type (self )} object." )
40
46
41
47
@classmethod
42
48
def mixin (cls , klass , * args , ** kwargs ):
@@ -47,7 +53,10 @@ def mixin(cls, klass, *args, **kwargs):
47
53
48
54
49
55
def serializable (_cls = None , * args , ** kwargs ):
50
- """ Wrapper to transform class into serializable (Note: extends dataclass) """
56
+ """
57
+ Wrapper to transform class into serializable
58
+ (Note: extends dataclass)
59
+ """
51
60
52
61
def wrap (cls ):
53
62
return _SerializableDataclassMixin .mixin (klass = cls , * args , ** kwargs )
0 commit comments