1212# See the License for the specific language governing permissions and
1313# limitations under the License.
1414
15- from typing import Any , Tuple , Type , Union , TYPE_CHECKING
15+ from typing import TYPE_CHECKING , Any , Tuple , Type , Union
1616from uuid import UUID
1717
1818"""Tools for representing BSON binary data.
@@ -163,13 +163,15 @@ class UuidRepresentation:
163163 UuidRepresentation .STANDARD ,
164164 UuidRepresentation .PYTHON_LEGACY ,
165165 UuidRepresentation .JAVA_LEGACY ,
166- UuidRepresentation .CSHARP_LEGACY )
166+ UuidRepresentation .CSHARP_LEGACY ,
167+ )
167168UUID_REPRESENTATION_NAMES = {
168- UuidRepresentation .UNSPECIFIED : 'UuidRepresentation.UNSPECIFIED' ,
169- UuidRepresentation .STANDARD : 'UuidRepresentation.STANDARD' ,
170- UuidRepresentation .PYTHON_LEGACY : 'UuidRepresentation.PYTHON_LEGACY' ,
171- UuidRepresentation .JAVA_LEGACY : 'UuidRepresentation.JAVA_LEGACY' ,
172- UuidRepresentation .CSHARP_LEGACY : 'UuidRepresentation.CSHARP_LEGACY' }
169+ UuidRepresentation .UNSPECIFIED : "UuidRepresentation.UNSPECIFIED" ,
170+ UuidRepresentation .STANDARD : "UuidRepresentation.STANDARD" ,
171+ UuidRepresentation .PYTHON_LEGACY : "UuidRepresentation.PYTHON_LEGACY" ,
172+ UuidRepresentation .JAVA_LEGACY : "UuidRepresentation.JAVA_LEGACY" ,
173+ UuidRepresentation .CSHARP_LEGACY : "UuidRepresentation.CSHARP_LEGACY" ,
174+ }
173175
174176MD5_SUBTYPE = 5
175177"""BSON binary subtype for an MD5 hash.
@@ -216,7 +218,11 @@ class Binary(bytes):
216218 _type_marker = 5
217219 __subtype : int
218220
219- def __new__ (cls : Type ["Binary" ], data : Union [memoryview , bytes , "_mmap" , "_array" ], subtype : int = BINARY_SUBTYPE ) -> "Binary" :
221+ def __new__ (
222+ cls : Type ["Binary" ],
223+ data : Union [memoryview , bytes , "_mmap" , "_array" ],
224+ subtype : int = BINARY_SUBTYPE ,
225+ ) -> "Binary" :
220226 if not isinstance (subtype , int ):
221227 raise TypeError ("subtype must be an instance of int" )
222228 if subtype >= 256 or subtype < 0 :
@@ -227,7 +233,9 @@ def __new__(cls: Type["Binary"], data: Union[memoryview, bytes, "_mmap", "_array
227233 return self
228234
229235 @classmethod
230- def from_uuid (cls : Type ["Binary" ], uuid : UUID , uuid_representation : int = UuidRepresentation .STANDARD ) -> "Binary" :
236+ def from_uuid (
237+ cls : Type ["Binary" ], uuid : UUID , uuid_representation : int = UuidRepresentation .STANDARD
238+ ) -> "Binary" :
231239 """Create a BSON Binary object from a Python UUID.
232240
233241 Creates a :class:`~bson.binary.Binary` object from a
@@ -251,8 +259,9 @@ def from_uuid(cls: Type["Binary"], uuid: UUID, uuid_representation: int = UuidRe
251259 raise TypeError ("uuid must be an instance of uuid.UUID" )
252260
253261 if uuid_representation not in ALL_UUID_REPRESENTATIONS :
254- raise ValueError ("uuid_representation must be a value "
255- "from bson.binary.UuidRepresentation" )
262+ raise ValueError (
263+ "uuid_representation must be a value " "from bson.binary.UuidRepresentation"
264+ )
256265
257266 if uuid_representation == UuidRepresentation .UNSPECIFIED :
258267 raise ValueError (
@@ -261,7 +270,8 @@ def from_uuid(cls: Type["Binary"], uuid: UUID, uuid_representation: int = UuidRe
261270 "converted to bson.Binary instances using "
262271 "bson.Binary.from_uuid() or a different UuidRepresentation "
263272 "can be configured. See the documentation for "
264- "UuidRepresentation for more information." )
273+ "UuidRepresentation for more information."
274+ )
265275
266276 subtype = OLD_UUID_SUBTYPE
267277 if uuid_representation == UuidRepresentation .PYTHON_LEGACY :
@@ -296,12 +306,12 @@ def as_uuid(self, uuid_representation: int = UuidRepresentation.STANDARD) -> UUI
296306 .. versionadded:: 3.11
297307 """
298308 if self .subtype not in ALL_UUID_SUBTYPES :
299- raise ValueError ("cannot decode subtype %s as a uuid" % (
300- self .subtype ,))
309+ raise ValueError ("cannot decode subtype %s as a uuid" % (self .subtype ,))
301310
302311 if uuid_representation not in ALL_UUID_REPRESENTATIONS :
303- raise ValueError ("uuid_representation must be a value from "
304- "bson.binary.UuidRepresentation" )
312+ raise ValueError (
313+ "uuid_representation must be a value from " "bson.binary.UuidRepresentation"
314+ )
305315
306316 if uuid_representation == UuidRepresentation .UNSPECIFIED :
307317 raise ValueError ("uuid_representation cannot be UNSPECIFIED" )
@@ -319,26 +329,26 @@ def as_uuid(self, uuid_representation: int = UuidRepresentation.STANDARD) -> UUI
319329 if self .subtype == UUID_SUBTYPE :
320330 return UUID (bytes = self )
321331
322- raise ValueError ("cannot decode subtype %s to %s" % (
323- self .subtype , UUID_REPRESENTATION_NAMES [uuid_representation ]))
332+ raise ValueError (
333+ "cannot decode subtype %s to %s"
334+ % (self .subtype , UUID_REPRESENTATION_NAMES [uuid_representation ])
335+ )
324336
325337 @property
326338 def subtype (self ) -> int :
327- """Subtype of this binary data.
328- """
339+ """Subtype of this binary data."""
329340 return self .__subtype
330341
331342 def __getnewargs__ (self ) -> Tuple [bytes , int ]: # type: ignore[override]
332343 # Work around http://bugs.python.org/issue7382
333344 data = super (Binary , self ).__getnewargs__ ()[0 ]
334345 if not isinstance (data , bytes ):
335- data = data .encode (' latin-1' )
346+ data = data .encode (" latin-1" )
336347 return data , self .__subtype
337348
338- def __eq__ (self , other : Any ) -> bool :
349+ def __eq__ (self , other : Any ) -> bool :
339350 if isinstance (other , Binary ):
340- return ((self .__subtype , bytes (self )) ==
341- (other .subtype , bytes (other )))
351+ return (self .__subtype , bytes (self )) == (other .subtype , bytes (other ))
342352 # We don't return NotImplemented here because if we did then
343353 # Binary("foo") == "foo" would return True, since Binary is a
344354 # subclass of str...
0 commit comments