-
-
Notifications
You must be signed in to change notification settings - Fork 138
/
Copy pathpredicates.py
103 lines (76 loc) · 3.18 KB
/
predicates.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
"""Predicates for GraphQL nodes"""
from __future__ import annotations
from .ast import (
DefinitionNode,
ExecutableDefinitionNode,
ListValueNode,
Node,
NullabilityAssertionNode,
ObjectValueNode,
SchemaExtensionNode,
SelectionNode,
TypeDefinitionNode,
TypeExtensionNode,
TypeNode,
TypeSystemDefinitionNode,
ValueNode,
VariableNode,
)
try:
from typing import TypeGuard
except ImportError: # Python < 3.10
from typing_extensions import TypeGuard
__all__ = [
"is_const_value_node",
"is_definition_node",
"is_executable_definition_node",
"is_nullability_assertion_node",
"is_selection_node",
"is_type_definition_node",
"is_type_extension_node",
"is_type_node",
"is_type_system_definition_node",
"is_type_system_extension_node",
"is_value_node",
]
def is_definition_node(node: Node) -> TypeGuard[DefinitionNode]:
"""Check whether the given node represents a definition."""
return isinstance(node, DefinitionNode)
def is_executable_definition_node(node: Node) -> TypeGuard[ExecutableDefinitionNode]:
"""Check whether the given node represents an executable definition."""
return isinstance(node, ExecutableDefinitionNode)
def is_selection_node(node: Node) -> TypeGuard[SelectionNode]:
"""Check whether the given node represents a selection."""
return isinstance(node, SelectionNode)
def is_nullability_assertion_node(node: Node) -> TypeGuard[NullabilityAssertionNode]:
"""Check whether the given node represents a nullability assertion node."""
return isinstance(node, NullabilityAssertionNode)
def is_value_node(node: Node) -> TypeGuard[ValueNode]:
"""Check whether the given node represents a value."""
return isinstance(node, ValueNode)
def is_const_value_node(node: Node) -> TypeGuard[ValueNode]:
"""Check whether the given node represents a constant value."""
return is_value_node(node) and (
any(is_const_value_node(value) for value in node.values)
if isinstance(node, ListValueNode)
else any(is_const_value_node(field.value) for field in node.fields)
if isinstance(node, ObjectValueNode)
else not isinstance(node, VariableNode)
)
def is_type_node(node: Node) -> TypeGuard[TypeNode]:
"""Check whether the given node represents a type."""
return isinstance(node, TypeNode)
def is_type_system_definition_node(node: Node) -> TypeGuard[TypeSystemDefinitionNode]:
"""Check whether the given node represents a type system definition."""
return isinstance(node, TypeSystemDefinitionNode)
def is_type_definition_node(node: Node) -> TypeGuard[TypeDefinitionNode]:
"""Check whether the given node represents a type definition."""
return isinstance(node, TypeDefinitionNode)
def is_type_system_extension_node(
node: Node,
) -> TypeGuard[SchemaExtensionNode | TypeExtensionNode]:
"""Check whether the given node represents a type system extension."""
return isinstance(node, (SchemaExtensionNode, TypeExtensionNode))
def is_type_extension_node(node: Node) -> TypeGuard[TypeExtensionNode]:
"""Check whether the given node represents a type extension."""
return isinstance(node, TypeExtensionNode)