@@ -309,7 +309,7 @@ def set(
309309) -> MutableMapping :
310310 """
311311 Set the value in obj at the place indicated by segments. If creator is not
312- None (default __default_creator__ ), then call the creator function to
312+ None (default _default_creator ), then call the creator function to
313313 create any missing path components.
314314
315315 set(obj, segments, value) -> obj
@@ -320,13 +320,18 @@ def set(
320320 # For everything except the last value, walk down the path and
321321 # create if creator is set.
322322 for (i , segment ) in enumerate (segments [:- 1 ]):
323+
324+ # If segment is non-int but supposed to be a sequence index
325+ if isinstance (segment , str ) and isinstance (current , Sequence ) and segment .isdigit ():
326+ segment = int (segment )
327+
323328 try :
324329 # Optimistically try to get the next value. This makes the
325330 # code agnostic to whether current is a list or a dict.
326331 # Unfortunately, for our use, 'x in thing' for lists checks
327332 # values, not keys whereas dicts check keys.
328333 current [segment ]
329- except ( KeyError , IndexError ) :
334+ except :
330335 if creator is not None :
331336 creator (current , segments , i , hints )
332337 else :
@@ -336,10 +341,16 @@ def set(
336341 if i != length - 1 and leaf (current ):
337342 raise PathNotFound (f"Path: { segments } [{ i } ]" )
338343
339- if isinstance (segments [- 1 ], int ):
340- extend (current , segments [- 1 ])
344+ last_segment = segments [- 1 ]
345+
346+ # Resolve ambiguity of last segment
347+ if isinstance (last_segment , str ) and isinstance (current , Sequence ) and last_segment .isdigit ():
348+ last_segment = int (last_segment )
341349
342- current [segments [- 1 ]] = value
350+ if isinstance (last_segment , int ):
351+ extend (current , last_segment )
352+
353+ current [last_segment ] = value
343354
344355 return obj
345356
@@ -388,9 +399,11 @@ def view(obj, glob):
388399
389400 view(obj, glob) -> obj'
390401 """
402+
391403 def f (obj , pair , result ):
392404 (segments , value ) = pair
393405 if match (segments , glob ):
394406 if not has (result , segments ):
395407 set (result , segments , deepcopy (value ), hints = types (obj , segments ))
408+
396409 return fold (obj , f , type (obj )())
0 commit comments