Skip to content

Commit 146984d

Browse files
committed
Use postponed evaluation of type annotations
1 parent d2d5030 commit 146984d

File tree

14 files changed

+72
-44
lines changed

14 files changed

+72
-44
lines changed

src/graphql/error/syntax_error.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations # Python < 3.10
2+
13
from typing import TYPE_CHECKING
24

35
from .graphql_error import GraphQLError
@@ -11,7 +13,7 @@
1113
class GraphQLSyntaxError(GraphQLError):
1214
"""A GraphQLError representing a syntax error."""
1315

14-
def __init__(self, source: "Source", position: int, description: str) -> None:
16+
def __init__(self, source: Source, position: int, description: str) -> None:
1517
super().__init__(
1618
f"Syntax Error: {description}", source=source, positions=[position]
1719
)

src/graphql/execution/map_async_iterator.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations # Python < 3.10
2+
13
from asyncio import CancelledError, Event, Task, ensure_future, wait
24
from concurrent.futures import FIRST_COMPLETED
35
from inspect import isasyncgen, isawaitable
@@ -23,7 +25,7 @@ def __init__(self, iterable: AsyncIterable, callback: Callable) -> None:
2325
self.callback = callback
2426
self._close_event = Event()
2527

26-
def __aiter__(self) -> "MapAsyncIterator":
28+
def __aiter__(self) -> MapAsyncIterator:
2729
"""Get the iterator object."""
2830
return self
2931

src/graphql/language/ast.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations # Python < 3.10
2+
13
from copy import copy, deepcopy
24
from enum import Enum
35
from typing import Any, Dict, List, Tuple, Optional, Union
@@ -137,7 +139,7 @@ def __hash__(self) -> int:
137139
(self.kind, self.start, self.end, self.line, self.column, self.value)
138140
)
139141

