Skip to content
This repository was archived by the owner on May 17, 2024. It is now read-only.

Commit 001f63f

Browse files
author
Sergey Vasilyev
committed
Annotate lazy ops and DSL magic methods
1 parent ad34a1d commit 001f63f

File tree

3 files changed

+43
-43
lines changed

3 files changed

+43
-43
lines changed

Diff for: data_diff/parse_time.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class ParseError(ValueError):
3333
UNITS_STR = ", ".join(sorted(TIME_UNITS.keys()))
3434

3535

36-
def string_similarity(a, b):
36+
def string_similarity(a, b) -> SequenceMatcher:
3737
return SequenceMatcher(None, a, b).ratio()
3838

3939

Diff for: data_diff/queries/api.py

+15-15
Original file line numberDiff line numberDiff line change
@@ -26,31 +26,31 @@ def join(*tables: ITable) -> Join:
2626
return Join(tables)
2727

2828

29-
def leftjoin(*tables: ITable):
29+
def leftjoin(*tables: ITable) -> Join:
3030
"""Left-joins a sequence of table expressions.
3131
3232
See Also: ``join()``
3333
"""
3434
return Join(tables, "LEFT")
3535

3636

37-
def rightjoin(*tables: ITable):
37+
def rightjoin(*tables: ITable) -> Join:
3838
"""Right-joins a sequence of table expressions.
3939
4040
See Also: ``join()``
4141
"""
4242
return Join(tables, "RIGHT")
4343

4444

45-
def outerjoin(*tables: ITable):
45+
def outerjoin(*tables: ITable) -> Join:
4646
"""Outer-joins a sequence of table expressions.
4747
4848
See Also: ``join()``
4949
"""
5050
return Join(tables, "FULL OUTER")
5151

5252

53-
def cte(expr: Expr, *, name: Optional[str] = None, params: Sequence[str] = None):
53+
def cte(expr: Expr, *, name: Optional[str] = None, params: Sequence[str] = None) -> Cte:
5454
"""Define a CTE"""
5555
return Cte(expr, name, params)
5656

@@ -72,48 +72,48 @@ def table(*path: str, schema: Union[dict, CaseAwareMapping] = None) -> TablePath
7272
return TablePath(path, schema)
7373

7474

75-
def or_(*exprs: Expr):
75+
def or_(*exprs: Expr) -> Union[BinBoolOp, Expr]:
7676
"""Apply OR between a sequence of boolean expressions"""
7777
exprs = args_as_tuple(exprs)
7878
if len(exprs) == 1:
7979
return exprs[0]
8080
return BinBoolOp("OR", exprs)
8181

8282

83-
def and_(*exprs: Expr):
83+
def and_(*exprs: Expr) -> Union[BinBoolOp, Expr]:
8484
"""Apply AND between a sequence of boolean expressions"""
8585
exprs = args_as_tuple(exprs)
8686
if len(exprs) == 1:
8787
return exprs[0]
8888
return BinBoolOp("AND", exprs)
8989

9090

91-
def sum_(expr: Expr):
91+
def sum_(expr: Expr) -> Func:
9292
"""Call SUM(expr)"""
9393
return Func("sum", [expr])
9494

9595

96-
def avg(expr: Expr):
96+
def avg(expr: Expr) -> Func:
9797
"""Call AVG(expr)"""
9898
return Func("avg", [expr])
9999

100100

101-
def min_(expr: Expr):
101+
def min_(expr: Expr) -> Func:
102102
"""Call MIN(expr)"""
103103
return Func("min", [expr])
104104

105105

106-
def max_(expr: Expr):
106+
def max_(expr: Expr) -> Func:
107107
"""Call MAX(expr)"""
108108
return Func("max", [expr])
109109

110110

111-
def exists(expr: Expr):
111+
def exists(expr: Expr) -> Func:
112112
"""Call EXISTS(expr)"""
113113
return Func("exists", [expr])
114114

