@@ -119,7 +119,7 @@ def __init__(
119119 self .start_value : Any = initial_value
120120 """Value of last_value at the beginning of current pipeline run"""
121121 self .resource_name : Optional [str ] = None
122- self .primary_key : Optional [TTableHintTemplate [TColumnNames ]] = primary_key
122+ self ._primary_key : Optional [TTableHintTemplate [TColumnNames ]] = primary_key
123123 self .allow_external_schedulers = allow_external_schedulers
124124
125125 self ._cached_state : IncrementalColumnState = None
@@ -133,6 +133,18 @@ def __init__(
133133
134134 self ._transformers : Dict [str , IncrementalTransform ] = {}
135135
136+ @property
137+ def primary_key (self ) -> Optional [TTableHintTemplate [TColumnNames ]]:
138+ return self ._primary_key
139+
140+ @primary_key .setter
141+ def primary_key (self , value : str ) -> None :
142+ # set key in incremental and data type transformers
143+ self ._primary_key = value
144+ if self ._transformers :
145+ for transform in self ._transformers .values ():
146+ transform .primary_key = value
147+
136148 def _make_transforms (self ) -> None :
137149 types = [("arrow" , ArrowIncremental ), ("json" , JsonIncremental )]
138150 for dt , kls in types :
@@ -143,7 +155,7 @@ def _make_transforms(self) -> None:
143155 self .end_value ,
144156 self ._cached_state ,
145157 self .last_value_func ,
146- self .primary_key ,
158+ self ._primary_key ,
147159 )
148160
149161 @classmethod
@@ -163,7 +175,7 @@ def copy(self) -> "Incremental[TCursorValue]":
163175 self .cursor_path ,
164176 initial_value = self .initial_value ,
165177 last_value_func = self .last_value_func ,
166- primary_key = self .primary_key ,
178+ primary_key = self ._primary_key ,
167179 end_value = self .end_value ,
168180 allow_external_schedulers = self .allow_external_schedulers ,
169181 )
@@ -178,7 +190,7 @@ def merge(self, other: "Incremental[TCursorValue]") -> "Incremental[TCursorValue
178190 >>>
179191 >>> my_resource(updated=incremental(initial_value='2023-01-01', end_value='2023-02-01'))
180192 """
181- kwargs = dict (self , last_value_func = self .last_value_func , primary_key = self .primary_key )
193+ kwargs = dict (self , last_value_func = self .last_value_func , primary_key = self ._primary_key )
182194 for key , value in dict (
183195 other , last_value_func = other .last_value_func , primary_key = other .primary_key
184196 ).items ():
@@ -395,7 +407,6 @@ def __call__(self, rows: TDataItems, meta: Any = None) -> Optional[TDataItems]:
395407 return rows
396408
397409 transformer = self ._get_transformer (rows )
398- transformer .primary_key = self .primary_key
399410
400411 if isinstance (rows , list ):
401412 return [
@@ -476,7 +487,7 @@ def _wrap(*args: Any, **kwargs: Any) -> Any:
476487 elif isinstance (p .default , Incremental ):
477488 new_incremental = p .default .copy ()
478489
479- if not new_incremental or new_incremental .is_partial ():
490+ if ( not new_incremental or new_incremental .is_partial ()) and not self . _incremental :
480491 if is_optional_type (p .annotation ):
481492 bound_args .arguments [p .name ] = None # Remove partial spec
482493 return func (* bound_args .args , ** bound_args .kwargs )
@@ -486,15 +497,16 @@ def _wrap(*args: Any, **kwargs: Any) -> Any:
486497 )
487498 # pass Generic information from annotation to new_incremental
488499 if (
489- not hasattr (new_incremental , "__orig_class__" )
500+ new_incremental
501+ and not hasattr (new_incremental , "__orig_class__" )
490502 and p .annotation
491503 and get_args (p .annotation )
492504 ):
493505 new_incremental .__orig_class__ = p .annotation # type: ignore
494506
495507 # set the incremental only if not yet set or if it was passed explicitly
496508 # NOTE: the _incremental may be also set by applying hints to the resource see `set_template` in `DltResource`
497- if p .name in bound_args .arguments or not self ._incremental :
509+ if ( new_incremental and p .name in bound_args .arguments ) or not self ._incremental :
498510 self ._incremental = new_incremental
499511 self ._incremental .resolve ()
500512 # in case of transformers the bind will be called before this wrapper is set: because transformer is called for a first time late in the pipe
@@ -531,6 +543,9 @@ def __call__(self, item: TDataItems, meta: Any = None) -> Optional[TDataItems]:
531543 return item
532544 if self ._incremental .primary_key is None :
533545 self ._incremental .primary_key = self .primary_key
546+ elif self .primary_key is None :
547+ # propagate from incremental
548+ self .primary_key = self ._incremental .primary_key
534549 return self ._incremental (item , meta )
535550
536551
0 commit comments