|
1 | 1 | import binascii
|
2 | 2 | import warnings
|
3 |
| -from typing import Any, Optional, Sequence |
| 3 | +from typing import Any, Iterator, Optional, overload, Sequence, Sized, Union |
| 4 | +try: |
| 5 | + from typing import Protocol |
| 6 | +except ImportError: |
| 7 | + from typing_extensions import Protocol # type: ignore |
4 | 8 |
|
5 | 9 | from ..utils.base64 import base64, unbase64
|
6 | 10 | from .connectiontypes import (
|
|
21 | 25 | ]
|
22 | 26 |
|
23 | 27 |
|
| 28 | +class Sliceable(Protocol): |
| 29 | + def __getitem__(self, index: slice) -> Any: ... |
| 30 | + def __iter__(self) -> Iterator: ... |
| 31 | + |
| 32 | + |
| 33 | +class SizedSliceable(Sliceable, Protocol): |
| 34 | + def __len__(self) -> int: ... |
| 35 | + |
| 36 | + |
24 | 37 | def connection_from_array(
|
25 |
| - data: Sequence, |
| 38 | + data: SizedSliceable, |
26 | 39 | args: ConnectionArguments = None,
|
27 | 40 | connection_type: Any = Connection,
|
28 | 41 | edge_type: Any = Edge,
|
@@ -78,8 +91,35 @@ def connection_from_list(
|
78 | 91 | )
|
79 | 92 |
|
80 | 93 |
|
| 94 | +@overload |
| 95 | +def connection_from_array_slice( |
| 96 | + array_slice: Sliceable, |
| 97 | + *, |
| 98 | + args: ConnectionArguments = None, |
| 99 | + slice_start: int = 0, |
| 100 | + array_length: int = None, |
| 101 | + array_slice_length: int, |
| 102 | + connection_type: Any = Connection, |
| 103 | + edge_type: Any = Edge, |
| 104 | + page_info_type: Any = PageInfo, |
| 105 | +) -> Connection: ... |
| 106 | + |
| 107 | + |
| 108 | +@overload |
| 109 | +def connection_from_array_slice( |
| 110 | + array_slice: SizedSliceable, |
| 111 | + args: ConnectionArguments = None, |
| 112 | + slice_start: int = 0, |
| 113 | + array_length: int = None, |
| 114 | + array_slice_length: int = None, |
| 115 | + connection_type: Any = Connection, |
| 116 | + edge_type: Any = Edge, |
| 117 | + page_info_type: Any = PageInfo, |
| 118 | +) -> Connection: ... |
| 119 | + |
| 120 | + |
81 | 121 | def connection_from_array_slice(
|
82 |
| - array_slice: Sequence, |
| 122 | + array_slice: Union[Sliceable, SizedSliceable], |
83 | 123 | args: ConnectionArguments = None,
|
84 | 124 | slice_start: int = 0,
|
85 | 125 | array_length: int = None,
|
@@ -112,6 +152,7 @@ def connection_from_array_slice(
|
112 | 152 | first = args.get("first")
|
113 | 153 | last = args.get("last")
|
114 | 154 | if array_slice_length is None:
|
| 155 | + assert isinstance(array_slice, Sized) |
115 | 156 | array_slice_length = len(array_slice)
|
116 | 157 | slice_end = slice_start + array_slice_length
|
117 | 158 | if array_length is None:
|
|
0 commit comments