Skip to content

Commit 10a2d8a

Browse files
committed
More modernization of typing annotations in src
1 parent 066617b commit 10a2d8a

File tree

108 files changed

+856
-638
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

108 files changed

+856
-638
lines changed

src/graphql/error/graphql_error.py

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
"""GraphQL Error"""
22

3+
from __future__ import annotations
4+
35
from sys import exc_info
4-
from typing import TYPE_CHECKING, Any, Collection, Dict, List, Optional, Union
6+
from typing import TYPE_CHECKING, Any, Collection, Dict
57

68
try:
79
from typing import TypedDict
@@ -39,12 +41,12 @@ class GraphQLFormattedError(TypedDict, total=False):
3941
message: str
4042
# If an error can be associated to a particular point in the requested
4143
# GraphQL document, it should contain a list of locations.
42-
locations: List["FormattedSourceLocation"]
44+
locations: list[FormattedSourceLocation]
4345
# If an error can be associated to a particular field in the GraphQL result,
4446
# it _must_ contain an entry with the key `path` that details the path of
4547
# the response field which experienced the error. This allows clients to
4648
# identify whether a null result is intentional or caused by a runtime error.
47-
path: List[Union[str, int]]
49+
path: list[str | int]
4850
# Reserved for implementors to extend the protocol however they see fit,
4951
# and hence there are no additional restrictions on its contents.
5052
extensions: GraphQLErrorExtensions
@@ -62,7 +64,7 @@ class GraphQLError(Exception):
6264
message: str
6365
"""A message describing the Error for debugging purposes"""
6466

65-
locations: Optional[List["SourceLocation"]]
67+
locations: list[SourceLocation] | None
6668
"""Source locations
6769
6870
A list of (line, column) locations within the source GraphQL document which
@@ -73,7 +75,7 @@ class GraphQLError(Exception):
7375
the field which produced the error.
7476
"""
7577

76-
path: Optional[List[Union[str, int]]]
78+
path: list[str | int] | None
7779
"""
7880
7981
A list of field names and array indexes describing the JSON-path into the execution
@@ -82,27 +84,27 @@ class GraphQLError(Exception):
8284
Only included for errors during execution.
8385
"""
8486

85-
nodes: Optional[List["Node"]]
87+
nodes: list[Node] | None
8688
"""A list of GraphQL AST Nodes corresponding to this error"""
8789

88-
source: Optional["Source"]
90+
source: Source | None
8991
"""The source GraphQL document for the first location of this error
9092
9193
Note that if this Error represents more than one node, the source may not represent
9294
nodes after the first node.
9395
"""
9496

95-
positions: Optional[Collection[int]]
97+
positions: Collection[int] | None
9698
"""Error positions
9799
98100
A list of character offsets within the source GraphQL document which correspond
99101
to this error.
100102
"""
101103

102-
original_error: Optional[Exception]
104+
original_error: Exception | None
103105
"""The original error thrown from a field resolver during execution"""
104106

105-
extensions: Optional[GraphQLErrorExtensions]
107+
extensions: GraphQLErrorExtensions | None
106108
"""Extension fields to add to the formatted error"""
107109

