@@ -123,6 +123,7 @@ def __getattr__(self, item):
123
123
124
124
125
125
_none = object ()
126
+ _none_as_none = object ()
126
127
127
128
128
129
def _get_column (model , column_or_name ) -> Column :
@@ -213,6 +214,8 @@ def __init__(self, model, *columns, **extras):
213
214
self .on_clause = None
214
215
215
216
def _do_load (self , row , none_as_none ):
217
+ # none_as_none indicates that in the case of every column of the object is
218
+ # None, whether a None or empty instance of the model should be returned.
216
219
values = dict ((c .name , row [c ]) for c in self .columns if c in row )
217
220
if none_as_none and all ((v is None ) for v in values .values ()):
218
221
return None
@@ -233,27 +236,29 @@ def do_load(self, row, context):
233
236
result is distinct.
234
237
"""
235
238
239
+ if context is None :
240
+ context = {}
236
241
distinct = True
237
242
if self ._distinct :
238
- if context is None :
239
- context = {}
240
243
ctx = context .setdefault (self ._distinct , {})
241
244
key = tuple (row [col ] for col in self ._distinct )
242
245
rv = ctx .get (key , _none )
243
246
if rv is _none :
244
- rv = self ._do_load (row , context .get ('none_as_none' , False ))
247
+ rv = self ._do_load (row , context .get (_none_as_none , False ))
245
248
ctx [key ] = rv
246
249
else :
247
250
distinct = False
248
251
else :
249
- rv = self ._do_load (row , context .get ('none_as_none' , False ))
252
+ rv = self ._do_load (row , context .get (_none_as_none , False ))
250
253
251
254
if rv is None :
252
255
return None , None
253
256
else :
254
257
for key , value in self .extras .items ():
255
- context .setdefault ('none_as_none' , True )
258
+ context .setdefault (_none_as_none , True )
256
259
value , distinct_ = value .do_load (row , context )
260
+ # _none_as_none should not be propagated to parents
261
+ context .pop (_none_as_none , 0 )
257
262
if distinct_ is None :
258
263
continue
259
264
0 commit comments