Skip to content

Commit ddfe2bf

Browse files
authored
Enable recursive type definitions (#218)
1 parent 27e0cd0 commit ddfe2bf

File tree

4 files changed

+7
-10
lines changed

4 files changed

+7
-10
lines changed

src/graphql/language/visitor.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ class Stack(NamedTuple):
162162
idx: int
163163
keys: tuple[Node, ...]
164164
edits: list[tuple[int | str, Node]]
165-
prev: Any # 'Stack' (python/mypy/issues/731)
165+
prev: Stack
166166

167167

168168
def visit(

src/graphql/pyutils/path.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
from __future__ import annotations
44

5-
from typing import Any, NamedTuple
5+
from typing import NamedTuple
66

77
__all__ = ["Path"]
88

99

1010
class Path(NamedTuple):
1111
"""A generic path of string or integer indices"""
1212

13-
prev: Any # Optional['Path'] (python/mypy/issues/731)
13+
prev: Path | None
1414
"""path with the previous indices"""
1515
key: str | int
1616
"""current index in the path (string or integer)"""
@@ -25,7 +25,7 @@ def as_list(self) -> list[str | int]:
2525
"""Return a list of the path keys."""
2626
flattened: list[str | int] = []
2727
append = flattened.append
28-
curr: Path = self
28+
curr: Path | None = self
2929
while curr:
3030
append(curr.key)
3131
curr = curr.prev

src/graphql/validation/rules/overlapping_fields_can_be_merged.py

+1-6
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@
3838
from typing_extensions import TypeAlias
3939

4040

41-
MYPY = False
42-
4341
__all__ = ["OverlappingFieldsCanBeMergedRule"]
4442

4543

@@ -98,10 +96,7 @@ def enter_selection_set(self, selection_set: SelectionSetNode, *_args: Any) -> N
9896
# Field name and reason.
9997
ConflictReason: TypeAlias = Tuple[str, "ConflictReasonMessage"]
10098
# Reason is a string, or a nested list of conflicts.
101-
if MYPY: # recursive types not fully supported yet (/python/mypy/issues/731)
102-
ConflictReasonMessage: TypeAlias = Union[str, List]
103-
else:
104-
ConflictReasonMessage: TypeAlias = Union[str, List[ConflictReason]]
99+
ConflictReasonMessage: TypeAlias = Union[str, List[ConflictReason]]
105100
# Tuple defining a field node in a context.
106101
NodeAndDef: TypeAlias = Tuple[GraphQLCompositeType, FieldNode, Optional[GraphQLField]]
107102
# Dictionary of lists of those.

tests/execution/test_executor.py

+2
Original file line numberDiff line numberDiff line change
@@ -308,9 +308,11 @@ def resolve_type(_val, _info, _type):
308308
prev, key, typename = path
309309
assert key == "l2"
310310
assert typename == "SomeObject"
311+
assert prev is not None
311312
prev, key, typename = prev
312313
assert key == 0
313314
assert typename is None
315+
assert prev is not None
314316
prev, key, typename = prev
315317
assert key == "l1"
316318
assert typename == "SomeQuery"

0 commit comments

Comments
 (0)