Skip to content

Commit 55547d8

Browse files
committed
Use type aliases for more clarity
1 parent 2c215fc commit 55547d8

26 files changed

+211
-69
lines changed

src/graphql/error/graphql_error.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
from typing import TypedDict
77
except ImportError: # Python < 3.8
88
from typing_extensions import TypedDict
9+
try:
10+
from typing import TypeAlias
11+
except ImportError: # Python < 3.10
12+
from typing_extensions import TypeAlias
913

1014
if TYPE_CHECKING:
1115
from ..language.ast import Node # noqa: F401
@@ -19,7 +23,7 @@
1923

2024

2125
# Custom extensions
22-
GraphQLErrorExtensions = Dict[str, Any]
26+
GraphQLErrorExtensions: TypeAlias = Dict[str, Any]
2327
# Use a unique identifier name for your extension, for example the name of
2428
# your library or project. Do not use a shortened identifier as this increases
2529
# the risk of conflicts. We recommend you add at most one extension key,

src/graphql/execution/execute.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@
2424
except ImportError: # Python < 3.8
2525
from typing_extensions import TypedDict
2626
try:
27-
from typing import TypeGuard
27+
from typing import TypeAlias, TypeGuard
2828
except ImportError: # Python < 3.10
29-
from typing_extensions import TypeGuard
29+
from typing_extensions import TypeAlias, TypeGuard
3030

