rqltypes.py 4.15 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# copyright 2004-2021 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
#
# This file is part of rql.
#
# rql is free software: you can redistribute it and/or modify it under the
# terms of the GNU Lesser General Public License as published by the Free
# Software Foundation, either version 2.1 of the License, or (at your option)
# any later version.
#
# rql is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License along
# with rql. If not, see <http://www.gnu.org/licenses/>.
"""Types declarations for RQL types annotations"""

20
21
import sys

22
23
24
from typing import (
    TYPE_CHECKING,
    Dict,
25
    Iterator,
26
27
28
29
30
31
    List,
    Tuple,
    Union as Union_,
    Optional,
    Callable,
    Set,
32
    Type,
33
    TypeVar,
34
35
    Any,
)
36

37
if sys.version_info[0] == 3 and sys.version_info[1] >= 8:
38
    from typing import Protocol  # Python 3.8 or above
39
40
else:
    from typing_extensions import Protocol
41
42
43
44
45
46
47


__docformat__ = "restructuredtext en"

Solution = Dict[str, str]
SolutionsList = List[Solution]

48
49
# this is a node of the syntax tree
# used, for now, for Select.vargraph
50
Graph = Union_[Dict[Tuple[str, str], str], Dict[str, List[str]], Dict]
51

52
53
54
55
56

class TranslationFunction(Protocol):
    def __call__(self, msg: str, context: Optional[str] = None) -> str:
        ...

57

58
class ResolvesEType(Protocol):
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
    def __init__(self, schema, uid_func_mapping=None) -> None:
        ...

    def set_schema(self, schema) -> None:
        ...

    def solve(self, node, constraints) -> None:
        ...

    def visit(self, node, uid_func_mapping=None, kwargs=None, debug=False) -> Set[Any]:
        ...

    def visit_union(self, node) -> None:
        ...

    def visit_insert(self, node) -> None:
        ...

    def visit_delete(self, node) -> None:
        ...

    def visit_set(self, node) -> None:
        ...

    def visit_select(self, node) -> None:
        ...

    def visit_relation(self, relation, constraints) -> None:
        ...

    def visit_type_restriction(self, relation, constraints) -> None:
        ...

    def visit_and(self, et, constraints) -> None:
        ...

    def visit_or(self, ou, constraints) -> None:
        ...

    def visit_not(self, et, constraints) -> None:
        ...

    def visit_comparison(self, comparison, constraints) -> None:
        ...

    def visit_mathexpression(self, mathexpression, constraints) -> None:
        ...

    def visit_function(self, function, constraints) -> None:
        ...

    def visit_variableref(self, variableref, constraints) -> None:
        ...

    def visit_constant(self, constant, constraints) -> None:
        ...

    def visit_keyword(self, keyword, constraints) -> None:
        ...

    def visit_exists(self, exists, constraints) -> None:
        ...

122

123
124
if TYPE_CHECKING:
    import rql
125

126
127
128
    BaseNode = rql.base.BaseNode
else:
    BaseNode = "rql.base.BaseNode"
129
130

UIDFuncMapping = Optional[Dict[str, Callable[[int], str]]]
131

Nsukami Patrick's avatar
Nsukami Patrick committed
132
133
# AnyStatement is the Union of all Statement child classes
AnyStatementNode = Union_[
134
135
136
137
138
    "rql.stmts.Set",
    "rql.stmts.Delete",
    "rql.stmts.Union",
    "rql.stmts.Insert",
]
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158

P = TypeVar("P", bound="rql.base.BaseNode")


class HasRelationAttribute(Protocol):
    def relation(
        self,
    ) -> "P":
        ...


class HasHand(Protocol):
    parent: HasRelationAttribute

    def get_type(
        self,
        solution: Optional[Dict[str, str]] = None,
        kwargs: Optional[Dict] = None,
    ) -> str:
        ...
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175


Y = TypeVar("Y")


class HasOperatorAttribute(Protocol):
    operator: str

    def iget_nodes(self, klass: Type["Y"]) -> Iterator["Y"]:
        ...

    def get_type(
        self,
        solution: Optional[Dict[str, str]] = None,
        kwargs: Optional[Dict] = None,
    ) -> str:
        ...
176

177

Nsukami Patrick's avatar
Nsukami Patrick committed
178
179
# AnyScopeNode is the Union of all ScopeNode child classes
AnyScopeNode = Union_[
180
181
182
183
184
185
    "rql.stmts.Set",
    "rql.stmts.Delete",
    "rql.stmts.Insert",
    "rql.stmts.Select",
    "rql.stmts.Select",
]