Skip to content

Commit 681be2d

Browse files
committed
Improve more typing
1 parent 9c4e45e commit 681be2d

File tree

9 files changed

+122
-58
lines changed

9 files changed

+122
-58
lines changed

pendulum/date.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
from datetime import timedelta
99
from typing import TYPE_CHECKING
1010
from typing import NoReturn
11-
from typing import TypeVar
1211
from typing import cast
1312
from typing import overload
1413

@@ -33,9 +32,6 @@
3332
from typing_extensions import Self
3433

3534

36-
_D = TypeVar("_D", bound="Date")
37-
38-
3935
class Date(FormattableMixin, date):
4036
# Names of days of the week
4137
_days = {
@@ -275,7 +271,7 @@ def __sub__(self, __dt: datetime) -> NoReturn:
275271
...
276272

277273
@overload
278-
def __sub__(self: _D, __dt: _D) -> Interval:
274+
def __sub__(self, __dt: Self) -> Interval:
279275
...
280276

281277
def __sub__(self, other: timedelta | date) -> Self | Interval:

pendulum/testing/traveller.py

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,17 @@
99
if TYPE_CHECKING:
1010
from types import TracebackType
1111

12+
from typing_extensions import Self
13+
1214

1315
class BaseTraveller:
1416
def __init__(self, datetime_class: type[DateTime] = DateTime) -> None:
1517
self._datetime_class: type[DateTime] = datetime_class
1618

17-
def freeze(self: BaseTraveller) -> BaseTraveller:
19+
def freeze(self) -> Self:
1820
raise NotImplementedError()
1921

20-
def travel_back(self: BaseTraveller) -> BaseTraveller:
22+
def travel_back(self) -> Self:
2123
raise NotImplementedError()
2224

2325
def travel(
@@ -30,12 +32,23 @@ def travel(
3032
minutes: int = 0,
3133
seconds: int = 0,
3234
microseconds: int = 0,
33-
) -> BaseTraveller:
35+
) -> Self:
3436
raise NotImplementedError()
3537

36-
def travel_to(self, dt: DateTime) -> BaseTraveller:
38+
def travel_to(self, dt: DateTime, *, freeze: bool = False) -> Self:
3739
raise NotImplementedError()
3840

41+
def __enter__(self) -> Self:
42+
return self
43+
44+
def __exit__(
45+
self,
46+
exc_type: type[BaseException] | None,
47+
exc_val: BaseException | None,
48+
exc_tb: TracebackType,
49+
) -> None:
50+
...
51+
3952

4053
if not PYPY:
4154
import time_machine
@@ -48,7 +61,7 @@ def __init__(self, datetime_class: type[DateTime] = DateTime) -> None:
4861
self._traveller: time_machine.travel | None = None
4962
self._coordinates: time_machine.Coordinates | None = None
5063

51-
def freeze(self) -> Traveller:
64+
def freeze(self) -> Self:
5265
if self._started:
5366
cast(time_machine.Coordinates, self._coordinates).move_to(
5467
self._datetime_class.now(), tick=False
@@ -58,7 +71,7 @@ def freeze(self) -> Traveller:
5871

5972
return self
6073

61-
def travel_back(self) -> Traveller:
74+
def travel_back(self) -> Self:
6275
if not self._started:
6376
return self
6477

@@ -81,7 +94,7 @@ def travel(
8194
microseconds: int = 0,
8295
*,
8396
freeze: bool = False,
84-
) -> Traveller:
97+
) -> Self:
8598
self._start(freeze=freeze)
8699

87100
cast(time_machine.Coordinates, self._coordinates).move_to(
@@ -99,7 +112,7 @@ def travel(
99112

100113
return self
101114

102-
def travel_to(self, dt: DateTime, *, freeze: bool = False) -> Traveller:
115+
def travel_to(self, dt: DateTime, *, freeze: bool = False) -> Self:
103116
self._start(freeze=freeze)
104117

105118
cast(time_machine.Coordinates, self._coordinates).move_to(dt)
@@ -119,7 +132,7 @@ def _start(self, freeze: bool = False) -> None:
119132

120133
self._started = True
121134

122-
def __enter__(self) -> Traveller:
135+
def __enter__(self) -> Self:
123136
self._start()
124137

125138
return self

pendulum/tz/__init__.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from pathlib import Path
44
from typing import cast
5+
from typing import overload
56

67
from pendulum.tz.local_timezone import get_local_timezone
78
from pendulum.tz.local_timezone import set_local_timezone
@@ -30,6 +31,21 @@ def timezones() -> tuple[str, ...]:
3031
return _timezones
3132

3233

34+
@overload
35+
def timezone(name: int) -> FixedTimezone:
36+
...
37+
38+
39+
@overload
40+
def timezone(name: str) -> Timezone:
41+
...
42+
43+
44+
@overload
45+
def timezone(name: str | int) -> Timezone | FixedTimezone:
46+
...
47+
48+
3349
def timezone(name: str | int) -> Timezone | FixedTimezone:
3450
"""
3551
Return a Timezone instance given its name.

poetry.lock

Lines changed: 13 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ pre-commit = "^2.20.0"
5353
[tool.poetry.group.typing.dependencies]
5454
mypy = "^1.0.1"
5555
types-python-dateutil = "^2.8.19"
56+
types-pytz = ">=2022.7.1.2"
5657

5758
[tool.poetry.group.dev.dependencies]
5859
babel = "^2.10.3"
@@ -111,9 +112,6 @@ exclude = [
111112
[[tool.mypy.overrides]]
112113
module = [
113114
"pendulum.mixins.default",
114-
"tests.conftest",
115-
"tests.test_helpers",
116-
"tests.test_main",
117115
"tests.test_parsing",
118116
"tests.date.test_add",
119117
"tests.date.test_behavior",

tests/conftest.py

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
from __future__ import annotations
22

3+
from typing import TYPE_CHECKING
4+
35
import pytest
46

57
import pendulum
68

9+
if TYPE_CHECKING:
10+
from collections.abc import Iterator
11+
712

813
@pytest.fixture(autouse=True)
9-
def setup():
14+
def setup() -> Iterator[None]:
1015
pendulum.set_local_timezone(pendulum.timezone("America/Toronto"))
1116

1217
yield
@@ -18,8 +23,15 @@ def setup():
1823

1924

2025
def assert_datetime(
21-
d, year, month, day, hour=None, minute=None, second=None, microsecond=None
22-
):
26+
d: pendulum.DateTime,
27+
year: int,
28+
month: int,
29+
day: int,
30+
hour: int | None = None,
31+
minute: int | None = None,
32+
second: int | None = None,
33+
microsecond: int | None = None,
34+
) -> None:
2335
assert year == d.year
2436
assert month == d.month
2537
assert day == d.day
@@ -37,13 +49,19 @@ def assert_datetime(
3749
assert microsecond == d.microsecond
3850

3951

40-
def assert_date(d, year, month, day):
52+
def assert_date(d: pendulum.Date, year: int, month: int, day: int) -> None:
4153
assert year == d.year
4254
assert month == d.month
4355
assert day == d.day
4456

4557

46-
def assert_time(t, hour, minute, second, microsecond=None):
58+
def assert_time(
59+
t: pendulum.Time,
60+
hour: int,
61+
minute: int,
62+
second: int,
63+
microsecond: int | None = None,
64+
) -> None:
4765
assert hour == t.hour
4866
assert minute == t.minute
4967
assert second == t.second
@@ -53,15 +71,15 @@ def assert_time(t, hour, minute, second, microsecond=None):
5371

5472

5573
def assert_duration(
56-
dur,
57-
years=None,
58-
months=None,
59-
weeks=None,
60-
days=None,
61-
hours=None,
62-
minutes=None,
63-
seconds=None,
64-
microseconds=None,
74+
dur: pendulum.Duration,
75+
years: int | None = None,
76+
months: int | None = None,
77+
weeks: int | None = None,
78+
days: int | None = None,
79+
hours: int | None = None,
80+
minutes: int | None = None,
81+
seconds: int | None = None,
82+
microseconds: int | None = None,
6583
) -> None:
6684
expected = {}
6785
actual = {}

0 commit comments

Comments
 (0)