Skip to content

Commit

Permalink
Enable recursive type definitions (#218)
Browse files Browse the repository at this point in the history
  • Loading branch information
yilinjuang committed Apr 9, 2024
1 parent 27e0cd0 commit ddfe2bf
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/graphql/language/visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ class Stack(NamedTuple):
idx: int
keys: tuple[Node, ...]
edits: list[tuple[int | str, Node]]
prev: Any # 'Stack' (python/mypy/issues/731)
prev: Stack


def visit(
Expand Down
6 changes: 3 additions & 3 deletions src/graphql/pyutils/path.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

from __future__ import annotations

from typing import Any, NamedTuple
from typing import NamedTuple

__all__ = ["Path"]


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

prev: Any # Optional['Path'] (python/mypy/issues/731)
prev: Path | None
"""path with the previous indices"""
key: str | int
"""current index in the path (string or integer)"""
Expand All @@ -25,7 +25,7 @@ def as_list(self) -> list[str | int]:
"""Return a list of the path keys."""
flattened: list[str | int] = []
append = flattened.append
curr: Path = self
curr: Path | None = self
while curr:
append(curr.key)
curr = curr.prev
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@
from typing_extensions import TypeAlias


MYPY = False

__all__ = ["OverlappingFieldsCanBeMergedRule"]


Expand Down Expand Up @@ -98,10 +96,7 @@ def enter_selection_set(self, selection_set: SelectionSetNode, *_args: Any) -> N
# Field name and reason.
ConflictReason: TypeAlias = Tuple[str, "ConflictReasonMessage"]
# Reason is a string, or a nested list of conflicts.
if MYPY: # recursive types not fully supported yet (/python/mypy/issues/731)
ConflictReasonMessage: TypeAlias = Union[str, List]
else:
ConflictReasonMessage: TypeAlias = Union[str, List[ConflictReason]]
ConflictReasonMessage: TypeAlias = Union[str, List[ConflictReason]]
# Tuple defining a field node in a context.
NodeAndDef: TypeAlias = Tuple[GraphQLCompositeType, FieldNode, Optional[GraphQLField]]
# Dictionary of lists of those.
Expand Down
2 changes: 2 additions & 0 deletions tests/execution/test_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,9 +308,11 @@ def resolve_type(_val, _info, _type):
prev, key, typename = path
assert key == "l2"
assert typename == "SomeObject"
assert prev is not None
prev, key, typename = prev
assert key == 0
assert typename is None
assert prev is not None
prev, key, typename = prev
assert key == "l1"
assert typename == "SomeQuery"
Expand Down

0 comments on commit ddfe2bf

Please sign in to comment.