108110
__slots__ = (
@@ -121,12 +123,12 @@ class GraphQLError(Exception):
121123
def __init__(
122124
self,
123125
message: str,
124-
nodes: Union[Collection["Node"], "Node", None] = None,
125-
source: Optional["Source"] = None,
126-
positions: Optional[Collection[int]] = None,
127-
path: Optional[Collection[Union[str, int]]] = None,
128-
original_error: Optional[Exception] = None,
129-
extensions: Optional[GraphQLErrorExtensions] = None,
126+
nodes: Collection[Node] | Node | None = None,
127+
source: Source | None = None,
128+
positions: Collection[int] | None = None,
129+
path: Collection[str | int] | None = None,
130+
original_error: Exception | None = None,
131+
extensions: GraphQLErrorExtensions | None = None,
130132
) -> None:
131133
"""Initialize a GraphQLError."""
132134
super().__init__(message)
@@ -155,7 +157,7 @@ def __init__(
155157
positions = [loc.start for loc in node_locations]
156158
self.positions = positions or None
157159
if positions and source:
158-
locations: Optional[List[SourceLocation]] = [
160+
locations: list[SourceLocation] | None = [
159161
source.get_location(pos) for pos in positions
160162
]
161163
else:

src/graphql/error/located_error.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
"""Located GraphQL Error"""
22

3+
from __future__ import annotations
4+
35
from contextlib import suppress
4-
from typing import TYPE_CHECKING, Collection, Optional, Union
6+
from typing import TYPE_CHECKING, Collection
57

68
from ..pyutils import inspect
79
from .graphql_error import GraphQLError
@@ -14,8 +16,8 @@
1416

1517
def located_error(
1618
original_error: Exception,
17-
nodes: Optional[Union[None, Collection["Node"]]] = None,
18-
path: Optional[Collection[Union[str, int]]] = None,
19+
nodes: None | Collection[Node] = None,
20+
path: Collection[str | int] | None = None,
1921
) -> GraphQLError:
2022
"""Located GraphQL Error
2123

src/graphql/error/syntax_error.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""GraphQL Syntax Error"""
22

3-
from __future__ import annotations # Python < 3.10
3+
from __future__ import annotations
44

55
from typing import TYPE_CHECKING
66

src/graphql/execution/async_iterables.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Helpers for async iterables"""
22

3-
from __future__ import annotations # Python < 3.10
3+
from __future__ import annotations
44

55
from contextlib import AbstractAsyncContextManager
66
from typing import (

src/graphql/execution/collect_fields.py

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
"""Collect fields"""
22

3+
from __future__ import annotations
4+
35
from collections import defaultdict
4-
from typing import Any, Dict, List, NamedTuple, Optional, Set, Union
6+
from typing import Any, NamedTuple
57

68
from ..language import (
79
FieldNode,
@@ -29,21 +31,21 @@
2931
class PatchFields(NamedTuple):
3032
"""Optionally labelled set of fields to be used as a patch."""
3133

32-
label: Optional[str]
33-
fields: Dict[str, List[FieldNode]]
34+
label: str | None
35+
fields: dict[str, list[FieldNode]]
3436

3537

3638
class FieldsAndPatches(NamedTuple):
3739
"""Tuple of collected fields and patches to be applied."""
3840

39-
fields: Dict[str, List[FieldNode]]
40-
patches: List[PatchFields]
41+
fields: dict[str, list[FieldNode]]
42+
patches: list[PatchFields]
4143

4244

4345
def collect_fields(
4446
schema: GraphQLSchema,
45-
fragments: Dict[str, FragmentDefinitionNode],
46-
variable_values: Dict[str, Any],
47+
fragments: dict[str, FragmentDefinitionNode],
48+
variable_values: dict[str, Any],
4749
runtime_type: GraphQLObjectType,
4850
operation: OperationDefinitionNode,
4951
) -> FieldsAndPatches:
@@ -57,8 +59,8 @@ def collect_fields(
5759
5860
For internal use only.
5961
"""
60-
fields: Dict[str, List[FieldNode]] = defaultdict(list)
61-
patches: List[PatchFields] = []
62+
fields: dict[str, list[FieldNode]] = defaultdict(list)
63+
patches: list[PatchFields] = []
6264
collect_fields_impl(
6365
schema,
6466
fragments,
@@ -75,11 +77,11 @@ def collect_fields(
7577

7678
def collect_subfields(
7779
schema: GraphQLSchema,
78-
fragments: Dict[str, FragmentDefinitionNode],
79-
variable_values: Dict[str, Any],
80+
fragments: dict[str, FragmentDefinitionNode],
81+
variable_values: dict[str, Any],
8082
operation: OperationDefinitionNode,
8183
return_type: GraphQLObjectType,
82-
field_nodes: List[FieldNode],
84+
field_nodes: list[FieldNode],
8385
) -> FieldsAndPatches:
8486
"""Collect subfields.
8587
@@ -92,10 +94,10 @@ def collect_subfields(
9294
9395
For internal use only.
9496
"""
95-
sub_field_nodes: Dict[str, List[FieldNode]] = defaultdict(list)
96-
visited_fragment_names: Set[str] = set()
97+
sub_field_nodes: dict[str, list[FieldNode]] = defaultdict(list)
98+
visited_fragment_names: set[str] = set()
9799

98-
sub_patches: List[PatchFields] = []
100+
sub_patches: list[PatchFields] = []
99101
sub_fields_and_patches = FieldsAndPatches(sub_field_nodes, sub_patches)
100102

101103
for node in field_nodes:
@@ -116,17 +118,17 @@ def collect_subfields(
116118

117119
def collect_fields_impl(
118120
schema: GraphQLSchema,
119-
fragments: Dict[str, FragmentDefinitionNode],
120-
variable_values: Dict[str, Any],
121+
fragments: dict[str, FragmentDefinitionNode],
122+
variable_values: dict[str, Any],
121123
operation: OperationDefinitionNode,
122124
runtime_type: GraphQLObjectType,
123125
selection_set: SelectionSetNode,
124-
fields: Dict[str, List[FieldNode]],
125-
patches: List[PatchFields],
126-
visited_fragment_names: Set[str],
126+
fields: dict[str, list[FieldNode]],
127+
patches: list[PatchFields],
128+
visited_fragment_names: set[str],
127129
) -> None:
128130
"""Collect fields (internal implementation)."""
129-
patch_fields: Dict[str, List[FieldNode]]
131+
patch_fields: dict[str, list[FieldNode]]
130132

131133
for selection in selection_set.selections:
132134
if isinstance(selection, FieldNode):
@@ -216,14 +218,14 @@ def collect_fields_impl(
216218
class DeferValues(NamedTuple):
217219
"""Values of an active defer directive."""
218220

219-
label: Optional[str]
221+
label: str | None
220222

221223

222224
def get_defer_values(
223225
operation: OperationDefinitionNode,
224-
variable_values: Dict[str, Any],
225-
node: Union[FragmentSpreadNode, InlineFragmentNode],
226-
) -> Optional[DeferValues]:
226+
variable_values: dict[str, Any],
227+
node: FragmentSpreadNode | InlineFragmentNode,
228+
) -> DeferValues | None:
227229
"""Get values of defer directive if active.
228230
229231
Returns an object containing the `@defer` arguments if a field should be
@@ -246,8 +248,8 @@ def get_defer_values(
246248

247249

248250
def should_include_node(
249-
variable_values: Dict[str, Any],
250-
node: Union[FragmentSpreadNode, FieldNode, InlineFragmentNode],
251+
variable_values: dict[str, Any],
252+
node: FragmentSpreadNode | FieldNode | InlineFragmentNode,
251253
) -> bool:
252254
"""Check if node should be included
253255
@@ -267,7 +269,7 @@ def should_include_node(
267269

268270
def does_fragment_condition_match(
269271
schema: GraphQLSchema,
270-
fragment: Union[FragmentDefinitionNode, InlineFragmentNode],
272+
fragment: FragmentDefinitionNode | InlineFragmentNode,
271273
type_: GraphQLObjectType,
272274
) -> bool:
273275
"""Determine if a fragment is applicable to the given type."""

src/graphql/execution/execute.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""GraphQL execution"""
22

3-
from __future__ import annotations # Python < 3.10
3+
from __future__ import annotations
44

55
from asyncio import Event, as_completed, ensure_future, gather, shield, sleep, wait_for
66
from collections.abc import Mapping

src/graphql/execution/middleware.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
"""Middleware manager"""
22

3+
from __future__ import annotations
4+
35
from functools import partial, reduce
46
from inspect import isfunction
5-
from typing import Any, Callable, Dict, Iterator, List, Optional, Tuple
7+
from typing import Any, Callable, Iterator
68

79
try:
810
from typing import TypeAlias
@@ -30,8 +32,8 @@ class MiddlewareManager:
3032
# allow custom attributes (not used internally)
3133
__slots__ = "__dict__", "middlewares", "_middleware_resolvers", "_cached_resolvers"
3234

33-
_cached_resolvers: Dict[GraphQLFieldResolver, GraphQLFieldResolver]
34-
_middleware_resolvers: Optional[List[Callable]]
35+
_cached_resolvers: dict[GraphQLFieldResolver, GraphQLFieldResolver]
36+
_middleware_resolvers: list[Callable] | None
3537

3638
def __init__(self, *middlewares: Any) -> None:
3739
self.middlewares = middlewares
@@ -59,7 +61,7 @@ def get_field_resolver(
5961
return self._cached_resolvers[field_resolver]
6062

6163

62-
def get_middleware_resolvers(middlewares: Tuple[Any, ...]) -> Iterator[Callable]:
64+
def get_middleware_resolvers(middlewares: tuple[Any, ...]) -> Iterator[Callable]:
6365
"""Get a list of resolver functions from a list of classes or functions."""
6466
for middleware in middlewares:
6567
if isfunction(middleware):

0 commit comments

Comments
 (0)