Skip to content

Commit 63b7414

Browse files
committed
Update to mypy 1.0.1
1 parent 8eb0f3f commit 63b7414

File tree

13 files changed

+314
-238
lines changed

13 files changed

+314
-238
lines changed

pendulum/date.py

Lines changed: 57 additions & 52 deletions
Large diffs are not rendered by default.

pendulum/datetime.py

Lines changed: 68 additions & 77 deletions
Large diffs are not rendered by default.

pendulum/duration.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
from datetime import timedelta
4+
from typing import TYPE_CHECKING
45
from typing import cast
56
from typing import overload
67

@@ -12,6 +13,9 @@
1213
from pendulum.constants import US_PER_SECOND
1314
from pendulum.utils._compat import PYPY
1415

16+
if TYPE_CHECKING:
17+
from typing_extensions import Self
18+
1519

1620
def _divide_and_round(a: float, b: float) -> int:
1721
"""divide a by b and round result to the nearest integer
@@ -74,7 +78,7 @@ def __new__(
7478
weeks: float = 0,
7579
years: float = 0,
7680
months: float = 0,
77-
) -> Duration:
81+
) -> Self:
7882
if not isinstance(years, int) or not isinstance(months, int):
7983
raise ValueError("Float year and months are not supported")
8084

@@ -313,21 +317,21 @@ def __repr__(self) -> str:
313317

314318
return rep.replace(", )", ")")
315319

316-
def __add__(self, other: timedelta) -> Duration:
320+
def __add__(self, other: timedelta) -> Self:
317321
if isinstance(other, timedelta):
318322
return self.__class__(seconds=self.total_seconds() + other.total_seconds())
319323

320324
return NotImplemented
321325

322326
__radd__ = __add__
323327

324-
def __sub__(self, other: timedelta) -> Duration:
328+
def __sub__(self, other: timedelta) -> Self:
325329
if isinstance(other, timedelta):
326330
return self.__class__(seconds=self.total_seconds() - other.total_seconds())
327331

328332
return NotImplemented
329333

330-
def __neg__(self) -> Duration:
334+
def __neg__(self) -> Self:
331335
return self.__class__(
332336
years=-self._years,
333337
months=-self._months,
@@ -340,7 +344,7 @@ def __neg__(self) -> Duration:
340344
def _to_microseconds(self) -> int:
341345
return (self._days * (24 * 3600) + self._seconds) * 1000000 + self._microseconds
342346

343-
def __mul__(self, other: int | float) -> Duration:
347+
def __mul__(self, other: int | float) -> Self:
344348
if isinstance(other, int):
345349
return self.__class__(
346350
years=self._years * other,
@@ -363,7 +367,7 @@ def __floordiv__(self, other: timedelta) -> int:
363367
...
364368

365369
@overload
366-
def __floordiv__(self, other: int) -> Duration:
370+
def __floordiv__(self, other: int) -> Self:
367371
...
368372

369373
def __floordiv__(self, other: int | timedelta) -> int | Duration:
@@ -388,10 +392,10 @@ def __truediv__(self, other: timedelta) -> float:
388392
...
389393

390394
@overload
391-
def __truediv__(self, other: float) -> Duration:
395+
def __truediv__(self, other: float) -> Self:
392396
...
393397

394-
def __truediv__(self, other: int | float | timedelta) -> Duration | float:
398+
def __truediv__(self, other: int | float | timedelta) -> Self | float:
395399
if not isinstance(other, (int, float, timedelta)):
396400
return NotImplemented
397401

@@ -421,7 +425,7 @@ def __truediv__(self, other: int | float | timedelta) -> Duration | float:
421425

422426
__div__ = __floordiv__
423427

424-
def __mod__(self, other: timedelta) -> Duration:
428+
def __mod__(self, other: timedelta) -> Self:
425429
if isinstance(other, timedelta):
426430
r = self._to_microseconds() % other._to_microseconds() # type: ignore[attr-defined]
427431

pendulum/helpers.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454

5555
@overload
5656
def add_duration(
57-
dt: datetime,
57+
dt: _DT,
5858
years: int = 0,
5959
months: int = 0,
6060
weeks: int = 0,
@@ -63,23 +63,23 @@ def add_duration(
6363
minutes: int = 0,
6464
seconds: float = 0,
6565
microseconds: int = 0,
66-
) -> datetime:
66+
) -> _DT:
6767
...
6868

6969

7070
@overload
7171
def add_duration(
72-
dt: date,
72+
dt: _D,
7373
years: int = 0,
7474
months: int = 0,
7575
weeks: int = 0,
7676
days: int = 0,
77-
) -> date:
77+
) -> _D:
7878
pass
7979

8080

8181
def add_duration(
82-
dt: date | datetime,
82+
dt: _D,
8383
years: int = 0,
8484
months: int = 0,
8585
weeks: int = 0,
@@ -88,7 +88,7 @@ def add_duration(
8888
minutes: int = 0,
8989
seconds: float = 0,
9090
microseconds: int = 0,
91-
) -> date | datetime:
91+
) -> _D:
9292
"""
9393
Adds a duration to a date/datetime instance.
9494
"""

pendulum/interval.py

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from pendulum.helpers import precise_diff
1919

2020
if TYPE_CHECKING:
21+
from typing_extensions import Self
2122
from typing_extensions import SupportsIndex
2223

2324
from pendulum.helpers import PreciseDiff
@@ -35,7 +36,7 @@ def __new__(
3536
start: pendulum.DateTime | datetime,
3637
end: pendulum.DateTime | datetime,
3738
absolute: bool = False,
38-
) -> Interval:
39+
) -> Self:
3940
...
4041

4142
@overload
@@ -44,15 +45,15 @@ def __new__(
4445
start: pendulum.Date | date,
4546
end: pendulum.Date | date,
4647
absolute: bool = False,
47-
) -> Interval:
48+
) -> Self:
4849
...
4950

5051
def __new__(
5152
cls,
5253
start: pendulum.DateTime | pendulum.Date | datetime | date,
5354
end: pendulum.DateTime | pendulum.Date | datetime | date,
5455
absolute: bool = False,
55-
) -> Interval:
56+
) -> Self:
5657
if (
5758
isinstance(start, datetime)
5859
and not isinstance(end, datetime)
@@ -125,7 +126,7 @@ def __new__(
125126

126127
delta: timedelta = _end - _start # type: ignore[operator]
127128

128-
return cast(Interval, super().__new__(cls, seconds=delta.total_seconds()))
129+
return super().__new__(cls, seconds=delta.total_seconds())
129130

130131
def __init__(
131132
self,
@@ -316,9 +317,9 @@ def range(
316317

317318
i += amount
318319

319-
def as_interval(self) -> Duration:
320+
def as_duration(self) -> Duration:
320321
"""
321-
Return the Period as a Duration.
322+
Return the Interval as a Duration.
322323
"""
323324
return Duration(seconds=self.total_seconds())
324325

@@ -330,23 +331,23 @@ def __contains__(
330331
) -> bool:
331332
return self.start <= item <= self.end
332333

333-
def __add__(self, other: timedelta) -> Duration:
334-
return self.as_interval().__add__(other)
334+
def __add__(self, other: timedelta) -> Duration: # type: ignore[override]
335+
return self.as_duration().__add__(other)
335336

336-
__radd__ = __add__
337+
__radd__ = __add__ # type: ignore[assignment]
337338

338-
def __sub__(self, other: timedelta) -> Duration:
339-
return self.as_interval().__sub__(other)
339+
def __sub__(self, other: timedelta) -> Duration: # type: ignore[override]
340+
return self.as_duration().__sub__(other)
340341

341-
def __neg__(self) -> Interval:
342+
def __neg__(self) -> Self:
342343
return self.__class__(self.end, self.start, self._absolute)
343344

344-
def __mul__(self, other: int | float) -> Duration:
345-
return self.as_interval().__mul__(other)
345+
def __mul__(self, other: int | float) -> Duration: # type: ignore[override]
346+
return self.as_duration().__mul__(other)
346347

347-
__rmul__ = __mul__
348+
__rmul__ = __mul__ # type: ignore[assignment]
348349

349-
@overload
350+
@overload # type: ignore[override]
350351
def __floordiv__(self, other: timedelta) -> int:
351352
...
352353

@@ -355,11 +356,11 @@ def __floordiv__(self, other: int) -> Duration:
355356
...
356357

357358
def __floordiv__(self, other: int | timedelta) -> int | Duration:
358-
return self.as_interval().__floordiv__(other)
359+
return self.as_duration().__floordiv__(other)
359360

360361
__div__ = __floordiv__ # type: ignore[assignment]
361362

362-
@overload
363+
@overload # type: ignore[override]
363364
def __truediv__(self, other: timedelta) -> float:
364365
...
365366

@@ -368,15 +369,15 @@ def __truediv__(self, other: float) -> Duration:
368369
...
369370

370371
def __truediv__(self, other: float | timedelta) -> Duration | float:
371-
return self.as_interval().__truediv__(other)
372+
return self.as_duration().__truediv__(other)
372373

373-
def __mod__(self, other: timedelta) -> Duration:
374-
return self.as_interval().__mod__(other)
374+
def __mod__(self, other: timedelta) -> Duration: # type: ignore[override]
375+
return self.as_duration().__mod__(other)
375376

376377
def __divmod__(self, other: timedelta) -> tuple[int, Duration]:
377-
return self.as_interval().__divmod__(other)
378+
return self.as_duration().__divmod__(other)
378379

379-
def __abs__(self) -> Interval:
380+
def __abs__(self) -> Self:
380381
return self.__class__(self.start, self.end, absolute=True)
381382

382383
def __repr__(self) -> str:
@@ -413,7 +414,7 @@ def _getstate(
413414
def __reduce__(
414415
self,
415416
) -> tuple[
416-
type[Interval],
417+
type[Self],
417418
tuple[
418419
pendulum.DateTime | pendulum.Date | datetime | date,
419420
pendulum.DateTime | pendulum.Date | datetime | date,
@@ -425,7 +426,7 @@ def __reduce__(
425426
def __reduce_ex__(
426427
self, protocol: SupportsIndex
427428
) -> tuple[
428-
type[Interval],
429+
type[Self],
429430
tuple[
430431
pendulum.DateTime | pendulum.Date | datetime | date,
431432
pendulum.DateTime | pendulum.Date | datetime | date,
@@ -445,4 +446,4 @@ def __eq__(self, other: object) -> bool:
445446
other._absolute,
446447
)
447448
else:
448-
return self.as_interval() == other
449+
return self.as_duration() == other

pendulum/parsing/_iso8601.pyi

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ from datetime import datetime
55
from datetime import time
66

77
class Duration:
8-
98
years: int = 0
109
months: int = 0
1110
weeks: int = 0

pendulum/time.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
if TYPE_CHECKING:
2222
from typing_extensions import Literal
23+
from typing_extensions import Self
2324
from typing_extensions import SupportsIndex
2425

2526

@@ -45,7 +46,7 @@ def __repr__(self) -> str:
4546

4647
# Comparisons
4748

48-
def closest(self, dt1: Time | time, dt2: Time | time) -> Time:
49+
def closest(self, dt1: Time | time, dt2: Time | time) -> Self:
4950
"""
5051
Get the closest time from the instance.
5152
"""
@@ -57,7 +58,7 @@ def closest(self, dt1: Time | time, dt2: Time | time) -> Time:
5758

5859
return dt2
5960

60-
def farthest(self, dt1: Time | time, dt2: Time | time) -> Time:
61+
def farthest(self, dt1: Time | time, dt2: Time | time) -> Self:
6162
"""
6263
Get the farthest time from the instance.
6364
"""
@@ -257,7 +258,7 @@ def replace(
257258
microsecond: int | None = None,
258259
tzinfo: bool | datetime.tzinfo | Literal[True] | None = True,
259260
fold: int = 0,
260-
) -> Time:
261+
) -> Self:
261262
if tzinfo is True:
262263
tzinfo = self.tzinfo
263264

0 commit comments

Comments
 (0)