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

Commit ec6786c

Browse files
author
Sergey Vasilyev
committed
Annotate magic methods with predictable result types
1 parent ce09a5c commit ec6786c

File tree

5 files changed

+25
-25
lines changed

5 files changed

+25
-25
lines changed

Diff for: data_diff/databases/base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -895,7 +895,7 @@ class QueryResult:
895895
def __iter__(self):
896896
return iter(self.rows)
897897

898-
def __len__(self):
898+
def __len__(self) -> int:
899899
return len(self.rows)
900900

901901
def __getitem__(self, i):

Diff for: data_diff/lexicographic_space.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def __init__(self, dims: Vector) -> None:
7070
super().__init__()
7171
self.dims = dims
7272

73-
def __contains__(self, v: Vector):
73+
def __contains__(self, v: Vector) -> bool:
7474
return all(0 <= i < d for i, d in safezip(v, self.dims))
7575

7676
def add(self, v1: Vector, v2: Vector) -> Vector:
@@ -138,7 +138,7 @@ def __init__(self, min_bound: Vector, max_bound: Vector) -> None:
138138

139139
self.uspace = LexicographicSpace(dims)
140140

141-
def __contains__(self, p: Vector):
141+
def __contains__(self, p: Vector) -> bool:
142142
return all(mn <= i < mx for i, mn, mx in safezip(p, self.min_bound, self.max_bound))
143143

144144
def to_uspace(self, v: Vector) -> Vector:

Diff for: data_diff/queries/base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
@attrs.define(frozen=True)
77
class _SKIP:
8-
def __repr__(self):
8+
def __repr__(self) -> str:
99
return "SKIP"
1010

1111

Diff for: data_diff/utils.py

+15-15
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ def range(self, other: "ArithUUID", count: int) -> List[Self]:
161161
checkpoints = split_space(self.uuid.int, other.uuid.int, count)
162162
return [attrs.evolve(self, uuid=i) for i in checkpoints]
163163

164-
def __int__(self):
164+
def __int__(self) -> int:
165165
return self.uuid.int
166166

167167
def __add__(self, other: int) -> Self:
@@ -255,16 +255,16 @@ def __attrs_post_init__(self) -> None:
255255
# def int(self):
256256
# return alphanumToNumber(self._str, alphanums)
257257

258-
def __str__(self):
258+
def __str__(self) -> str:
259259
s = self._str
260260
if self._max_len:
261261
s = s.rjust(self._max_len, alphanums[0])
262262
return s
263263

264-
def __len__(self):
264+
def __len__(self) -> int:
265265
return len(self._str)
266266

267-
def __repr__(self):
267+
def __repr__(self) -> str:
268268
return f'alphanum"{self._str}"'
269269

270270
def __add__(self, other: "Union[ArithAlphanumeric, int]") -> Self:
@@ -289,17 +289,17 @@ def __sub__(self, other: "Union[ArithAlphanumeric, int]") -> float:
289289

290290
return NotImplemented
291291

292-
def __ge__(self, other):
292+
def __ge__(self, other) -> bool:
293293
if not isinstance(other, type(self)):
294294
return NotImplemented
295295
return self._str >= other._str
296296

297-
def __lt__(self, other):
297+
def __lt__(self, other) -> bool:
298298
if not isinstance(other, type(self)):
299299
return NotImplemented
300300
return self._str < other._str
301301

302-
def __eq__(self, other):
302+
def __eq__(self, other) -> bool:
303303
if not isinstance(other, type(self)):
304304
return NotImplemented
305305
return self._str == other._str
@@ -424,32 +424,32 @@ class Vector(tuple):
424424
Partial implementation: Only the needed functionality is implemented
425425
"""
426426

427-
def __lt__(self, other: "Vector"):
427+
def __lt__(self, other: "Vector") -> bool:
428428
if isinstance(other, Vector):
429429
return all(a < b for a, b in safezip(self, other))
430430
return NotImplemented
431431

432-
def __le__(self, other: "Vector"):
432+
def __le__(self, other: "Vector") -> bool:
433433
if isinstance(other, Vector):
434434
return all(a <= b for a, b in safezip(self, other))
435435
return NotImplemented
436436

437-
def __gt__(self, other: "Vector"):
437+
def __gt__(self, other: "Vector") -> bool:
438438
if isinstance(other, Vector):
439439
return all(a > b for a, b in safezip(self, other))
440440
return NotImplemented
441441

442-
def __ge__(self, other: "Vector"):
442+
def __ge__(self, other: "Vector") -> bool:
443443
if isinstance(other, Vector):
444444
return all(a >= b for a, b in safezip(self, other))
445445
return NotImplemented
446446

447-
def __eq__(self, other: "Vector"):
447+
def __eq__(self, other: "Vector") -> bool:
448448
if isinstance(other, Vector):
449449
return all(a == b for a, b in safezip(self, other))
450450
return NotImplemented
451451

452-
def __sub__(self, other: "Vector"):
452+
def __sub__(self, other: "Vector") -> "Vector":
453453
if isinstance(other, Vector):
454454
return Vector((a - b) for a, b in safezip(self, other))
455455
raise NotImplementedError()
@@ -575,12 +575,12 @@ class UnknownMeta(type):
575575
def __instancecheck__(self, instance):
576576
return instance is Unknown
577577

578-
def __repr__(self):
578+
def __repr__(self) -> str:
579579
return "Unknown"
580580

581581

582582
class Unknown(metaclass=UnknownMeta):
583-
def __nonzero__(self):
583+
def __bool__(self) -> bool:
584584
raise TypeError()
585585

586586
def __new__(class_, *args, **kwargs):

Diff for: tests/test_database_types.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ def __iter__(self):
407407
step = timedelta(seconds=3, microseconds=571)
408408
return islice(chain(self.MANUAL_FAKES, accumulate(repeat(step), initial=initial)), self.max)
409409

410-
def __len__(self):
410+
def __len__(self) -> int:
411411
return self.max
412412

413413

@@ -423,7 +423,7 @@ def __iter__(self):
423423
step = 1
424424
return islice(chain(self.MANUAL_FAKES, accumulate(repeat(step), initial=initial)), self.max)
425425

426-
def __len__(self):
426+
def __len__(self) -> int:
427427
return self.max
428428

429429

@@ -437,7 +437,7 @@ def __init__(self, max) -> None:
437437
def __iter__(self):
438438
return iter(self.MANUAL_FAKES[: self.max])
439439

440-
def __len__(self):
440+
def __len__(self) -> int:
441441
return min(self.max, len(self.MANUAL_FAKES))
442442

443443

@@ -470,7 +470,7 @@ def __iter__(self):
470470
step = 0.00571
471471
return islice(chain(self.MANUAL_FAKES, accumulate(repeat(step), initial=initial)), self.max)
472472

473-
def __len__(self):
473+
def __len__(self) -> int:
474474
return self.max
475475

476476

@@ -479,7 +479,7 @@ def __init__(self, max) -> None:
479479
super().__init__()
480480
self.max = max
481481

482-
def __len__(self):
482+
def __len__(self) -> int:
483483
return self.max
484484

485485
def __iter__(self):
@@ -498,7 +498,7 @@ def __init__(self, max) -> None:
498498
def __iter__(self):
499499
return iter(self.MANUAL_FAKES[: self.max])
500500

501-
def __len__(self):
501+
def __len__(self) -> int:
502502
return min(self.max, len(self.MANUAL_FAKES))
503503

504504

0 commit comments

Comments
 (0)