140-
def __copy__(self) -> "Token":
142+
def __copy__(self) -> Token:
141143
"""Create a shallow copy of the token"""
142144
token = self.__class__(
143145
self.kind,
@@ -150,7 +152,7 @@ def __copy__(self) -> "Token":
150152
token.prev = self.prev
151153
return token
152154

153-
def __deepcopy__(self, memo: Dict) -> "Token":
155+
def __deepcopy__(self, memo: Dict) -> Token:
154156
"""Allow only shallow copies to avoid recursion."""
155157
return copy(self)
156158

@@ -360,11 +362,11 @@ def __setattr__(self, key: str, value: Any) -> None:
360362
del self._hash
361363
super().__setattr__(key, value)
362364

363-
def __copy__(self) -> "Node":
365+
def __copy__(self) -> Node:
364366
"""Create a shallow copy of the node."""
365367
return self.__class__(**{key: getattr(self, key) for key in self.keys})
366368

367-
def __deepcopy__(self, memo: Dict) -> "Node":
369+
def __deepcopy__(self, memo: Dict) -> Node:
368370
"""Create a deep copy of the node"""
369371
# noinspection PyArgumentList
370372
return self.__class__(

src/graphql/language/location.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations # Python < 3.10
2+
13
from typing import Any, NamedTuple, TYPE_CHECKING
24

35
try:
@@ -37,7 +39,7 @@ def __ne__(self, other: Any) -> bool:
3739
return not self == other
3840

3941

40-
def get_location(source: "Source", position: int) -> SourceLocation:
42+
def get_location(source: Source, position: int) -> SourceLocation:
4143
"""Get the line and column for a character position in the source.
4244
4345
Takes a Source and a UTF-8 character offset, and returns the corresponding line and

src/graphql/pyutils/frozen_dict.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations # Python < 3.10
2+
13
from copy import deepcopy
24
from typing import Dict, TypeVar
35

@@ -28,12 +30,12 @@ def __iadd__(self, value):
2830
def __hash__(self):
2931
return hash(tuple(self.items()))
3032

31-
def __copy__(self) -> "FrozenDict":
33+
def __copy__(self) -> FrozenDict:
3234
return FrozenDict(self)
3335

3436
copy = __copy__
3537

36-
def __deepcopy__(self, memo: Dict) -> "FrozenDict":
38+
def __deepcopy__(self, memo: Dict) -> FrozenDict:
3739
return FrozenDict({k: deepcopy(v, memo) for k, v in self.items()})
3840

3941
def clear(self):

src/graphql/pyutils/frozen_list.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations # Python < 3.10
2+
13
from copy import deepcopy
24
from typing import Dict, List, TypeVar
35

@@ -39,10 +41,10 @@ def __imul__(self, value):
3941
def __hash__(self):
4042
return hash(tuple(self))
4143

42-
def __copy__(self) -> "FrozenList":
44+
def __copy__(self) -> FrozenList:
4345
return FrozenList(self)
4446

45-
def __deepcopy__(self, memo: Dict) -> "FrozenList":
47+
def __deepcopy__(self, memo: Dict) -> FrozenList:
4648
return FrozenList(deepcopy(value, memo) for value in self)
4749

4850
def append(self, x):

src/graphql/pyutils/path.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations # Python < 3.10
2+
13
from typing import Any, List, NamedTuple, Optional, Union
24

35
__all__ = ["Path"]
@@ -13,7 +15,7 @@ class Path(NamedTuple):
1315
typename: Optional[str]
1416
"""name of the parent type to avoid path ambiguity"""
1517

16-
def add_key(self, key: Union[str, int], typename: Optional[str] = None) -> "Path":
18+
def add_key(self, key: Union[str, int], typename: Optional[str] = None) -> Path:
1719
"""Return a new Path containing the given key."""
1820
return Path(self, key, typename)
1921

src/graphql/pyutils/simple_pub_sub.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations # Python < 3.10
2+
13
from asyncio import Future, Queue, ensure_future, sleep
24
from inspect import isawaitable
35
from typing import Any, AsyncIterator, Callable, Optional, Set
@@ -31,7 +33,7 @@ def emit(self, event: Any) -> bool:
3133

3234
def get_subscriber(
3335
self, transform: Optional[Callable] = None
34-
) -> "SimplePubSubIterator":
36+
) -> SimplePubSubIterator:
3537
return SimplePubSubIterator(self, transform)
3638

3739

@@ -44,7 +46,7 @@ def __init__(self, pubsub: SimplePubSub, transform: Optional[Callable]) -> None:
4446
self.listening = True
4547
pubsub.subscribers.add(self.push_value)
4648

47-
def __aiter__(self) -> "SimplePubSubIterator":
49+
def __aiter__(self) -> SimplePubSubIterator:
4850
return self
4951

5052
async def __anext__(self) -> Any:

src/graphql/type/definition.py

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations # Python < 3.10
2+
13
from enum import Enum
24
from typing import (
35
Any,
@@ -280,7 +282,7 @@ def to_kwargs(self) -> GraphQLNamedTypeKwargs:
280282
extension_ast_nodes=self.extension_ast_nodes,
281283
)
282284

283-
def __copy__(self) -> "GraphQLNamedType": # pragma: no cover
285+
def __copy__(self) -> GraphQLNamedType: # pragma: no cover
284286
return self.__class__(**self.to_kwargs())
285287

286288

@@ -451,7 +453,7 @@ def to_kwargs(self) -> GraphQLScalarTypeKwargs:
451453
specified_by_url=self.specified_by_url,
452454
)
453455

454-
def __copy__(self) -> "GraphQLScalarType": # pragma: no cover
456+
def __copy__(self) -> GraphQLScalarType: # pragma: no cover
455457
return self.__class__(**self.to_kwargs())
456458

457459

@@ -469,7 +471,7 @@ def assert_scalar_type(type_: Any) -> GraphQLScalarType:
469471

470472

471473
class GraphQLFieldKwargs(TypedDict, total=False):
472-
type_: "GraphQLOutputType"
474+
type_: GraphQLOutputType
473475
args: Optional[GraphQLArgumentMap]
474476
resolve: Optional["GraphQLFieldResolver"]
475477
subscribe: Optional["GraphQLFieldResolver"]
@@ -482,7 +484,7 @@ class GraphQLFieldKwargs(TypedDict, total=False):
482484
class GraphQLField:
483485
"""Definition of a GraphQL field"""
484486

485-
type: "GraphQLOutputType"
487+
type: GraphQLOutputType
486488
args: GraphQLArgumentMap
487489
resolve: Optional["GraphQLFieldResolver"]
488490
subscribe: Optional["GraphQLFieldResolver"]
@@ -493,7 +495,7 @@ class GraphQLField:
493495

494496
def __init__(
495497
self,
496-
type_: "GraphQLOutputType",
498+
type_: GraphQLOutputType,
497499
args: Optional[GraphQLArgumentMap] = None,
498500
resolve: Optional["GraphQLFieldResolver"] = None,
499501
subscribe: Optional["GraphQLFieldResolver"] = None,
@@ -577,7 +579,7 @@ def to_kwargs(self) -> GraphQLFieldKwargs:
577579
ast_node=self.ast_node,
578580
)
579581

580-
def __copy__(self) -> "GraphQLField": # pragma: no cover
582+
def __copy__(self) -> GraphQLField: # pragma: no cover
581583
return self.__class__(**self.to_kwargs())
582584

583585

@@ -593,10 +595,10 @@ class GraphQLResolveInfo(NamedTuple):
593595

594596
field_name: str
595597
field_nodes: List[FieldNode]
596-
return_type: "GraphQLOutputType"
597-
parent_type: "GraphQLObjectType"
598+
return_type: GraphQLOutputType
599+
parent_type: GraphQLObjectType
598600
path: Path
599-
schema: "GraphQLSchema"
601+
schema: GraphQLSchema
600602
fragments: Dict[str, FragmentDefinitionNode]
601603
root_value: Any
602604
operation: OperationDefinitionNode
@@ -628,7 +630,7 @@ class GraphQLResolveInfo(NamedTuple):
628630

629631

630632
class GraphQLArgumentKwargs(TypedDict, total=False):
631-
type_: "GraphQLInputType"
633+
type_: GraphQLInputType
632634
default_value: Any
633635
description: Optional[str]
634636
deprecation_reason: Optional[str]
@@ -640,7 +642,7 @@ class GraphQLArgumentKwargs(TypedDict, total=False):
640642
class GraphQLArgument:
641643
"""Definition of a GraphQL argument"""
642644

643-
type: "GraphQLInputType"
645+
type: GraphQLInputType
644646
default_value: Any
645647
description: Optional[str]
646648
deprecation_reason: Optional[str]
@@ -650,7 +652,7 @@ class GraphQLArgument:
650652

651653
def __init__(
652654
self,
653-
type_: "GraphQLInputType",
655+
type_: GraphQLInputType,
654656
default_value: Any = Undefined,
655657
description: Optional[str] = None,
656658
deprecation_reason: Optional[str] = None,
@@ -706,7 +708,7 @@ def to_kwargs(self) -> GraphQLArgumentKwargs:
706708
ast_node=self.ast_node,
707709
)
708710

709-
def __copy__(self) -> "GraphQLArgument": # pragma: no cover
711+
def __copy__(self) -> GraphQLArgument: # pragma: no cover
710712
return self.__class__(**self.to_kwargs())
711713

712714

@@ -798,7 +800,7 @@ def to_kwargs(self) -> GraphQLObjectTypeKwargs:
798800
is_type_of=self.is_type_of,
799801
)
800802

801-
def __copy__(self) -> "GraphQLObjectType": # pragma: no cover
803+
def __copy__(self) -> GraphQLObjectType: # pragma: no cover
802804
return self.__class__(**self.to_kwargs())
803805

804806
@cached_property
@@ -932,7 +934,7 @@ def to_kwargs(self) -> GraphQLInterfaceTypeKwargs:
932934
resolve_type=self.resolve_type,
933935
)
934936

935-
def __copy__(self) -> "GraphQLInterfaceType": # pragma: no cover
937+
def __copy__(self) -> GraphQLInterfaceType: # pragma: no cover
936938
return self.__class__(**self.to_kwargs())
937939

938940
@cached_property
@@ -1063,7 +1065,7 @@ def to_kwargs(self) -> GraphQLUnionTypeKwargs:
10631065
super().to_kwargs(), types=self.types, resolve_type=self.resolve_type
10641066
)
10651067

1066-
def __copy__(self) -> "GraphQLUnionType": # pragma: no cover
1068+
def __copy__(self) -> GraphQLUnionType: # pragma: no cover
10671069
return self.__class__(**self.to_kwargs())
10681070

10691071
@cached_property
@@ -1203,7 +1205,7 @@ def to_kwargs(self) -> GraphQLEnumTypeKwargs:
12031205
super().to_kwargs(), values=self.values.copy()
12041206
)
12051207

1206-
def __copy__(self) -> "GraphQLEnumType": # pragma: no cover
1208+
def __copy__(self) -> GraphQLEnumType: # pragma: no cover
12071209
return self.__class__(**self.to_kwargs())
12081210

12091211
@cached_property
@@ -1352,7 +1354,7 @@ def to_kwargs(self) -> GraphQLEnumValueKwargs:
13521354
ast_node=self.ast_node,
13531355
)
13541356

1355-
def __copy__(self) -> "GraphQLEnumValue": # pragma: no cover
1357+
def __copy__(self) -> GraphQLEnumValue: # pragma: no cover
13561358
return self.__class__(**self.to_kwargs())
13571359

13581360

@@ -1446,7 +1448,7 @@ def to_kwargs(self) -> GraphQLInputObjectTypeKwargs:
14461448
else self.out_type,
14471449
)
14481450

1449-
def __copy__(self) -> "GraphQLInputObjectType": # pragma: no cover
1451+
def __copy__(self) -> GraphQLInputObjectType: # pragma: no cover
14501452
return self.__class__(**self.to_kwargs())
14511453

14521454
@cached_property
@@ -1491,7 +1493,7 @@ def assert_input_object_type(type_: Any) -> GraphQLInputObjectType:
14911493

14921494

14931495
class GraphQLInputFieldKwargs(TypedDict, total=False):
1494-
type_: "GraphQLInputType"
1496+
type_: GraphQLInputType
14951497
default_value: Any
14961498
description: Optional[str]
14971499
deprecation_reason: Optional[str]
@@ -1503,7 +1505,7 @@ class GraphQLInputFieldKwargs(TypedDict, total=False):
15031505
class GraphQLInputField:
15041506
"""Definition of a GraphQL input field"""
15051507

1506-
type: "GraphQLInputType"
1508+
type: GraphQLInputType
15071509
default_value: Any
15081510
description: Optional[str]
15091511
deprecation_reason: Optional[str]
@@ -1513,7 +1515,7 @@ class GraphQLInputField:
15131515

15141516
def __init__(
15151517
self,
1516-
type_: "GraphQLInputType",
1518+
type_: GraphQLInputType,
15171519
default_value: Any = Undefined,
15181520
description: Optional[str] = None,
15191521
deprecation_reason: Optional[str] = None,
@@ -1569,7 +1571,7 @@ def to_kwargs(self) -> GraphQLInputFieldKwargs:
15691571
ast_node=self.ast_node,
15701572
)
15711573

1572-
def __copy__(self) -> "GraphQLInputField": # pragma: no cover
1574+
def __copy__(self) -> GraphQLInputField: # pragma: no cover
15731575
return self.__class__(**self.to_kwargs())
15741576

15751577

src/graphql/type/directives.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations # Python < 3.10
2+
13
from typing import Any, Collection, Dict, Optional, Tuple, cast
24

35
from ..language import ast, DirectiveLocation
@@ -143,7 +145,7 @@ def to_kwargs(self) -> GraphQLDirectiveKwargs:
143145
ast_node=self.ast_node,
144146
)
145147

146-
def __copy__(self) -> "GraphQLDirective": # pragma: no cover
148+
def __copy__(self) -> GraphQLDirective: # pragma: no cover
147149
return self.__class__(**self.to_kwargs())
148150

149151

0 commit comments

Comments
 (0)