Skip to content

Commit a8feb7c

Browse files
committed
Updated signature of Binary.from_vector to take a BinaryVector
1 parent 006a996 commit a8feb7c

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

bson/binary.py

Lines changed: 15 additions & 4 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, Sequence, Tuple, Type, Union
19+
from typing import TYPE_CHECKING, Any, Optional, Sequence, Tuple, Type, Union
2020
from uuid import UUID
2121

2222
"""Tools for representing BSON binary data.
@@ -400,9 +400,9 @@ def as_uuid(self, uuid_representation: int = UuidRepresentation.STANDARD) -> UUI
400400
@classmethod
401401
def from_vector(
402402
cls: Type[Binary],
403-
vector: list[int, float],
404-
dtype: BinaryVectorDtype,
405-
padding: int = 0,
403+
vector: Union[BinaryVector, list[int, float]],
404+
dtype: Optional[BinaryVectorDtype] = None,
405+
padding: Optional[int] = None,
406406
) -> Binary:
407407
"""**(BETA)** Create a BSON :class:`~bson.binary.Binary` of Vector subtype from a list of Numbers.
408408
@@ -418,6 +418,17 @@ def from_vector(
418418
419419
.. versionadded:: 4.10
420420
"""
421+
if isinstance(vector, BinaryVector):
422+
if dtype or padding:
423+
raise ValueError(
424+
"The first argument, vector, has type BinaryVector. "
425+
"dtype or padding cannot be separately defined, but were."
426+
)
427+
dtype = vector.dtype
428+
padding = vector.padding
429+
vector = vector.data
430+
431+
padding = 0 if padding is None else padding
421432
if dtype == BinaryVectorDtype.INT8: # pack ints in [-128, 127] as signed int8
422433
format_str = "b"
423434
if padding:

test/test_bson.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,13 @@
5151
is_valid,
5252
json_util,
5353
)
54-
from bson.binary import USER_DEFINED_SUBTYPE, Binary, BinaryVectorDtype, UuidRepresentation
54+
from bson.binary import (
55+
USER_DEFINED_SUBTYPE,
56+
Binary,
57+
BinaryVector,
58+
BinaryVectorDtype,
59+
UuidRepresentation,
60+
)
5561
from bson.code import Code
5662
from bson.codec_options import CodecOptions, DatetimeConversion
5763
from bson.datetime_ms import _DATETIME_ERROR_SUGGESTION
@@ -785,6 +791,18 @@ def test_vector(self):
785791
else:
786792
self.fail("Failed to raise an exception.")
787793

794+
# Test form of Binary.from_vector(BinaryVector)
795+
796+
assert padded_vec == Binary.from_vector(
797+
BinaryVector(list_vector, BinaryVectorDtype.PACKED_BIT, padding)
798+
)
799+
assert binary_vector == Binary.from_vector(
800+
BinaryVector(list_vector, BinaryVectorDtype.INT8)
801+
)
802+
assert float_binary == Binary.from_vector(
803+
BinaryVector(list_vector, BinaryVectorDtype.FLOAT32)
804+
)
805+
788806
def test_unicode_regex(self):
789807
"""Tests we do not get a segfault for C extension on unicode RegExs.
790808
This had been happening.

0 commit comments

Comments
 (0)