@@ -330,11 +330,21 @@ def _set_new_value(self, parent, parent_to_obj_elem, parent_to_obj_action,
330330 Set the element value on an object and if necessary convert the object to the proper mutable type
331331 """
332332 if isinstance (obj , tuple ):
333- # convert this object back to a tuple later
334- obj = self ._coerce_obj (
335- parent , obj , path , parent_to_obj_elem ,
336- parent_to_obj_action , elements ,
337- to_type = list , from_type = tuple )
333+ # Check if it's a NamedTuple and use _replace() to generate a new copy with the change
334+ if hasattr (obj , '_fields' ) and hasattr (obj , '_replace' ):
335+ if action == GETATTR :
336+ obj = obj ._replace (** {elem : new_value })
337+ if parent :
338+ self ._simple_set_elem_value (obj = parent , path_for_err_reporting = path ,
339+ elem = parent_to_obj_elem , value = obj ,
340+ action = parent_to_obj_action )
341+ return
342+ else :
343+ # Regular tuple - convert this object back to a tuple later
344+ obj = self ._coerce_obj (
345+ parent , obj , path , parent_to_obj_elem ,
346+ parent_to_obj_action , elements ,
347+ to_type = list , from_type = tuple )
338348 if elem != 0 and self .force and isinstance (obj , list ) and len (obj ) == 0 :
339349 # it must have been a dictionary
340350 obj = {}
0 commit comments