Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update typing #48

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3

- name: Set up Python 3.9
uses: actions/setup-python@v2
uses: actions/setup-python@v4
with:
python-version: 3.9

Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3

- name: Set up Python 3.9
uses: actions/setup-python@v2
uses: actions/setup-python@v4
with:
python-version: 3.9

Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ jobs:
python: ['3.6', '3.7', '3.8', '3.9', '3.10', 'pypy3']

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}

Expand Down
2 changes: 2 additions & 0 deletions src/graphql_relay/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
PageInfo,
PageInfoConstructor,
PageInfoType,
Traversable,
)

# Helpers for creating connections from arrays
Expand Down Expand Up @@ -92,6 +93,7 @@
"plural_identifying_root_field",
"ResolvedGlobalId",
"SizedSliceable",
"Traversable",
"to_global_id",
"version",
"version_info",
Expand Down
95 changes: 86 additions & 9 deletions src/graphql_relay/connection/array_connection.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
from typing import Any, Iterator, Optional, Sequence
import sys
from typing import Any, Iterator, Optional, overload, Sequence

try:
from typing import Protocol
except ImportError: # Python < 3.8
from typing_extensions import Protocol # type: ignore
if sys.version_info >= (3, 8):
from typing import Protocol # pragma: no cover
else:
from typing_extensions import Protocol # pragma: no cover

from ..utils.base64 import base64, unbase64
from .connection import (
Connection,
ConnectionArguments,
ConnectionConstructor,
ConnectionCursor,
ConnectionType,
Edge,
EdgeConstructor,
PageInfo,
Expand All @@ -33,20 +33,54 @@ class SizedSliceable(Protocol):
def __getitem__(self, index: slice) -> Any:
...

def __iter__(self) -> Iterator:
def __iter__(self) -> Iterator[Any]:
...

def __len__(self) -> int:
...


@overload
def connection_from_array(
data: SizedSliceable,
args: Optional[ConnectionArguments] = None,
*,
edge_type: EdgeConstructor = Edge,
page_info_type: PageInfoConstructor = PageInfo,
) -> Connection:
...


@overload
def connection_from_array(
data: SizedSliceable,
args: Optional[ConnectionArguments],
connection_type: ConnectionConstructor,
edge_type: EdgeConstructor = Edge,
page_info_type: PageInfoConstructor = PageInfo,
) -> Any:
...


@overload
def connection_from_array(
data: SizedSliceable,
args: Optional[ConnectionArguments] = None,
*,
connection_type: ConnectionConstructor,
edge_type: EdgeConstructor = Edge,
page_info_type: PageInfoConstructor = PageInfo,
) -> Any:
...


def connection_from_array(
data: SizedSliceable,
args: Optional[ConnectionArguments] = None,
connection_type: ConnectionConstructor = Connection,
edge_type: EdgeConstructor = Edge,
page_info_type: PageInfoConstructor = PageInfo,
) -> ConnectionType:
) -> Any:
"""Create a connection object from a sequence of objects.

Note that different from its JavaScript counterpart which expects an array,
Expand All @@ -70,6 +104,49 @@ def connection_from_array(
)


@overload
def connection_from_array_slice(
array_slice: SizedSliceable,
args: Optional[ConnectionArguments] = None,
slice_start: int = 0,
array_length: Optional[int] = None,
array_slice_length: Optional[int] = None,
*,
edge_type: EdgeConstructor = Edge,
page_info_type: PageInfoConstructor = PageInfo,
) -> Connection:
...


@overload
def connection_from_array_slice(
array_slice: SizedSliceable,
args: Optional[ConnectionArguments],
slice_start: int,
array_length: Optional[int],
array_slice_length: Optional[int],
connection_type: ConnectionConstructor,
edge_type: EdgeConstructor = Edge,
page_info_type: PageInfoConstructor = PageInfo,
) -> Any:
...


@overload
def connection_from_array_slice(
array_slice: SizedSliceable,
args: Optional[ConnectionArguments] = None,
slice_start: int = 0,
array_length: Optional[int] = None,
array_slice_length: Optional[int] = None,
*,
connection_type: ConnectionConstructor,
edge_type: EdgeConstructor = Edge,
page_info_type: PageInfoConstructor = PageInfo,
) -> Any:
...


def connection_from_array_slice(
array_slice: SizedSliceable,
args: Optional[ConnectionArguments] = None,
Expand All @@ -79,7 +156,7 @@ def connection_from_array_slice(
connection_type: ConnectionConstructor = Connection,
edge_type: EdgeConstructor = Edge,
page_info_type: PageInfoConstructor = PageInfo,
) -> ConnectionType:
) -> Any:
"""Create a connection object from a slice of the result set.

