Skip to content

Commit 7dba1e5

Browse files
authored
PYTHON-5043: Fix list[int, float] typo in binary.py (#2066)
1 parent 2ff2fde commit 7dba1e5

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

bson/binary.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -405,14 +405,17 @@ def from_vector(cls: Type[Binary], vector: BinaryVector) -> Binary:
405405
@classmethod
406406
@overload
407407
def from_vector(
408-
cls: Type[Binary], vector: list[int, float], dtype: BinaryVectorDtype, padding: int = 0
408+
cls: Type[Binary],
409+
vector: Union[list[int], list[float]],
410+
dtype: BinaryVectorDtype,
411+
padding: int = 0,
409412
) -> Binary:
410413
...
411414

412415
@classmethod
413416
def from_vector(
414417
cls: Type[Binary],
415-
vector: Union[BinaryVector, list[int, float]],
418+
vector: Union[BinaryVector, list[int], list[float]],
416419
dtype: Optional[BinaryVectorDtype] = None,
417420
padding: Optional[int] = None,
418421
) -> Binary:

test/test_typing.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"""Test that each file in mypy_fails/ actually fails mypy, and test some
1616
sample client code that uses PyMongo typings.
1717
"""
18+
1819
from __future__ import annotations
1920

2021
import os
@@ -37,7 +38,8 @@
3738
if TYPE_CHECKING:
3839
from typing_extensions import NotRequired, TypedDict
3940

40-
from bson import ObjectId
41+
from bson import Binary, ObjectId
42+
from bson.binary import BinaryVector, BinaryVectorDtype
4143

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

593595

596+
class TestBSONFromVectorType(unittest.TestCase):
597+
@only_type_check
598+
def test_from_vector_binaryvector(self):
599+
list_vector = BinaryVector([127, 7], BinaryVectorDtype.INT8)
600+
Binary.from_vector(list_vector)
601+
602+
@only_type_check
603+
def test_from_vector_list_int(self):
604+
list_vector = [127, 7]
605+
Binary.from_vector(list_vector, BinaryVectorDtype.INT8)
606+
607+
@only_type_check
608+
def test_from_vector_list_float(self):
609+
list_vector = [127.0, 7.0]
610+
Binary.from_vector(list_vector, BinaryVectorDtype.INT8)
611+
612+
594613
if __name__ == "__main__":
595614
unittest.main()

0 commit comments

Comments
 (0)