1
1
import logging
2
+ from typing import Type
2
3
3
4
from django .db import connections , models
4
5
from django .db .backends .signals import connection_created
@@ -66,10 +67,10 @@ def __new__(cls, name, bases, attrs):
66
67
# Use an EmptyManager for everything as types cannot be queried.
67
68
meta_obj .default_manager_name = "objects"
68
69
meta_obj .base_manager_name = "objects"
69
- attrs ["objects" ] = EmptyManager (model = None )
70
+ attrs ["objects" ] = EmptyManager (model = None ) # type: ignore
70
71
71
72
ret = super ().__new__ (cls , name , bases , attrs )
72
- ret .Field ._composite_type_model = ret
73
+ ret .Field ._composite_type_model = ret # type: ignore
73
74
return ret
74
75
75
76
def __init__ (cls , name , bases , attrs ):
@@ -147,16 +148,18 @@ class CompositeType(metaclass=CompositeTypeMeta):
147
148
148
149
# The database connection this type is registered with
149
150
registered_connection = None
151
+ _meta : Type
150
152
151
153
def __init__ (self , * args , ** kwargs ):
152
154
if args and kwargs :
153
155
raise RuntimeError ("Specify either args or kwargs but not both." )
154
156
155
- for field in self ._meta .fields :
157
+ fields = self .get_fields ()
158
+ for field in fields :
156
159
setattr (self , field .name , None )
157
160
158
161
# Unpack any args as if they came from the type
159
- for field , arg in zip (self . _meta . fields , args ):
162
+ for field , arg in zip (fields , args ):
160
163
setattr (self , field .name , arg )
161
164
162
165
for name , value in kwargs .items ():
@@ -169,21 +172,21 @@ def __repr__(self):
169
172
def __to_tuple__ (self ):
170
173
return tuple (
171
174
field .get_prep_value (getattr (self , field .name ))
172
- for field in self ._meta . fields
175
+ for field in self .get_fields ()
173
176
)
174
177
175
178
def __to_dict__ (self ):
176
179
return {
177
180
field .name : field .get_prep_value (getattr (self , field .name ))
178
- for field in self ._meta . fields
181
+ for field in self .get_fields ()
179
182
}
180
183
181
184
def __eq__ (self , other ):
182
185
if not isinstance (other , CompositeType ):
183
186
return False
184
187
if self ._meta .model != other ._meta .model :
185
188
return False
186
- for field in self ._meta . fields :
189
+ for field in self .get_fields () :
187
190
if getattr (self , field .name ) != getattr (other , field .name ):
188
191
return False
189
192
return True
@@ -235,3 +238,6 @@ def _get_next_or_previous_by_FIELD(self):
235
238
@classmethod
236
239
def check (cls , ** kwargs ):
237
240
return []
241
+
242
+ def get_fields (self ):
243
+ return self ._meta .fields
0 commit comments