@@ -457,11 +457,19 @@ def validate_list_or_none(option, value):
457457 return validate_list (option , value )
458458
459459
460+ def validate_list_or_mapping (option , value ):
461+ """Validates that 'value' is a list or a document."""
462+ if not isinstance (value , (abc .Mapping , list )):
463+ raise TypeError ("%s must either be a list or an instance of dict, "
464+ "bson.son.SON, or any other type that inherits from "
465+ "collections.Mapping" % (option ,))
466+
467+
460468def validate_is_mapping (option , value ):
461469 """Validate the type of method arguments that expect a document."""
462470 if not isinstance (value , abc .Mapping ):
463471 raise TypeError ("%s must be an instance of dict, bson.son.SON, or "
464- "other type that inherits from "
472+ "any other type that inherits from "
465473 "collections.Mapping" % (option ,))
466474
467475
@@ -515,12 +523,14 @@ def validate_ok_for_replace(replacement):
515523
516524def validate_ok_for_update (update ):
517525 """Validate an update document."""
518- validate_is_mapping ("update" , update )
519- # Update can not be {}
526+ validate_list_or_mapping ("update" , update )
527+ # Update cannot be {}.
520528 if not update :
521- raise ValueError ('update only works with $ operators' )
529+ raise ValueError ('update cannot be empty' )
530+
531+ is_document = not isinstance (update , list )
522532 first = next (iter (update ))
523- if not first .startswith ('$' ):
533+ if is_document and not first .startswith ('$' ):
524534 raise ValueError ('update only works with $ operators' )
525535
526536
0 commit comments