Skip to content

Commit 466d0a1

Browse files
PYTHON-4903 Adds typing overloading to bson.binary.Binary.from_vector (mongodb#1967)
1 parent 91d0d89 commit 466d0a1

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

bson/binary.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import struct
1717
from dataclasses import dataclass
1818
from enum import Enum
19-
from typing import TYPE_CHECKING, Any, Optional, Sequence, Tuple, Type, Union
19+
from typing import TYPE_CHECKING, Any, Optional, Sequence, Tuple, Type, Union, overload
2020
from uuid import UUID
2121

2222
"""Tools for representing BSON binary data.
@@ -195,7 +195,7 @@ class UuidRepresentation:
195195

196196

197197
VECTOR_SUBTYPE = 9
198-
"""**(BETA)** BSON binary subtype for densely packed vector data.
198+
"""BSON binary subtype for densely packed vector data.
199199
200200
.. versionadded:: 4.10
201201
"""
@@ -207,7 +207,7 @@ class UuidRepresentation:
207207

208208

209209
class BinaryVectorDtype(Enum):
210-
"""**(BETA)** Datatypes of vector subtype.
210+
"""Datatypes of vector subtype.
211211
212212
:param FLOAT32: (0x27) Pack list of :class:`float` as float32
213213
:param INT8: (0x03) Pack list of :class:`int` in [-128, 127] as signed int8
@@ -229,7 +229,7 @@ class BinaryVectorDtype(Enum):
229229

230230
@dataclass
231231
class BinaryVector:
232-
"""**(BETA)** Vector of numbers along with metadata for binary interoperability.
232+
"""Vector of numbers along with metadata for binary interoperability.
233233
.. versionadded:: 4.10
234234
"""
235235

@@ -256,7 +256,7 @@ class Binary(bytes):
256256
the difference between what should be considered binary data and
257257
what should be considered a string when we encode to BSON.
258258
259-
**(BETA)** Subtype 9 provides a space-efficient representation of 1-dimensional vector data.
259+
Subtype 9 provides a space-efficient representation of 1-dimensional vector data.
260260
Its data is prepended with two bytes of metadata.
261261
The first (dtype) describes its data type, such as float32 or int8.
262262
The second (padding) prescribes the number of bits to ignore in the final byte.
@@ -278,7 +278,7 @@ class Binary(bytes):
278278
Support any bytes-like type that implements the buffer protocol.
279279
280280
.. versionchanged:: 4.10
281-
**(BETA)** Addition of vector subtype.
281+
Addition of vector subtype.
282282
"""
283283

284284
_type_marker = 5
@@ -397,14 +397,26 @@ def as_uuid(self, uuid_representation: int = UuidRepresentation.STANDARD) -> UUI
397397
f"cannot decode subtype {self.subtype} to {UUID_REPRESENTATION_NAMES[uuid_representation]}"
398398
)
399399

400+
@classmethod
401+
@overload
402+
def from_vector(cls: Type[Binary], vector: BinaryVector) -> Binary:
403+
...
404+
405+
@classmethod
406+
@overload
407+
def from_vector(
408+
cls: Type[Binary], vector: list[int, float], dtype: BinaryVectorDtype, padding: int = 0
409+
) -> Binary:
410+
...
411+
400412
@classmethod
401413
def from_vector(
402414
cls: Type[Binary],
403415
vector: Union[BinaryVector, list[int, float]],
404416
dtype: Optional[BinaryVectorDtype] = None,
405417
padding: Optional[int] = None,
406418
) -> Binary:
407-
"""**(BETA)** Create a BSON :class:`~bson.binary.Binary` of Vector subtype.
419+
"""Create a BSON :class:`~bson.binary.Binary` of Vector subtype.
408420
409421
To interpret the representation of the numbers, a data type must be included.
410422
See :class:`~bson.binary.BinaryVectorDtype` for available types and descriptions.
@@ -447,7 +459,7 @@ def from_vector(
447459
return cls(metadata + data, subtype=VECTOR_SUBTYPE)
448460

449461
def as_vector(self) -> BinaryVector:
450-
"""**(BETA)** From the Binary, create a list of numbers, along with dtype and padding.
462+
"""From the Binary, create a list of numbers, along with dtype and padding.
451463
452464
:return: BinaryVector
453465

test/test_bson.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -802,6 +802,12 @@ def test_vector(self):
802802
assert float_binary == Binary.from_vector(
803803
BinaryVector(list_vector, BinaryVectorDtype.FLOAT32)
804804
)
805+
# Confirm kwargs cannot be passed when BinaryVector is provided
806+
with self.assertRaises(ValueError):
807+
Binary.from_vector(
808+
BinaryVector(list_vector, BinaryVectorDtype.PACKED_BIT, padding),
809+
dtype=BinaryVectorDtype.PACKED_BIT,
810+
) # type: ignore[call-overload]
805811

806812
def test_unicode_regex(self):
807813
"""Tests we do not get a segfault for C extension on unicode RegExs.

0 commit comments

Comments
 (0)