16
16
import struct
17
17
from dataclasses import dataclass
18
18
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
20
20
from uuid import UUID
21
21
22
22
"""Tools for representing BSON binary data.
@@ -400,9 +400,9 @@ def as_uuid(self, uuid_representation: int = UuidRepresentation.STANDARD) -> UUI
400
400
@classmethod
401
401
def from_vector (
402
402
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 ,
406
406
) -> Binary :
407
407
"""**(BETA)** Create a BSON :class:`~bson.binary.Binary` of Vector subtype from a list of Numbers.
408
408
@@ -418,6 +418,17 @@ def from_vector(
418
418
419
419
.. versionadded:: 4.10
420
420
"""
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
421
432
if dtype == BinaryVectorDtype .INT8 : # pack ints in [-128, 127] as signed int8
422
433
format_str = "b"
423
434
if padding :
0 commit comments