Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/graphql/pyutils/undefined.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import Any
import warnings
from typing import Any, Optional


__all__ = ["Undefined", "UndefinedType"]
Expand All @@ -7,6 +8,18 @@
class UndefinedType(ValueError):
"""Auxiliary class for creating the Undefined singleton."""

_instance: Optional["UndefinedType"] = None

def __new__(cls) -> "UndefinedType":
if cls._instance is None:
cls._instance = super().__new__(cls)
else:
warnings.warn("Redefinition of 'Undefined'", RuntimeWarning, stacklevel=2)
return cls._instance

def __reduce__(self) -> str:
return "Undefined"

def __repr__(self) -> str:
return "Undefined"

Expand Down
17 changes: 17 additions & 0 deletions src/graphql/type/definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,23 @@ class GraphQLNamedType(GraphQLType):
ast_node: Optional[TypeDefinitionNode]
extension_ast_nodes: Tuple[TypeExtensionNode, ...]

reserved_types: Dict[str, "GraphQLNamedType"] = {}

def __new__(cls, name: str, *_args: Any, **_kwargs: Any) -> "GraphQLNamedType":
if name in cls.reserved_types:
raise TypeError(f"Redefinition of reserved type {name!r}")
return super().__new__(cls)

def __reduce__(self) -> Tuple[Callable, Tuple]:
return self._get_instance, (self.name, tuple(self.to_kwargs().items()))

@classmethod
def _get_instance(cls, name: str, args: Tuple) -> "GraphQLNamedType":
try:
return cls.reserved_types[name]
except KeyError:
return cls(**dict(args))

def __init__(
self,
name: str,
Expand Down
Loading