3131
from ..error import GraphQLError, GraphQLFormattedError, located_error
3232
from ..language import (
@@ -170,7 +170,7 @@ def __ne__(self, other: Any) -> bool:
170170
return not self == other
171171

172172

173-
Middleware = Optional[Union[Tuple, List, MiddlewareManager]]
173+
Middleware: TypeAlias = Optional[Union[Tuple, List, MiddlewareManager]]
174174

175175

176176
class ExecutionContext:

src/graphql/execution/middleware.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,15 @@
33
from typing import Any, Callable, Dict, Iterator, List, Optional, Tuple
44

55

6+
try:
7+
from typing import TypeAlias
8+
except ImportError: # Python < 3.10
9+
from typing_extensions import TypeAlias
10+
11+
612
__all__ = ["MiddlewareManager"]
713

8-
GraphQLFieldResolver = Callable[..., Any]
14+
GraphQLFieldResolver: TypeAlias = Callable[..., Any]
915

1016

1117
class MiddlewareManager:

src/graphql/execution/values.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,16 @@
3030
from ..utilities.value_from_ast import value_from_ast
3131

3232

33+
try:
34+
from typing import TypeAlias
35+
except ImportError: # Python < 3.10
36+
from typing_extensions import TypeAlias
37+
38+
3339
__all__ = ["get_argument_values", "get_directive_values", "get_variable_values"]
3440

3541

36-
CoercedVariableValues = Union[List[GraphQLError], Dict[str, Any]]
42+
CoercedVariableValues: TypeAlias = Union[List[GraphQLError], Dict[str, Any]]
3743

3844

3945
def get_variable_values(
@@ -209,7 +215,7 @@ def get_argument_values(
209215
return coerced_values
210216

211217

212-
NodeWithDirective = Union[
218+
NodeWithDirective: TypeAlias = Union[
213219
EnumValueDefinitionNode,
214220
ExecutableDefinitionNode,
215221
FieldDefinitionNode,

src/graphql/language/ast.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@
99
from .token_kind import TokenKind
1010

1111

12+
try:
13+
from typing import TypeAlias
14+
except ImportError: # Python < 3.10
15+
from typing_extensions import TypeAlias
16+
17+
1218
__all__ = [
1319
"Location",
1420
"Token",
@@ -582,7 +588,7 @@ class ConstObjectFieldNode(ObjectFieldNode):
582588
value: ConstValueNode
583589

584590

585-
ConstValueNode = Union[
591+
ConstValueNode: TypeAlias = Union[
586592
IntValueNode,
587593
FloatValueNode,
588594
StringValueNode,
@@ -771,7 +777,7 @@ class TypeExtensionNode(TypeSystemDefinitionNode):
771777
directives: Tuple[ConstDirectiveNode, ...]
772778

773779

774-
TypeSystemExtensionNode = Union[SchemaExtensionNode, TypeExtensionNode]
780+
TypeSystemExtensionNode: TypeAlias = Union[SchemaExtensionNode, TypeExtensionNode]
775781

776782

777783
class ScalarTypeExtensionNode(TypeExtensionNode):

src/graphql/language/parser.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,17 @@
6464
from .token_kind import TokenKind
6565

6666

67+
try:
68+
from typing import TypeAlias
69+
except ImportError: # Python < 3.10
70+
from typing_extensions import TypeAlias
71+
72+
6773
__all__ = ["parse", "parse_type", "parse_value", "parse_const_value"]
6874

6975
T = TypeVar("T")
7076

71-
SourceType = Union[Source, str]
77+
SourceType: TypeAlias = Union[Source, str]
7278

7379

7480
def parse(

src/graphql/language/printer.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,18 @@
66
from .visitor import Visitor, visit
77

88

9+
try:
10+
from typing import TypeAlias
11+
except ImportError: # Python < 3.10
12+
from typing_extensions import TypeAlias
13+
14+
915
__all__ = ["print_ast"]
1016

1117

1218
MAX_LINE_LENGTH = 80
1319

14-
Strings = Collection[str]
20+
Strings: TypeAlias = Collection[str]
1521

1622

1723
class PrintedNode:

src/graphql/language/visitor.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@
1717
from .ast import QUERY_DOCUMENT_KEYS, Node
1818

1919

20+
try:
21+
from typing import TypeAlias
22+
except ImportError: # Python < 3.10
23+
from typing_extensions import TypeAlias
24+
25+
2026
__all__ = [
2127
"Visitor",
2228
"ParallelVisitor",
@@ -41,7 +47,7 @@ class VisitorActionEnum(Enum):
4147
REMOVE = Ellipsis
4248

4349

44-
VisitorAction = Optional[VisitorActionEnum]
50+
VisitorAction: TypeAlias = Optional[VisitorActionEnum]
4551

4652
# Note that in GraphQL.js these are defined differently:
4753
# BREAK = {}, SKIP = false, REMOVE = null, IDLE = undefined
@@ -51,7 +57,7 @@ class VisitorActionEnum(Enum):
5157
REMOVE = VisitorActionEnum.REMOVE
5258
IDLE = None
5359

54-
VisitorKeyMap = Dict[str, Tuple[str, ...]]
60+
VisitorKeyMap: TypeAlias = Dict[str, Tuple[str, ...]]
5561

5662

5763
class EnterLeaveVisitor(NamedTuple):
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
from typing import Awaitable, TypeVar, Union
22

33

4+
try:
5+
from typing import TypeAlias
6+
except ImportError: # Python < 3.10
7+
from typing_extensions import TypeAlias
8+
9+
410
__all__ = ["AwaitableOrValue"]
511

612

713
T = TypeVar("T")
814

9-
AwaitableOrValue = Union[Awaitable[T], T]
15+
AwaitableOrValue: TypeAlias = Union[Awaitable[T], T]

src/graphql/type/definition.py

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@
6666
except ImportError: # Python < 3.8
6767
from typing_extensions import TypedDict
6868
try:
69-
from typing import TypeGuard
69+
from typing import TypeAlias, TypeGuard
7070
except ImportError: # Python < 3.10
71-
from typing_extensions import TypeGuard
71+
from typing_extensions import TypeAlias, TypeGuard
7272

7373
if TYPE_CHECKING:
7474
from .schema import GraphQLSchema # noqa: F401
@@ -311,9 +311,9 @@ def __copy__(self) -> GraphQLNamedType: # pragma: no cover
311311

312312
T = TypeVar("T")
313313

314-
ThunkCollection = Union[Callable[[], Collection[T]], Collection[T]]
315-
ThunkMapping = Union[Callable[[], Mapping[str, T]], Mapping[str, T]]
316-
Thunk = Union[Callable[[], T], T]
314+
ThunkCollection: TypeAlias = Union[Callable[[], Collection[T]], Collection[T]]
315+
ThunkMapping: TypeAlias = Union[Callable[[], Mapping[str, T]], Mapping[str, T]]
316+
Thunk: TypeAlias = Union[Callable[[], T], T]
317317

318318

319319
def resolve_thunk(thunk: Thunk[T]) -> T:
@@ -325,9 +325,11 @@ def resolve_thunk(thunk: Thunk[T]) -> T:
325325
return thunk() if callable(thunk) else thunk
326326

327327

328-
GraphQLScalarSerializer = Callable[[Any], Any]
329-
GraphQLScalarValueParser = Callable[[Any], Any]
330-
GraphQLScalarLiteralParser = Callable[[ValueNode, Optional[Dict[str, Any]]], Any]
328+
GraphQLScalarSerializer: TypeAlias = Callable[[Any], Any]
329+
GraphQLScalarValueParser: TypeAlias = Callable[[Any], Any]
330+
GraphQLScalarLiteralParser: TypeAlias = Callable[
331+
[ValueNode, Optional[Dict[str, Any]]], Any
332+
]
331333

332334

333335
class GraphQLScalarTypeKwargs(GraphQLNamedTypeKwargs, total=False):
@@ -490,7 +492,7 @@ def assert_scalar_type(type_: Any) -> GraphQLScalarType:
490492
return type_
491493

492494

493-
GraphQLArgumentMap = Dict[str, "GraphQLArgument"]
495+
GraphQLArgumentMap: TypeAlias = Dict[str, "GraphQLArgument"]
494496

495497

496498
class GraphQLFieldKwargs(TypedDict, total=False):
@@ -633,23 +635,25 @@ class GraphQLResolveInfo(NamedTuple):
633635
# Note: Contrary to the Javascript implementation of GraphQLFieldResolver,
634636
# the context is passed as part of the GraphQLResolveInfo and any arguments
635637
# are passed individually as keyword arguments.
636-
GraphQLFieldResolverWithoutArgs = Callable[[Any, GraphQLResolveInfo], Any]
638+
GraphQLFieldResolverWithoutArgs: TypeAlias = Callable[[Any, GraphQLResolveInfo], Any]
637639
# Unfortunately there is currently no syntax to indicate optional or keyword
638640
# arguments in Python, so we also allow any other Callable as a workaround:
639-
GraphQLFieldResolver = Callable[..., Any]
641+
GraphQLFieldResolver: TypeAlias = Callable[..., Any]
640642

641643
# Note: Contrary to the Javascript implementation of GraphQLTypeResolver,
642644
# the context is passed as part of the GraphQLResolveInfo:
643-
GraphQLTypeResolver = Callable[
645+
GraphQLTypeResolver: TypeAlias = Callable[
644646
[Any, GraphQLResolveInfo, "GraphQLAbstractType"],
645647
AwaitableOrValue[Optional[str]],
646648
]
647649

648650
# Note: Contrary to the Javascript implementation of GraphQLIsTypeOfFn,
649651
# the context is passed as part of the GraphQLResolveInfo:
650-
GraphQLIsTypeOfFn = Callable[[Any, GraphQLResolveInfo], AwaitableOrValue[bool]]
652+
GraphQLIsTypeOfFn: TypeAlias = Callable[
653+
[Any, GraphQLResolveInfo], AwaitableOrValue[bool]
654+
]
651655

652-
GraphQLFieldMap = Dict[str, GraphQLField]
656+
GraphQLFieldMap: TypeAlias = Dict[str, GraphQLField]
653657

654658

655659
class GraphQLArgumentKwargs(TypedDict, total=False):
@@ -1121,7 +1125,7 @@ def assert_union_type(type_: Any) -> GraphQLUnionType:
11211125
return type_
11221126

11231127

1124-
GraphQLEnumValueMap = Dict[str, "GraphQLEnumValue"]
1128+
GraphQLEnumValueMap: TypeAlias = Dict[str, "GraphQLEnumValue"]
11251129

11261130

11271131
class GraphQLEnumTypeKwargs(GraphQLNamedTypeKwargs, total=False):
@@ -1381,7 +1385,7 @@ def __copy__(self) -> GraphQLEnumValue: # pragma: no cover
13811385
return self.__class__(**self.to_kwargs())
13821386

13831387

1384-
GraphQLInputFieldMap = Dict[str, "GraphQLInputField"]
1388+
GraphQLInputFieldMap: TypeAlias = Dict[str, "GraphQLInputField"]
13851389
GraphQLInputFieldOutType = Callable[[Dict[str, Any]], Any]
13861390

13871391

@@ -1698,7 +1702,7 @@ def assert_non_null_type(type_: Any) -> GraphQLNonNull:
16981702
GraphQLList,
16991703
)
17001704

1701-
GraphQLNullableType = Union[
1705+
GraphQLNullableType: TypeAlias = Union[
17021706
GraphQLScalarType,
17031707
GraphQLObjectType,
17041708
GraphQLInterfaceType,
@@ -1747,7 +1751,7 @@ def get_nullable_type(
17471751

17481752
graphql_input_types = (GraphQLScalarType, GraphQLEnumType, GraphQLInputObjectType)
17491753

1750-
GraphQLInputType = Union[
1754+
GraphQLInputType: TypeAlias = Union[
17511755
GraphQLScalarType, GraphQLEnumType, GraphQLInputObjectType, GraphQLWrappingType
17521756
]
17531757

@@ -1774,7 +1778,7 @@ def assert_input_type(type_: Any) -> GraphQLInputType:
17741778
GraphQLEnumType,
17751779
)
17761780

1777-
GraphQLOutputType = Union[
1781+
GraphQLOutputType: TypeAlias = Union[
17781782
GraphQLScalarType,
17791783
GraphQLObjectType,
17801784
GraphQLInterfaceType,
@@ -1798,11 +1802,11 @@ def assert_output_type(type_: Any) -> GraphQLOutputType:
17981802

17991803
# These named types do not include modifiers like List or NonNull.
18001804

1801-
GraphQLNamedInputType = Union[
1805+
GraphQLNamedInputType: TypeAlias = Union[
18021806
GraphQLScalarType, GraphQLEnumType, GraphQLInputObjectType
18031807
]
18041808

1805-
GraphQLNamedOutputType = Union[
1809+
GraphQLNamedOutputType: TypeAlias = Union[
18061810
GraphQLScalarType,
18071811
GraphQLObjectType,
18081812
GraphQLInterfaceType,
@@ -1845,7 +1849,7 @@ def get_named_type(type_: Optional[GraphQLType]) -> Optional[GraphQLNamedType]:
18451849

18461850
graphql_leaf_types = (GraphQLScalarType, GraphQLEnumType)
18471851

1848-
GraphQLLeafType = Union[GraphQLScalarType, GraphQLEnumType]
1852+
GraphQLLeafType: TypeAlias = Union[GraphQLScalarType, GraphQLEnumType]
18491853

18501854

18511855
def is_leaf_type(type_: Any) -> TypeGuard[GraphQLLeafType]:
@@ -1862,7 +1866,9 @@ def assert_leaf_type(type_: Any) -> GraphQLLeafType:
18621866

18631867
graphql_composite_types = (GraphQLObjectType, GraphQLInterfaceType, GraphQLUnionType)
18641868

1865-
GraphQLCompositeType = Union[GraphQLObjectType, GraphQLInterfaceType, GraphQLUnionType]
1869+
GraphQLCompositeType: TypeAlias = Union[
1870+
GraphQLObjectType, GraphQLInterfaceType, GraphQLUnionType
1871+
]
18661872

18671873

18681874
def is_composite_type(type_: Any) -> TypeGuard[GraphQLCompositeType]:
@@ -1879,7 +1885,7 @@ def assert_composite_type(type_: Any) -> GraphQLType:
18791885

18801886
graphql_abstract_types = (GraphQLInterfaceType, GraphQLUnionType)
18811887

1882-
GraphQLAbstractType = Union[GraphQLInterfaceType, GraphQLUnionType]
1888+
GraphQLAbstractType: TypeAlias = Union[GraphQLInterfaceType, GraphQLUnionType]
18831889

18841890

18851891
def is_abstract_type(type_: Any) -> TypeGuard[GraphQLAbstractType]:

src/graphql/type/schema.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@
2828
except ImportError: # Python < 3.8
2929
from typing_extensions import TypedDict
3030
try:
31-
from typing import TypeGuard
31+
from typing import TypeAlias, TypeGuard
3232
except ImportError: # Python < 3.10
33-
from typing_extensions import TypeGuard
33+
from typing_extensions import TypeAlias, TypeGuard
3434

3535

3636
__all__ = ["GraphQLSchema", "GraphQLSchemaKwargs", "is_schema", "assert_schema"]
3737

3838

39-
TypeMap = Dict[str, GraphQLNamedType]
39+
TypeMap: TypeAlias = Dict[str, GraphQLNamedType]
4040

4141

4242
class InterfaceImplementations(NamedTuple):

0 commit comments

Comments
 (0)