Skip to content

Commit 3abdca5

Browse files
author
Mark Edwards
committed
Implement Array protocol in src/graphql_relay/connection/arrayconnection.py
1 parent 4c29756 commit 3abdca5

File tree

3 files changed

+49
-4
lines changed

3 files changed

+49
-4
lines changed

Diff for: pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ packages = [
3131
[tool.poetry.dependencies]
3232
python = "^3.6"
3333
graphql-core = "^3.0"
34+
typing-extensions = { version = "^3.6.2", python = "<3.8" }
3435

3536
[tool.poetry.dev-dependencies]
3637
pytest = "^5.3"

Diff for: setup.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@
2929
"Programming Language :: Python :: 3.8",
3030
"Programming Language :: Python :: Implementation :: PyPy",
3131
],
32-
install_requires=["graphql-core>=3.0.0"],
32+
install_requires=[
33+
"graphql-core>=3.0.0",
34+
"typing-extensions>=3.6.2,<4; python_version < '3.8'",
35+
],
3336
python_requires=">=3.6,<4",
3437
packages=find_packages("src"),
3538
package_dir={"": "src"},

Diff for: src/graphql_relay/connection/arrayconnection.py

+44-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import binascii
22
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
48

59
from ..utils.base64 import base64, unbase64
610
from .connectiontypes import (
@@ -21,8 +25,17 @@
2125
]
2226

2327

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+
2437
def connection_from_array(
25-
data: Sequence,
38+
data: SizedSliceable,
2639
args: ConnectionArguments = None,
2740
connection_type: Any = Connection,
2841
edge_type: Any = Edge,
@@ -78,8 +91,35 @@ def connection_from_list(
7891
)
7992

8093

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+
81121
def connection_from_array_slice(
82-
array_slice: Sequence,
122+
array_slice: Union[Sliceable, SizedSliceable],
83123
args: ConnectionArguments = None,
84124
slice_start: int = 0,
85125
array_length: int = None,
@@ -112,6 +152,7 @@ def connection_from_array_slice(
112152
first = args.get("first")
113153
last = args.get("last")
114154
if array_slice_length is None:
155+
assert isinstance(array_slice, Sized)
115156
array_slice_length = len(array_slice)
116157
slice_end = slice_start + array_slice_length
117158
if array_length is None:

0 commit comments

Comments
 (0)