Skip to content

Commit e24c6a8

Browse files
committed
better multi prop
1 parent 7cee054 commit e24c6a8

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

gel/_internal/_qbmodel/_pydantic/_fields.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -285,16 +285,25 @@ def _validate(
285285
):
286286
lt: type[_tracked_list.DowncastingTrackedList[_ST_co, _BT_co]] = (
287287
_tracked_list.DowncastingTrackedList[
288-
generic_args[0],
289-
generic_args[1],
288+
generic_args[0], # type: ignore [valid-type]
289+
generic_args[1], # type: ignore [valid-type]
290290
]
291291
)
292-
if isinstance(value, _tracked_list.DefaultList):
293-
# Convert the pydantic generated default into
294-
# the internal DEFAULT_VALUE
295-
return _abstract.DEFAULT_VALUE
296292

297-
return LinkSet.__gel_validate__(lt, value)
293+
# If the value wasn't explicitly provided by a user, pydantic will
294+
# generate a DefaultList. __gel_validate__ will then create the
295+
# appropriate tracked list.
296+
lst = LinkSet.__gel_validate__(lt, value)
297+
298+
if isinstance(value, _tracked_list.DefaultList):
299+
# GelSourceModel will do its best to set tracking flags, but set
300+
# the here just in case.
301+
#
302+
# eg. __init__ will call this
303+
lst._mode = _tracked_list.Mode.Write
304+
lst.__gel_overwrite_data__ = False
305+
306+
return lst
298307

299308

300309
class _ComputedMultiProperty(

gel/_internal/_save.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,9 @@
4040
from gel._internal._tracked_list import (
4141
AbstractCollection,
4242
AbstractTrackedList,
43-
TrackedList,
4443
DowncastingTrackedList,
44+
Mode,
45+
TrackedList,
4546
)
4647
from gel._internal._edgeql import PointerKind, quote_ident
4748

@@ -822,7 +823,21 @@ def make_plan(
822823
(mp_added := val.__gel_get_added__())
823824
# Even if there are no added values, push the change if
824825
# there is a default value we want to override.
825-
or (is_new and prop.has_default)
826+
or (
827+
is_new
828+
and prop.has_default
829+
and not (
830+
# Pydantic should ensure unset tracked lists will
831+
# have these flags.
832+
# We do this in:
833+
# - GelSourceModel.model_construct
834+
# - GelSourceModel.__getattr__
835+
# - GelSourceModel.__init__
836+
# - via _MultiProperty._validate
837+
val._mode == Mode.Write
838+
and not val.__gel_overwrite_data__
839+
)
840+
)
826841
):
827842
push_change(
828843
requireds,

0 commit comments

Comments
 (0)