Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions bson/binary.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,14 +405,17 @@ def from_vector(cls: Type[Binary], vector: BinaryVector) -> Binary:
@classmethod
@overload
def from_vector(
cls: Type[Binary], vector: list[int, float], dtype: BinaryVectorDtype, padding: int = 0
cls: Type[Binary],
vector: Union[list[int], list[float]],
dtype: BinaryVectorDtype,
padding: int = 0,
) -> Binary:
...

@classmethod
def from_vector(
cls: Type[Binary],
vector: Union[BinaryVector, list[int, float]],
vector: Union[BinaryVector, list[int], list[float]],
dtype: Optional[BinaryVectorDtype] = None,
padding: Optional[int] = None,
) -> Binary:
Expand Down
21 changes: 20 additions & 1 deletion test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"""Test that each file in mypy_fails/ actually fails mypy, and test some
sample client code that uses PyMongo typings.
"""

from __future__ import annotations

import os
Expand All @@ -37,7 +38,8 @@
if TYPE_CHECKING:
from typing_extensions import NotRequired, TypedDict

from bson import ObjectId
from bson import Binary, ObjectId
from bson.binary import BinaryVector, BinaryVectorDtype

class Movie(TypedDict):
name: str
Expand Down Expand Up @@ -591,5 +593,22 @@ def test_son_document_type(self) -> None:
obj["a"] = 1


class TestBSONFromVectorType(unittest.TestCase):
@only_type_check
def test_from_vector_binaryvector(self):
list_vector = BinaryVector([127, 7], BinaryVectorDtype.INT8)
Binary.from_vector(list_vector)

@only_type_check
def test_from_vector_list_int(self):
list_vector = [127, 7]
Binary.from_vector(list_vector, BinaryVectorDtype.INT8)

@only_type_check
def test_from_vector_list_float(self):
list_vector = [127.0, 7.0]
Binary.from_vector(list_vector, BinaryVectorDtype.INT8)


if __name__ == "__main__":
unittest.main()
Loading