1212# See the License for the specific language governing permissions and
1313# limitations under the License.
1414
15+ from typing import Any , Tuple , Type
1516from uuid import UUID
16- from warnings import warn
1717
1818"""Tools for representing BSON binary data.
1919"""
@@ -69,7 +69,7 @@ class UuidRepresentation:
6969 code. When decoding a BSON binary field with a UUID subtype, a
7070 :class:`~bson.binary.Binary` instance will be returned instead of a
7171 :class:`uuid.UUID` instance.
72-
72+
7373 See :ref:`unspecified-representation-details` for details.
7474
7575 .. versionadded:: 3.11
@@ -81,7 +81,7 @@ class UuidRepresentation:
8181 :class:`uuid.UUID` instances will automatically be encoded to
8282 and decoded from BSON binary, using RFC-4122 byte order with
8383 binary subtype :data:`UUID_SUBTYPE`.
84-
84+
8585 See :ref:`standard-representation-details` for details.
8686
8787 .. versionadded:: 3.11
@@ -93,7 +93,7 @@ class UuidRepresentation:
9393 :class:`uuid.UUID` instances will automatically be encoded to
9494 and decoded from BSON binary, using RFC-4122 byte order with
9595 binary subtype :data:`OLD_UUID_SUBTYPE`.
96-
96+
9797 See :ref:`python-legacy-representation-details` for details.
9898
9999 .. versionadded:: 3.11
@@ -105,7 +105,7 @@ class UuidRepresentation:
105105 :class:`uuid.UUID` instances will automatically be encoded to
106106 and decoded from BSON binary subtype :data:`OLD_UUID_SUBTYPE`,
107107 using the Java driver's legacy byte order.
108-
108+
109109 See :ref:`java-legacy-representation-details` for details.
110110
111111 .. versionadded:: 3.11
@@ -117,7 +117,7 @@ class UuidRepresentation:
117117 :class:`uuid.UUID` instances will automatically be encoded to
118118 and decoded from BSON binary subtype :data:`OLD_UUID_SUBTYPE`,
119119 using the C# driver's legacy byte order.
120-
120+
121121 See :ref:`csharp-legacy-representation-details` for details.
122122
123123 .. versionadded:: 3.11
@@ -153,11 +153,12 @@ class UuidRepresentation:
153153"""
154154
155155ALL_UUID_SUBTYPES = (OLD_UUID_SUBTYPE , UUID_SUBTYPE )
156- ALL_UUID_REPRESENTATIONS = (UuidRepresentation .UNSPECIFIED ,
157- UuidRepresentation .STANDARD ,
158- UuidRepresentation .PYTHON_LEGACY ,
159- UuidRepresentation .JAVA_LEGACY ,
160- UuidRepresentation .CSHARP_LEGACY )
156+ ALL_UUID_REPRESENTATIONS = (
157+ UuidRepresentation .UNSPECIFIED ,
158+ UuidRepresentation .STANDARD ,
159+ UuidRepresentation .PYTHON_LEGACY ,
160+ UuidRepresentation .JAVA_LEGACY ,
161+ UuidRepresentation .CSHARP_LEGACY )
161162UUID_REPRESENTATION_NAMES = {
162163 UuidRepresentation .UNSPECIFIED : 'UuidRepresentation.UNSPECIFIED' ,
163164 UuidRepresentation .STANDARD : 'UuidRepresentation.STANDARD' ,
@@ -208,8 +209,9 @@ class Binary(bytes):
208209 """
209210
210211 _type_marker = 5
212+ __subtype : int
211213
212- def __new__ (cls , data , subtype = BINARY_SUBTYPE ):
214+ def __new__ (cls : Type [ "Binary" ] , data : bytes , subtype : int = BINARY_SUBTYPE ) -> "Binary" :
213215 if not isinstance (subtype , int ):
214216 raise TypeError ("subtype must be an instance of int" )
215217 if subtype >= 256 or subtype < 0 :
@@ -220,7 +222,7 @@ def __new__(cls, data, subtype=BINARY_SUBTYPE):
220222 return self
221223
222224 @classmethod
223- def from_uuid (cls , uuid , uuid_representation = UuidRepresentation .STANDARD ):
225+ def from_uuid (cls : Type [ "Binary" ] , uuid : UUID , uuid_representation : int = UuidRepresentation .STANDARD ) -> "Binary" :
224226 """Create a BSON Binary object from a Python UUID.
225227
226228 Creates a :class:`~bson.binary.Binary` object from a
@@ -271,7 +273,7 @@ def from_uuid(cls, uuid, uuid_representation=UuidRepresentation.STANDARD):
271273
272274 return cls (payload , subtype )
273275
274- def as_uuid (self , uuid_representation = UuidRepresentation .STANDARD ):
276+ def as_uuid (self , uuid_representation : int = UuidRepresentation .STANDARD ) -> UUID :
275277 """Create a Python UUID from this BSON Binary object.
276278
277279 Decodes this binary object as a native :class:`uuid.UUID` instance
@@ -316,19 +318,19 @@ def as_uuid(self, uuid_representation=UuidRepresentation.STANDARD):
316318 self .subtype , UUID_REPRESENTATION_NAMES [uuid_representation ]))
317319
318320 @property
319- def subtype (self ):
321+ def subtype (self ) -> int :
320322 """Subtype of this binary data.
321323 """
322324 return self .__subtype
323325
324- def __getnewargs__ (self ):
326+ def __getnewargs__ (self ) -> Tuple [ bytes , int ]: # type: ignore[override]
325327 # Work around http://bugs.python.org/issue7382
326328 data = super (Binary , self ).__getnewargs__ ()[0 ]
327329 if not isinstance (data , bytes ):
328330 data = data .encode ('latin-1' )
329331 return data , self .__subtype
330332
331- def __eq__ (self , other ) :
333+ def __eq__ (self , other : Any ) -> bool :
332334 if isinstance (other , Binary ):
333335 return ((self .__subtype , bytes (self )) ==
334336 (other .subtype , bytes (other )))
@@ -337,10 +339,10 @@ def __eq__(self, other):
337339 # subclass of str...
338340 return False
339341
340- def __hash__ (self ):
342+ def __hash__ (self ) -> int :
341343 return super (Binary , self ).__hash__ () ^ hash (self .__subtype )
342344
343- def __ne__ (self , other ) :
345+ def __ne__ (self , other : Any ) -> bool :
344346 return not self == other
345347
346348 def __repr__ (self ):
0 commit comments