Skip to content

Commit 42328a6

Browse files
committed
backport: Solve issues with pickled schemas (#173)
1 parent dd4d5a1 commit 42328a6

21 files changed

+1035
-534
lines changed

pyproject.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,14 @@ target-version = [
6060
[tool.pyright]
6161
# silence pyright since we're using mypy already
6262
reportArgumentType = false
63+
reportAssignmentType = false
64+
reportAttributeAccessIssue = false
6365
reportIncompatibleVariableOverride = false
66+
reportInvalidTypeForm = false
6467
reportMissingModuleSource = false
6568
reportMissingTypeArgument = false
6669
reportReturnType = false
70+
reportTypedDictNotRequiredAccess = false
6771
reportUnknownArgumentType = false
6872
reportUnknownMemberType = false
6973
reportUnknownParameterType = false

src/graphql/pyutils/undefined.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,24 @@
1-
from typing import Any
1+
import warnings
2+
from typing import Any, Optional
23

34
__all__ = ["Undefined", "UndefinedType"]
45

56

67
class UndefinedType(ValueError):
78
"""Auxiliary class for creating the Undefined singleton."""
89

10+
_instance: Optional["UndefinedType"] = None
11+
12+
def __new__(cls) -> "UndefinedType":
13+
if cls._instance is None:
14+
cls._instance = super().__new__(cls)
15+
else:
16+
warnings.warn("Redefinition of 'Undefined'", RuntimeWarning, stacklevel=2)
17+
return cls._instance
18+
19+
def __reduce__(self) -> str:
20+
return "Undefined"
21+
922
def __repr__(self) -> str:
1023
return "Undefined"
1124

src/graphql/type/definition.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,23 @@ class GraphQLNamedType(GraphQLType):
229229
ast_node: Optional[TypeDefinitionNode]
230230
extension_ast_nodes: Tuple[TypeExtensionNode, ...]
231231

232+
reserved_types: Dict[str, "GraphQLNamedType"] = {}
233+
234+
def __new__(cls, name: str, *_args: Any, **_kwargs: Any) -> "GraphQLNamedType":
235+
if name in cls.reserved_types:
236+
raise TypeError(f"Redefinition of reserved type {name!r}")
237+
return super().__new__(cls)
238+
239+
def __reduce__(self) -> Tuple[Callable, Tuple]:
240+
return self._get_instance, (self.name, tuple(self.to_kwargs().items()))
241+
242+
@classmethod
243+
def _get_instance(cls, name: str, args: Tuple) -> "GraphQLNamedType":
244+
try:
245+
return cls.reserved_types[name]
246+
except KeyError:
247+
return cls(**dict(args))
248+
232249
def __init__(
233250
self,
234251
name: str,

0 commit comments

Comments
 (0)