Note that different from its JavaScript counterpart which expects an array,
Expand Down
31 changes: 19 additions & 12 deletions src/graphql_relay/connection/connection.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sys
from typing import Any, Dict, List, NamedTuple, Optional, Union

from graphql import (
Expand All @@ -18,10 +19,10 @@

from graphql import GraphQLNamedOutputType

try:
from typing import Protocol
except ImportError: # Python < 3.8
from typing_extensions import Protocol # type: ignore
if sys.version_info >= (3, 8):
from typing import Protocol # pragma: no cover
else:
from typing_extensions import Protocol # pragma: no cover

__all__ = [
"backward_connection_args",
Expand All @@ -41,6 +42,7 @@
"PageInfo",
"PageInfoConstructor",
"PageInfoType",
"Traversable",
]


Expand Down Expand Up @@ -152,12 +154,15 @@ class PageInfoType(Protocol):
def startCursor(self) -> Optional[ConnectionCursor]:
...

@property
def endCursor(self) -> Optional[ConnectionCursor]:
...

@property
def hasPreviousPage(self) -> bool:
...

@property
def hasNextPage(self) -> bool:
...

Expand All @@ -170,7 +175,7 @@ def __call__(
endCursor: Optional[ConnectionCursor],
hasPreviousPage: bool,
hasNextPage: bool,
) -> PageInfoType:
) -> Any:
...


Expand All @@ -183,18 +188,20 @@ class PageInfo(NamedTuple):
hasNextPage: bool


class EdgeType(Protocol):
class Traversable(Protocol):
@property
def node(self) -> Any:
def cursor(self) -> ConnectionCursor:
...


class EdgeType(Traversable, Protocol):
@property
def cursor(self) -> ConnectionCursor:
def node(self) -> Any:
...


class EdgeConstructor(Protocol):
def __call__(self, *, node: Any, cursor: ConnectionCursor) -> EdgeType:
def __call__(self, *, node: Any, cursor: ConnectionCursor) -> Traversable:
...


Expand All @@ -219,9 +226,9 @@ class ConnectionConstructor(Protocol):
def __call__(
self,
*,
edges: List[EdgeType],
pageInfo: PageInfoType,
) -> ConnectionType:
edges: List[Any],
pageInfo: Any,
) -> Any:
...


Expand Down
2 changes: 0 additions & 2 deletions src/graphql_relay/mutation/mutation.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ def augmented_output_fields() -> GraphQLFieldMap:
input_type = GraphQLInputObjectType(name + "Input", fields=augmented_input_fields)

if iscoroutinefunction(mutate_and_get_payload):

# noinspection PyShadowingBuiltins
async def resolve(_root: Any, info: GraphQLResolveInfo, input: Dict) -> Any:
payload = await mutate_and_get_payload(info, **input)
Expand All @@ -94,7 +93,6 @@ async def resolve(_root: Any, info: GraphQLResolveInfo, input: Dict) -> Any:
return payload

else:

# noinspection PyShadowingBuiltins
def resolve( # type: ignore
_root: Any, info: GraphQLResolveInfo, input: Dict
Expand Down
2 changes: 0 additions & 2 deletions src/graphql_relay/node/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@


class GraphQLNodeDefinitions(NamedTuple):

node_interface: GraphQLInterfaceType
node_field: GraphQLField
nodes_field: GraphQLField
Expand Down Expand Up @@ -83,7 +82,6 @@ def node_definitions(


class ResolvedGlobalId(NamedTuple):

type: str
id: str

Expand Down
1 change: 0 additions & 1 deletion tests/mutation/test_mutation.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@


class Result:

# noinspection PyPep8Naming
def __init__(self, result, clientMutationId=None):
self.clientMutationId = clientMutationId
Expand Down
1 change: 0 additions & 1 deletion tests/star_wars_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,6 @@ def get_node_type(obj, _info, _type):


class IntroduceShipMutation:

# noinspection PyPep8Naming
def __init__(self, shipId, factionId, clientMutationId=None):
self.shipId = shipId
Expand Down