@@ -288,7 +288,7 @@ def __new__(
288
288
strict_uuid : Optional [bool ] = None ,
289
289
json_mode : int = JSONMode .RELAXED ,
290
290
* args : Any ,
291
- ** kwargs : Any
291
+ ** kwargs : Any ,
292
292
) -> "JSONOptions" :
293
293
kwargs ["tz_aware" ] = kwargs .get ("tz_aware" , False )
294
294
if kwargs ["tz_aware" ]:
@@ -303,7 +303,7 @@ def __new__(
303
303
"JSONOptions.datetime_representation must be one of LEGACY, "
304
304
"NUMBERLONG, or ISO8601 from DatetimeRepresentation."
305
305
)
306
- self = cast (JSONOptions , super (JSONOptions , cls ).__new__ (cls , * args , ** kwargs ))
306
+ self = cast (JSONOptions , super ().__new__ (cls , * args , ** kwargs ))
307
307
if json_mode not in (JSONMode .LEGACY , JSONMode .RELAXED , JSONMode .CANONICAL ):
308
308
raise ValueError (
309
309
"JSONOptions.json_mode must be one of LEGACY, RELAXED, "
@@ -358,13 +358,13 @@ def _arguments_repr(self) -> str:
358
358
self .datetime_representation ,
359
359
self .strict_uuid ,
360
360
self .json_mode ,
361
- super (JSONOptions , self )._arguments_repr (),
361
+ super ()._arguments_repr (),
362
362
)
363
363
)
364
364
365
365
def _options_dict (self ) -> Dict [Any , Any ]:
366
366
# TODO: PYTHON-2442 use _asdict() instead
367
- options_dict = super (JSONOptions , self )._options_dict ()
367
+ options_dict = super ()._options_dict ()
368
368
options_dict .update (
369
369
{
370
370
"strict_number_long" : self .strict_number_long ,
@@ -492,7 +492,7 @@ def _json_convert(obj: Any, json_options: JSONOptions = DEFAULT_JSON_OPTIONS) ->
492
492
if hasattr (obj , "items" ):
493
493
return SON (((k , _json_convert (v , json_options )) for k , v in obj .items ()))
494
494
elif hasattr (obj , "__iter__" ) and not isinstance (obj , (str , bytes )):
495
- return list (( _json_convert (v , json_options ) for v in obj ) )
495
+ return list (_json_convert (v , json_options ) for v in obj )
496
496
try :
497
497
return default (obj , json_options )
498
498
except TypeError :
@@ -568,9 +568,9 @@ def _parse_legacy_regex(doc: Any) -> Any:
568
568
def _parse_legacy_uuid (doc : Any , json_options : JSONOptions ) -> Union [Binary , uuid .UUID ]:
569
569
"""Decode a JSON legacy $uuid to Python UUID."""
570
570
if len (doc ) != 1 :
571
- raise TypeError ("Bad $uuid, extra field(s): %s" % ( doc ,) )
571
+ raise TypeError (f "Bad $uuid, extra field(s): { doc } " )
572
572
if not isinstance (doc ["$uuid" ], str ):
573
- raise TypeError ("$uuid must be a string: %s" % ( doc ,) )
573
+ raise TypeError (f "$uuid must be a string: { doc } " )
574
574
if json_options .uuid_representation == UuidRepresentation .UNSPECIFIED :
575
575
return Binary .from_uuid (uuid .UUID (doc ["$uuid" ]))
576
576
else :
@@ -613,11 +613,11 @@ def _parse_canonical_binary(doc: Any, json_options: JSONOptions) -> Union[Binary
613
613
b64 = binary ["base64" ]
614
614
subtype = binary ["subType" ]
615
615
if not isinstance (b64 , str ):
616
- raise TypeError ("$binary base64 must be a string: %s" % ( doc ,) )
616
+ raise TypeError (f "$binary base64 must be a string: { doc } " )
617
617
if not isinstance (subtype , str ) or len (subtype ) > 2 :
618
- raise TypeError ("$binary subType must be a string at most 2 characters: %s" % ( doc ,) )
618
+ raise TypeError (f "$binary subType must be a string at most 2 characters: { doc } " )
619
619
if len (binary ) != 2 :
620
- raise TypeError ('$binary must include only "base64" and "subType" components: %s' % ( doc ,) )
620
+ raise TypeError (f '$binary must include only "base64" and "subType" components: { doc } ' )
621
621
622
622
data = base64 .b64decode (b64 .encode ())
623
623
return _binary_or_uuid (data , int (subtype , 16 ), json_options )
@@ -629,7 +629,7 @@ def _parse_canonical_datetime(
629
629
"""Decode a JSON datetime to python datetime.datetime."""
630
630
dtm = doc ["$date" ]
631
631
if len (doc ) != 1 :
632
- raise TypeError ("Bad $date, extra field(s): %s" % ( doc ,) )
632
+ raise TypeError (f "Bad $date, extra field(s): { doc } " )
633
633
# mongoexport 2.6 and newer
634
634
if isinstance (dtm , str ):
635
635
# Parse offset
@@ -692,31 +692,31 @@ def _parse_canonical_datetime(
692
692
def _parse_canonical_oid (doc : Any ) -> ObjectId :
693
693
"""Decode a JSON ObjectId to bson.objectid.ObjectId."""
694
694
if len (doc ) != 1 :
695
- raise TypeError ("Bad $oid, extra field(s): %s" % ( doc ,) )
695
+ raise TypeError (f "Bad $oid, extra field(s): { doc } " )
696
696
return ObjectId (doc ["$oid" ])
697
697
698
698
699
699
def _parse_canonical_symbol (doc : Any ) -> str :
700
700
"""Decode a JSON symbol to Python string."""
701
701
symbol = doc ["$symbol" ]
702
702
if len (doc ) != 1 :
703
- raise TypeError ("Bad $symbol, extra field(s): %s" % ( doc ,) )
703
+ raise TypeError (f "Bad $symbol, extra field(s): { doc } " )
704
704
return str (symbol )
705
705
706
706
707
707
def _parse_canonical_code (doc : Any ) -> Code :
708
708
"""Decode a JSON code to bson.code.Code."""
709
709
for key in doc :
710
710
if key not in ("$code" , "$scope" ):
711
- raise TypeError ("Bad $code, extra field(s): %s" % ( doc ,) )
711
+ raise TypeError (f "Bad $code, extra field(s): { doc } " )
712
712
return Code (doc ["$code" ], scope = doc .get ("$scope" ))
713
713
714
714
715
715
def _parse_canonical_regex (doc : Any ) -> Regex :
716
716
"""Decode a JSON regex to bson.regex.Regex."""
717
717
regex = doc ["$regularExpression" ]
718
718
if len (doc ) != 1 :
719
- raise TypeError ("Bad $regularExpression, extra field(s): %s" % ( doc ,) )
719
+ raise TypeError (f "Bad $regularExpression, extra field(s): { doc } " )
720
720
if len (regex ) != 2 :
721
721
raise TypeError (
722
722
'Bad $regularExpression must include only "pattern"'
@@ -739,65 +739,65 @@ def _parse_canonical_dbpointer(doc: Any) -> Any:
739
739
"""Decode a JSON (deprecated) DBPointer to bson.dbref.DBRef."""
740
740
dbref = doc ["$dbPointer" ]
741
741
if len (doc ) != 1 :
742
- raise TypeError ("Bad $dbPointer, extra field(s): %s" % ( doc ,) )
742
+ raise TypeError (f "Bad $dbPointer, extra field(s): { doc } " )
743
743
if isinstance (dbref , DBRef ):
744
744
dbref_doc = dbref .as_doc ()
745
745
# DBPointer must not contain $db in its value.
746
746
if dbref .database is not None :
747
- raise TypeError ("Bad $dbPointer, extra field $db: %s" % ( dbref_doc ,) )
747
+ raise TypeError (f "Bad $dbPointer, extra field $db: { dbref_doc } " )
748
748
if not isinstance (dbref .id , ObjectId ):
749
- raise TypeError ("Bad $dbPointer, $id must be an ObjectId: %s" % ( dbref_doc ,) )
749
+ raise TypeError (f "Bad $dbPointer, $id must be an ObjectId: { dbref_doc } " )
750
750
if len (dbref_doc ) != 2 :
751
- raise TypeError ("Bad $dbPointer, extra field(s) in DBRef: %s" % ( dbref_doc ,) )
751
+ raise TypeError (f "Bad $dbPointer, extra field(s) in DBRef: { dbref_doc } " )
752
752
return dbref
753
753
else :
754
- raise TypeError ("Bad $dbPointer, expected a DBRef: %s" % ( doc ,) )
754
+ raise TypeError (f "Bad $dbPointer, expected a DBRef: { doc } " )
755
755
756
756
757
757
def _parse_canonical_int32 (doc : Any ) -> int :
758
758
"""Decode a JSON int32 to python int."""
759
759
i_str = doc ["$numberInt" ]
760
760
if len (doc ) != 1 :
761
- raise TypeError ("Bad $numberInt, extra field(s): %s" % ( doc ,) )
761
+ raise TypeError (f "Bad $numberInt, extra field(s): { doc } " )
762
762
if not isinstance (i_str , str ):
763
- raise TypeError ("$numberInt must be string: %s" % ( doc ,) )
763
+ raise TypeError (f "$numberInt must be string: { doc } " )
764
764
return int (i_str )
765
765
766
766
767
767
def _parse_canonical_int64 (doc : Any ) -> Int64 :
768
768
"""Decode a JSON int64 to bson.int64.Int64."""
769
769
l_str = doc ["$numberLong" ]
770
770
if len (doc ) != 1 :
771
- raise TypeError ("Bad $numberLong, extra field(s): %s" % ( doc ,) )
771
+ raise TypeError (f "Bad $numberLong, extra field(s): { doc } " )
772
772
return Int64 (l_str )
773
773
774
774
775
775
def _parse_canonical_double (doc : Any ) -> float :
776
776
"""Decode a JSON double to python float."""
777
777
d_str = doc ["$numberDouble" ]
778
778
if len (doc ) != 1 :
779
- raise TypeError ("Bad $numberDouble, extra field(s): %s" % ( doc ,) )
779
+ raise TypeError (f "Bad $numberDouble, extra field(s): { doc } " )
780
780
if not isinstance (d_str , str ):
781
- raise TypeError ("$numberDouble must be string: %s" % ( doc ,) )
781
+ raise TypeError (f "$numberDouble must be string: { doc } " )
782
782
return float (d_str )
783
783
784
784
785
785
def _parse_canonical_decimal128 (doc : Any ) -> Decimal128 :
786
786
"""Decode a JSON decimal128 to bson.decimal128.Decimal128."""
787
787
d_str = doc ["$numberDecimal" ]
788
788
if len (doc ) != 1 :
789
- raise TypeError ("Bad $numberDecimal, extra field(s): %s" % ( doc ,) )
789
+ raise TypeError (f "Bad $numberDecimal, extra field(s): { doc } " )
790
790
if not isinstance (d_str , str ):
791
- raise TypeError ("$numberDecimal must be string: %s" % ( doc ,) )
791
+ raise TypeError (f "$numberDecimal must be string: { doc } " )
792
792
return Decimal128 (d_str )
793
793
794
794
795
795
def _parse_canonical_minkey (doc : Any ) -> MinKey :
796
796
"""Decode a JSON MinKey to bson.min_key.MinKey."""
797
797
if type (doc ["$minKey" ]) is not int or doc ["$minKey" ] != 1 :
798
- raise TypeError ("$minKey value must be 1: %s" % ( doc ,) )
798
+ raise TypeError (f "$minKey value must be 1: { doc } " )
799
799
if len (doc ) != 1 :
800
- raise TypeError ("Bad $minKey, extra field(s): %s" % ( doc ,) )
800
+ raise TypeError (f "Bad $minKey, extra field(s): { doc } " )
801
801
return MinKey ()
802
802
803
803
@@ -806,7 +806,7 @@ def _parse_canonical_maxkey(doc: Any) -> MaxKey:
806
806
if type (doc ["$maxKey" ]) is not int or doc ["$maxKey" ] != 1 :
807
807
raise TypeError ("$maxKey value must be 1: %s" , (doc ,))
808
808
if len (doc ) != 1 :
809
- raise TypeError ("Bad $minKey, extra field(s): %s" % ( doc ,) )
809
+ raise TypeError (f "Bad $minKey, extra field(s): { doc } " )
810
810
return MaxKey ()
811
811
812
812
@@ -839,7 +839,7 @@ def default(obj: Any, json_options: JSONOptions = DEFAULT_JSON_OPTIONS) -> Any:
839
839
millis = int (obj .microsecond / 1000 )
840
840
fracsecs = ".%03d" % (millis ,) if millis else ""
841
841
return {
842
- "$date" : "%s%s%s" % (obj .strftime ("%Y-%m-%dT%H:%M:%S" ), fracsecs , tz_string )
842
+ "$date" : "{}{}{}" . format (obj .strftime ("%Y-%m-%dT%H:%M:%S" ), fracsecs , tz_string )
843
843
}
844
844
845
845
millis = _datetime_to_millis (obj )
0 commit comments