Skip to content

Commit 1473548

Browse files
committed
Modify gql/dsl.py to use the Self Python typing
Remove the quotes around typing when possible
1 parent 257b60f commit 1473548

1 file changed

Lines changed: 39 additions & 33 deletions

File tree

‎gql/dsl.py‎

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import logging
77
import re
8+
import sys
89
from abc import ABC, abstractmethod
910
from math import isfinite
1011
from typing import (
@@ -83,6 +84,11 @@
8384
from .graphql_request import GraphQLRequest
8485
from .utils import to_camel_case
8586

87+
if sys.version_info >= (3, 11):
88+
from typing import Self # pragma: no cover
89+
else:
90+
from typing_extensions import Self # pragma: no cover
91+
8692
log = logging.getLogger(__name__)
8793

8894
_re_integer_string = re.compile("^-?(?:0|[1-9][0-9]*)$")
@@ -457,7 +463,7 @@ class DSLDirective:
457463
behavior in a GraphQL document.
458464
"""
459465

460-
def __init__(self, name: str, dsl_schema: "DSLSchema"):
466+
def __init__(self, name: str, dsl_schema: DSLSchema):
461467
r"""Initialize the DSLDirective with the given name and arguments.
462468
463469
:param name: the name of the directive
@@ -510,15 +516,15 @@ def name(self) -> str:
510516
"""Get the directive name."""
511517
return self.ast_directive.name.value
512518

513-
def __call__(self, **kwargs: Any) -> "DSLDirective":
519+
def __call__(self, **kwargs: Any) -> Self:
514520
"""Add arguments by calling the directive like a function.
515521
516522
:param kwargs: directive arguments
517523
:return: itself
518524
"""
519525
return self.args(**kwargs)
520526

521-
def args(self, **kwargs: Any) -> "DSLDirective":
527+
def args(self, **kwargs: Any) -> Self:
522528
r"""Set the arguments of a directive
523529
524530
The arguments are parsed to be stored in the AST of this field.
@@ -584,7 +590,7 @@ def __init__(self, *args, **kwargs):
584590
self._directives = ()
585591

586592
@abstractmethod
587-
def is_valid_directive(self, directive: "DSLDirective") -> bool:
593+
def is_valid_directive(self, directive: DSLDirective) -> bool:
588594
"""Check if a directive is valid for this DSL element.
589595
590596
:param directive: The DSLDirective to validate
@@ -594,7 +600,7 @@ def is_valid_directive(self, directive: "DSLDirective") -> bool:
594600
"Any DSLDirectable concrete class must have an is_valid_directive method"
595601
) # pragma: no cover
596602

597-
def directives(self, *directives: DSLDirective) -> Any:
603+
def directives(self, *directives: DSLDirective) -> Self:
598604
r"""Add directives to this DSL element.
599605
600606
:param \*directives: DSLDirective instances to add
@@ -799,23 +805,23 @@ def __repr__(self) -> str:
799805
class DSLQuery(DSLOperation):
800806
operation_type = OperationType.QUERY
801807

802-
def is_valid_directive(self, directive: "DSLDirective") -> bool:
808+
def is_valid_directive(self, directive: DSLDirective) -> bool:
803809
"""Check if directive is valid for Query operations."""
804810
return DirectiveLocation.QUERY in directive.directive_def.locations
805811

806812

807813
class DSLMutation(DSLOperation):
808814
operation_type = OperationType.MUTATION
809815

810-
def is_valid_directive(self, directive: "DSLDirective") -> bool:
816+
def is_valid_directive(self, directive: DSLDirective) -> bool:
811817
"""Check if directive is valid for Mutation operations."""
812818
return DirectiveLocation.MUTATION in directive.directive_def.locations
813819

814820

815821
class DSLSubscription(DSLOperation):
816822
operation_type = OperationType.SUBSCRIPTION
817823

818-
def is_valid_directive(self, directive: "DSLDirective") -> bool:
824+
def is_valid_directive(self, directive: DSLDirective) -> bool:
819825
"""Check if directive is valid for Subscription operations."""
820826
return DirectiveLocation.SUBSCRIPTION in directive.directive_def.locations
821827

@@ -854,16 +860,16 @@ def to_ast_type(self, type_: GraphQLInputType) -> TypeNode:
854860

855861
return NamedTypeNode(name=NameNode(value=type_.name))
856862

857-
def set_type(self, type_: GraphQLInputType) -> "DSLVariable":
863+
def set_type(self, type_: GraphQLInputType) -> Self:
858864
self.type = type_
859865
self.ast_variable_type = self.to_ast_type(type_)
860866
return self
861867

862-
def default(self, default_value: Any) -> "DSLVariable":
868+
def default(self, default_value: Any) -> Self:
863869
self.default_value = default_value
864870
return self
865871

866-
def is_valid_directive(self, directive: "DSLDirective") -> bool:
872+
def is_valid_directive(self, directive: DSLDirective) -> bool:
867873
"""Check if directive is valid for Variable definitions."""
868874
for arg in directive.ast_directive.arguments:
869875
if isinstance(arg.value, VariableNode):
@@ -894,7 +900,7 @@ def __init__(self):
894900
""":meta private:"""
895901
self.variables: Dict[str, DSLVariable] = {}
896902

897-
def __getattr__(self, name: str) -> "DSLVariable":
903+
def __getattr__(self, name: str) -> DSLVariable:
898904
"""Attributes of the DSLVariableDefinitions class are generated automatically
899905
with this dunder method in order to generate
900906
instances of :class:`DSLVariable`
@@ -1097,7 +1103,7 @@ class DSLSelectableWithAlias(DSLSelectable):
10971103

