11from __future__ import annotations
2+ import builtins
23from copy import deepcopy
34from dataclasses import dataclass , field
45import datetime
89import typing as t
910import uuid
1011
12+ from piccolo .columns import Column
1113from piccolo .columns .defaults .base import Default
1214from piccolo .columns .reference import LazyTableReference
1315from piccolo .table import Table
1719
1820
1921@dataclass
20- class SerialisedClass :
22+ class SerialisedBuiltin :
23+ builtin : t .Any
24+
25+ def __hash__ (self ):
26+ return hash (self .builtin .__name__ )
27+
28+ def __eq__ (self , other ):
29+ return self .__hash__ () == other .__hash__ ()
30+
31+ def __repr__ (self ):
32+ return self .builtin .__name__
33+
34+
35+ @dataclass
36+ class SerialisedClassInstance :
2137 instance : object
2238
2339 def __hash__ (self ):
@@ -26,19 +42,37 @@ def __hash__(self):
2642 def __eq__ (self , other ):
2743 return self .__hash__ () == other .__hash__ ()
2844
29- def _serialise_value (self , value ):
30- return f"'{ value } '" if isinstance (value , str ) else value
31-
3245 def __repr__ (self ):
3346 args = ", " .join (
3447 [
35- f"{ key } ={ self . _serialise_value ( value )} "
48+ f"{ key } ={ value . __repr__ ( )} "
3649 for key , value in self .instance .__dict__ .items ()
3750 ]
3851 )
3952 return f"{ self .instance .__class__ .__name__ } ({ args } )"
4053
4154
55+ @dataclass
56+ class SerialisedColumnInstance :
57+ instance : Column
58+ serialised_params : SerialisedParams
59+
60+ def __hash__ (self ):
61+ return self .instance .__hash__ ()
62+
63+ def __eq__ (self , other ):
64+ return self .__hash__ () == other .__hash__ ()
65+
66+ def __repr__ (self ):
67+ args = ", " .join (
68+ [
69+ f"{ key } ={ self .serialised_params .params .get (key ).__repr__ ()} " # noqa: E501
70+ for key in self .instance ._meta .params .keys ()
71+ ]
72+ )
73+ return f"{ self .instance .__class__ .__name__ } ({ args } )"
74+
75+
4276@dataclass
4377class SerialisedTableType :
4478 table_type : t .Type [Table ]
@@ -127,9 +161,40 @@ def serialise_params(params: t.Dict[str, t.Any]) -> SerialisedParams:
127161
128162 for key , value in params .items ():
129163
164+ # Builtins, such as str, list and dict.
165+ if (
166+ hasattr (value , "__module__" )
167+ and value .__module__ == builtins .__name__
168+ ):
169+ params [key ] = SerialisedBuiltin (builtin = value )
170+ continue
171+
172+ # Column instances, which are used by Array definitions.
173+ if isinstance (value , Column ):
174+ column : Column = value
175+ serialised_params : SerialisedParams = serialise_params (
176+ params = column ._meta .params
177+ )
178+
179+ # Include the extra imports and definitions required for the
180+ # column params.
181+ extra_imports .extend (serialised_params .extra_imports )
182+ extra_definitions .extend (serialised_params .extra_definitions )
183+
184+ extra_imports .append (
185+ Import (
186+ module = column .__class__ .__module__ ,
187+ target = column .__class__ .__name__ ,
188+ )
189+ )
190+ params [key ] = SerialisedColumnInstance (
191+ instance = value , serialised_params = serialised_params
192+ )
193+ continue
194+
130195 # Class instances
131196 if isinstance (value , Default ):
132- params [key ] = SerialisedClass (instance = value )
197+ params [key ] = SerialisedClassInstance (instance = value )
133198 extra_imports .append (
134199 Import (
135200 module = value .__class__ .__module__ ,
@@ -227,7 +292,7 @@ def deserialise_params(params: t.Dict[str, t.Any]) -> t.Dict[str, t.Any]:
227292 if isinstance (value , str ) and not isinstance (value , Enum ):
228293 if value != "self" :
229294 params [key ] = deserialise_legacy_params (name = key , value = value )
230- elif isinstance (value , SerialisedClass ):
295+ elif isinstance (value , SerialisedClassInstance ):
231296 params [key ] = value .instance
232297 elif isinstance (value , SerialisedUUID ):
233298 params [key ] = value .instance
0 commit comments