115115

116-
def if_(cond: Expr, then: Expr, else_: Optional[Expr] = None):
116+
def if_(cond: Expr, then: Expr, else_: Optional[Expr] = None) -> CaseWhen:
117117
"""Conditional expression, shortcut to when-then-else.
118118
119119
Example:
@@ -125,7 +125,7 @@ def if_(cond: Expr, then: Expr, else_: Optional[Expr] = None):
125125
return when(cond).then(then).else_(else_)
126126

127127

128-
def when(*when_exprs: Expr):
128+
def when(*when_exprs: Expr) -> QB_When:
129129
"""Start a when-then expression
130130
131131
Example:
@@ -145,7 +145,7 @@ def when(*when_exprs: Expr):
145145
return CaseWhen([]).when(*when_exprs)
146146

147147

148-
def coalesce(*exprs):
148+
def coalesce(*exprs) -> Func:
149149
"Returns a call to COALESCE"
150150
exprs = args_as_tuple(exprs)
151151
return Func("COALESCE", exprs)
@@ -160,7 +160,7 @@ def insert_rows_in_batches(db, tbl: TablePath, rows, *, columns=None, batch_size
160160
db.query(tbl.insert_rows(batch, columns=columns))
161161

162162

163-
def current_timestamp():
163+
def current_timestamp() -> CurrentTimestamp:
164164
"""Returns CURRENT_TIMESTAMP() or NOW()"""
165165
return CurrentTimestamp()
166166

Diff for: data_diff/queries/ast_classes.py

+27-27
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def _dfs_values(self):
4545
if isinstance(v, ExprNode):
4646
yield from v._dfs_values()
4747

48-
def cast_to(self, to):
48+
def cast_to(self, to) -> "Cast":
4949
return Cast(self, to)
5050

5151

@@ -110,7 +110,7 @@ def select(self, *exprs, distinct=SKIP, optimizer_hints=SKIP, **named_exprs) ->
110110
resolve_names(self.source_table, exprs)
111111
return Select.make(self, columns=exprs, distinct=distinct, optimizer_hints=optimizer_hints)
112112

113-
def where(self, *exprs):
113+
def where(self, *exprs) -> "Select":
114114
"""Filter the rows, based on the given predicates. (aka Selection)"""
115115
exprs = args_as_tuple(exprs)
116116
exprs = _drop_skips(exprs)
@@ -120,7 +120,7 @@ def where(self, *exprs):
120120
resolve_names(self.source_table, exprs)
121121
return Select.make(self, where_exprs=exprs)
122122

123-
def order_by(self, *exprs):
123+
def order_by(self, *exprs) -> "Select":
124124
"""Order the rows lexicographically, according to the given expressions."""
125125
exprs = _drop_skips(exprs)
126126
if not exprs:
@@ -129,14 +129,14 @@ def order_by(self, *exprs):
129129
resolve_names(self.source_table, exprs)
130130
return Select.make(self, order_by_exprs=exprs)
131131

132-
def limit(self, limit: int):
132+
def limit(self, limit: int) -> "Select":
133133
"""Stop yielding rows after the given limit. i.e. take the first 'n=limit' rows"""
134134
if limit is SKIP:
135135
return self
136136

137137
return Select.make(self, limit_expr=limit)
138138

139-
def join(self, target: "ITable"):
139+
def join(self, target: "ITable") -> "Join":
140140
"""Join the current table with the target table, returning a new table containing both side-by-side.
141141
142142
When joining, it's recommended to use explicit tables names, instead of `this`, in order to avoid potential name collisions.
@@ -180,37 +180,37 @@ def group_by(self, *keys) -> "GroupBy":
180180

181181
return GroupBy(self, keys)
182182

183-
def _get_column(self, name: str):
183+
def _get_column(self, name: str) -> "Column":
184184
if self.schema:
185185
name = self.schema.get_key(name) # Get the actual name. Might be case-insensitive.
186186
return Column(self, name)
187187

188188
# def __getattr__(self, column):
189189
# return self._get_column(column)
190190

191-
def __getitem__(self, column):
191+
def __getitem__(self, column) -> "Column":
192192
if not isinstance(column, str):
193193
raise TypeError()
194194
return self._get_column(column)
195195

196-
def count(self):
196+
def count(self) -> "Select":
197197
"""SELECT count() FROM self"""
198198
return Select(self, [Count()])
199199

200-
def union(self, other: "ITable"):
200+
def union(self, other: "ITable") -> "TableOp":
201201
"""SELECT * FROM self UNION other"""
202202
return TableOp("UNION", self, other)
203203

204-
def union_all(self, other: "ITable"):
204+
def union_all(self, other: "ITable") -> "TableOp":
205205
"""SELECT * FROM self UNION ALL other"""
206206
return TableOp("UNION ALL", self, other)
207207

208-
def minus(self, other: "ITable"):
208+
def minus(self, other: "ITable") -> "TableOp":
209209
"""SELECT * FROM self EXCEPT other"""
210210
# aka
211211
return TableOp("EXCEPT", self, other)
212212

213-
def intersect(self, other: "ITable"):
213+
def intersect(self, other: "ITable") -> "TableOp":
214214
"""SELECT * FROM self INTERSECT other"""
215215
return TableOp("INTERSECT", self, other)
216216

@@ -233,51 +233,51 @@ def type(self) -> Optional[type]:
233233

234234
@attrs.define(frozen=False, eq=False)
235235
class LazyOps:
236-
def __add__(self, other):
236+
def __add__(self, other) -> "BinOp":
237237
return BinOp("+", [self, other])
238238

239-
def __sub__(self, other):
239+
def __sub__(self, other) -> "BinOp":
240240
return BinOp("-", [self, other])
241241

242-
def __neg__(self):
242+
def __neg__(self) -> "UnaryOp":
243243
return UnaryOp("-", self)
244244

245-
def __gt__(self, other):
245+
def __gt__(self, other) -> "BinBoolOp":
246246
return BinBoolOp(">", [self, other])
247247

248-
def __ge__(self, other):
248+
def __ge__(self, other) -> "BinBoolOp":
249249
return BinBoolOp(">=", [self, other])
250250

251-
def __eq__(self, other):
251+
def __eq__(self, other) -> "BinBoolOp":
252252
if other is None:
253253
return BinBoolOp("IS", [self, None])
254254
return BinBoolOp("=", [self, other])
255255

256-
def __lt__(self, other):
256+
def __lt__(self, other) -> "BinBoolOp":
257257
return BinBoolOp("<", [self, other])
258258

259-
def __le__(self, other):
259+
def __le__(self, other) -> "BinBoolOp":
260260
return BinBoolOp("<=", [self, other])
261261

262-
def __or__(self, other):
262+
def __or__(self, other) -> "BinBoolOp":
263263
return BinBoolOp("OR", [self, other])
264264

265-
def __and__(self, other):
265+
def __and__(self, other) -> "BinBoolOp":
266266
return BinBoolOp("AND", [self, other])
267267

268-
def is_distinct_from(self, other):
268+
def is_distinct_from(self, other) -> "IsDistinctFrom":
269269
return IsDistinctFrom(self, other)
270270

271-
def like(self, other):
271+
def like(self, other) -> "BinBoolOp":
272272
return BinBoolOp("LIKE", [self, other])
273273

274-
def sum(self):
274+
def sum(self) -> "Func":
275275
return Func("SUM", [self])
276276

277-
def max(self):
277+
def max(self) -> "Func":
278278
return Func("MAX", [self])
279279

280-
def min(self):
280+
def min(self) -> "Func":
281281
return Func("MIN", [self])
282282

283283

0 commit comments

Comments
 (0)