Debian Codegraph @

src/graphql/pyutils/undefined.py

raw
1 import warnings
2 from typing import Any, Optional
3
4 __all__ = ["Undefined", "UndefinedType"]
5
6
7 class UndefinedType(ValueError):
8 """Auxiliary class for creating the Undefined singleton."""
9
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
22 def __repr__(self) -> str:
23 return "Undefined"
24
25 __str__ = __repr__
26
27 def __hash__(self) -> int:
28 return hash(UndefinedType)
29
30 def __bool__(self) -> bool:
31 return False
32
33 def __eq__(self, other: Any) -> bool:
34 return other is Undefined
35
36 def __ne__(self, other: Any) -> bool:
37 return not self == other
38
39
40 # Used to indicate undefined or invalid values (like "undefined" in JavaScript):
41 Undefined = UndefinedType()
42
43 Undefined.__doc__ = """Symbol for undefined values
44
45 This singleton object is used to describe undefined or invalid values.
46 It can be used in places where you would use ``undefined`` in GraphQL.js.
47 """