|
53 | 53 | ]
|
54 | 54 |
|
55 | 55 |
|
| 56 | +_DEFAULT = object() |
| 57 | + |
| 58 | + |
56 | 59 | class Vector:
|
57 | 60 | r"""
|
58 | 61 | A class representing a Neo4j vector.
|
@@ -753,10 +756,11 @@ def _from_native_rust(cls, data: _t.Iterable[object], /) -> _t.Self:
|
753 | 756 | @classmethod
|
754 | 757 | def _from_native_np(cls, data: _t.Iterable[object], /) -> _t.Self:
|
755 | 758 | data = tuple(data)
|
756 |
| - non_float = tuple(item for item in data if not isinstance(item, float)) |
757 |
| - if non_float: |
| 759 | + non_float_gen = (item for item in data if not isinstance(item, float)) |
| 760 | + non_float = next(non_float_gen, _DEFAULT) |
| 761 | + if non_float is not _DEFAULT: |
758 | 762 | raise TypeError(
|
759 |
| - f"Cannot build f64 vector from {type(non_float[0]).__name__}, " |
| 763 | + f"Cannot build f64 vector from {type(non_float).__name__}, " |
760 | 764 | "expected float."
|
761 | 765 | )
|
762 | 766 | return cls(_np.fromiter(data, dtype=_np.dtype(">f8")).tobytes())
|
@@ -826,10 +830,11 @@ def _from_native_rust(cls, data: _t.Iterable[object], /) -> _t.Self:
|
826 | 830 | @classmethod
|
827 | 831 | def _from_native_np(cls, data: _t.Iterable[object], /) -> _t.Self:
|
828 | 832 | data = tuple(data)
|
829 |
| - non_float = tuple(item for item in data if not isinstance(item, float)) |
830 |
| - if non_float: |
| 833 | + non_float_gen = (item for item in data if not isinstance(item, float)) |
| 834 | + non_float = next(non_float_gen, _DEFAULT) |
| 835 | + if non_float is not _DEFAULT: |
831 | 836 | raise TypeError(
|
832 |
| - f"Cannot build f32 vector from {type(non_float[0]).__name__}, " |
| 837 | + f"Cannot build f32 vector from {type(non_float).__name__}, " |
833 | 838 | "expected float."
|
834 | 839 | )
|
835 | 840 | return cls(_np.fromiter(data, dtype=_np.dtype(">f4")).tobytes())
|
@@ -903,10 +908,11 @@ def _from_native_rust(cls, data: _t.Iterable[object], /) -> _t.Self:
|
903 | 908 | @classmethod
|
904 | 909 | def _from_native_np(cls, data: _t.Iterable[object], /) -> _t.Self:
|
905 | 910 | data = tuple(data)
|
906 |
| - non_int = tuple(item for item in data if not isinstance(item, int)) |
907 |
| - if non_int: |
| 911 | + non_int_gen = (item for item in data if not isinstance(item, int)) |
| 912 | + non_int = next(non_int_gen, _DEFAULT) |
| 913 | + if non_int is not _DEFAULT: |
908 | 914 | raise TypeError(
|
909 |
| - f"Cannot build i64 vector from {type(non_int[0]).__name__}, " |
| 915 | + f"Cannot build i64 vector from {type(non_int).__name__}, " |
910 | 916 | "expected int."
|
911 | 917 | )
|
912 | 918 | data = _t.cast(tuple[int, ...], data)
|
@@ -994,10 +1000,11 @@ def _from_native_rust(cls, data: _t.Iterable[object], /) -> _t.Self:
|
994 | 1000 | @classmethod
|
995 | 1001 | def _from_native_np(cls, data: _t.Iterable[object], /) -> _t.Self:
|
996 | 1002 | data = tuple(data)
|
997 |
| - non_int = tuple(item for item in data if not isinstance(item, int)) |
998 |
| - if non_int: |
| 1003 | + non_int_gen = (item for item in data if not isinstance(item, int)) |
| 1004 | + non_int = next(non_int_gen, _DEFAULT) |
| 1005 | + if non_int is not _DEFAULT: |
999 | 1006 | raise TypeError(
|
1000 |
| - f"Cannot build i32 vector from {type(non_int[0]).__name__}, " |
| 1007 | + f"Cannot build i32 vector from {type(non_int).__name__}, " |
1001 | 1008 | "expected int."
|
1002 | 1009 | )
|
1003 | 1010 | data = _t.cast(tuple[int, ...], data)
|
@@ -1085,10 +1092,11 @@ def _from_native_rust(cls, data: _t.Iterable[object], /) -> _t.Self:
|
1085 | 1092 | @classmethod
|
1086 | 1093 | def _from_native_np(cls, data: _t.Iterable[object], /) -> _t.Self:
|
1087 | 1094 | data = tuple(data)
|
1088 |
| - non_int = tuple(item for item in data if not isinstance(item, int)) |
1089 |
| - if non_int: |
| 1095 | + non_int_gen = (item for item in data if not isinstance(item, int)) |
| 1096 | + non_int = next(non_int_gen, _DEFAULT) |
| 1097 | + if non_int is not _DEFAULT: |
1090 | 1098 | raise TypeError(
|
1091 |
| - f"Cannot build i16 vector from {type(non_int[0]).__name__}, " |
| 1099 | + f"Cannot build i16 vector from {type(non_int).__name__}, " |
1092 | 1100 | "expected int."
|
1093 | 1101 | )
|
1094 | 1102 | data = _t.cast(tuple[int, ...], data)
|
@@ -1176,10 +1184,11 @@ def _from_native_rust(cls, data: _t.Iterable[object], /) -> _t.Self:
|
1176 | 1184 | @classmethod
|
1177 | 1185 | def _from_native_np(cls, data: _t.Iterable[object], /) -> _t.Self:
|
1178 | 1186 | data = tuple(data)
|
1179 |
| - non_int = tuple(item for item in data if not isinstance(item, int)) |
1180 |
| - if non_int: |
| 1187 | + non_int_gen = (item for item in data if not isinstance(item, int)) |
| 1188 | + non_int = next(non_int_gen, _DEFAULT) |
| 1189 | + if non_int is not _DEFAULT: |
1181 | 1190 | raise TypeError(
|
1182 |
| - f"Cannot build i8 vector from {type(non_int[0]).__name__}, " |
| 1191 | + f"Cannot build i8 vector from {type(non_int).__name__}, " |
1183 | 1192 | "expected int."
|
1184 | 1193 | )
|
1185 | 1194 | data = _t.cast(tuple[int, ...], data)
|
|
0 commit comments