10981104
ast_field: FieldNode
10991105

1100-
def alias(self, alias: str) -> "DSLSelectableWithAlias":
1106+
def alias(self, alias: str) -> Self:
11011107
"""Set an alias
11021108
11031109
.. note::
@@ -1168,10 +1174,10 @@ def name(self):
11681174
""":meta private:"""
11691175
return self.ast_field.name.value
11701176

1171-
def __call__(self, **kwargs: Any) -> "DSLField":
1177+
def __call__(self, **kwargs: Any) -> Self:
11721178
return self.args(**kwargs)
11731179

1174-
def args(self, **kwargs: Any) -> "DSLField":
1180+
def args(self, **kwargs: Any) -> Self:
11751181
r"""Set the arguments of a field
11761182
11771183
The arguments are parsed to be stored in the AST of this field.
@@ -1217,8 +1223,8 @@ def _get_argument(self, name: str) -> GraphQLArgument:
12171223
return arg
12181224

12191225
def select(
1220-
self, *fields: "DSLSelectable", **fields_with_alias: "DSLSelectableWithAlias"
1221-
) -> "DSLField":
1226+
self, *fields: DSLSelectable, **fields_with_alias: DSLSelectableWithAlias
1227+
) -> Self:
12221228
"""Calling :meth:`select <gql.dsl.DSLSelector.select>` method with
12231229
corrected typing hints
12241230
"""
@@ -1228,14 +1234,14 @@ def select(
12281234

12291235
return self
12301236

1231-
def directives(self, *directives: DSLDirective) -> "DSLField":
1237+
def directives(self, *directives: DSLDirective) -> Self:
12321238
"""Add directives to this field."""
12331239
super().directives(*directives)
12341240
self.ast_field.directives = self.directives_ast
12351241

12361242
return self
12371243

1238-
def is_valid_directive(self, directive: "DSLDirective") -> bool:
1244+
def is_valid_directive(self, directive: DSLDirective) -> bool:
12391245
"""Check if directive is valid for Field locations."""
12401246
return DirectiveLocation.FIELD in directive.directive_def.locations
12411247

@@ -1277,7 +1283,7 @@ def __init__(self, name: str):
12771283

12781284
super().__init__(name, self.meta_type, field)
12791285

1280-
def is_valid_directive(self, directive: "DSLDirective") -> bool:
1286+
def is_valid_directive(self, directive: DSLDirective) -> bool:
12811287
"""Check if directive is valid for MetaField locations (same as Field)."""
12821288
return DirectiveLocation.FIELD in directive.directive_def.locations
12831289

@@ -1290,8 +1296,8 @@ class DSLInlineFragment(DSLSelectable, DSLFragmentSelector):
12901296

12911297
def __init__(
12921298
self,
1293-
*fields: "DSLSelectable",
1294-
**fields_with_alias: "DSLSelectableWithAlias",
1299+
*fields: DSLSelectable,
1300+
**fields_with_alias: DSLSelectableWithAlias,
12951301
):
12961302
r"""Initialize the DSLInlineFragment.
12971303
@@ -1309,8 +1315,8 @@ def __init__(
13091315
DSLDirectable.__init__(self)
13101316

13111317
def select(
1312-
self, *fields: "DSLSelectable", **fields_with_alias: "DSLSelectableWithAlias"
1313-
) -> "DSLInlineFragment":
1318+
self, *fields: DSLSelectable, **fields_with_alias: DSLSelectableWithAlias
1319+
) -> Self:
13141320
"""Calling :meth:`select <gql.dsl.DSLSelector.select>` method with
13151321
corrected typing hints
13161322
"""
@@ -1319,7 +1325,7 @@ def select(
13191325

13201326
return self
13211327

1322-
def on(self, type_condition: DSLType) -> "DSLInlineFragment":
1328+
def on(self, type_condition: DSLType) -> Self:
13231329
"""Provides the GraphQL type of this inline fragment."""
13241330

13251331
self._type = type_condition._type
@@ -1328,7 +1334,7 @@ def on(self, type_condition: DSLType) -> "DSLInlineFragment":
13281334
)
13291335
return self
13301336

1331-
def directives(self, *directives: DSLDirective) -> "DSLInlineFragment":
1337+
def directives(self, *directives: DSLDirective) -> Self:
13321338
"""Add directives to this inline fragment.
13331339
13341340
Inline fragments support all directive types through auto-validation.
@@ -1347,7 +1353,7 @@ def __repr__(self) -> str:
13471353

13481354
return f"<{self.__class__.__name__}{type_info}>"
13491355

1350-
def is_valid_directive(self, directive: "DSLDirective") -> bool:
1356+
def is_valid_directive(self, directive: DSLDirective) -> bool:
13511357
"""Check if directive is valid for Inline Fragment locations."""
13521358
return DirectiveLocation.INLINE_FRAGMENT in directive.directive_def.locations
13531359

@@ -1381,7 +1387,7 @@ def name(self) -> str:
13811387
""":meta private:"""
13821388
return self.ast_field.name.value
13831389

1384-
def directives(self, *directives: DSLDirective) -> "DSLFragmentSpread":
1390+
def directives(self, *directives: DSLDirective) -> Self:
13851391
"""Add directives to this fragment spread.
13861392
13871393
Fragment spreads support all directive types through auto-validation.
@@ -1390,7 +1396,7 @@ def directives(self, *directives: DSLDirective) -> "DSLFragmentSpread":
13901396
self.ast_field.directives = self.directives_ast
13911397
return self
13921398

1393-
def is_valid_directive(self, directive: "DSLDirective") -> bool:
1399+
def is_valid_directive(self, directive: DSLDirective) -> bool:
13941400
"""Check if directive is valid for Fragment Spread locations."""
13951401
return DirectiveLocation.FRAGMENT_SPREAD in directive.directive_def.locations
13961402

@@ -1444,8 +1450,8 @@ def spread(self) -> DSLFragmentSpread:
14441450
return DSLFragmentSpread(self)
14451451

14461452
def select(
1447-
self, *fields: "DSLSelectable", **fields_with_alias: "DSLSelectableWithAlias"
1448-
) -> "DSLFragment":
1453+
self, *fields: DSLSelectable, **fields_with_alias: DSLSelectableWithAlias
1454+
) -> Self:
14491455
"""Calling :meth:`select <gql.dsl.DSLSelector.select>` method with
14501456
corrected typing hints
14511457
"""
@@ -1458,7 +1464,7 @@ def select(
14581464

14591465
return self
14601466

1461-
def on(self, type_condition: DSLType) -> "DSLFragment":
1467+
def on(self, type_condition: DSLType) -> Self:
14621468
"""Provides the GraphQL type of this fragment.
14631469
14641470
:param type_condition: the provided type
@@ -1506,7 +1512,7 @@ def executable_ast(self) -> FragmentDefinitionNode:
15061512
directives=self.directives_ast,
15071513
)
15081514

1509-
def is_valid_directive(self, directive: "DSLDirective") -> bool:
1515+
def is_valid_directive(self, directive: DSLDirective) -> bool:
15101516
"""Check if directive is valid for Fragment Definition locations."""
15111517
return (
15121518
DirectiveLocation.FRAGMENT_DEFINITION in directive.directive_def.locations

0 commit comments

Comments
 (0)