Skip to content

Commit 9d06ff3

Browse files
authored
Rename the Period class to Interval (#676)
1 parent f712af4 commit 9d06ff3

16 files changed

+87
-86
lines changed

pendulum/__init__.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@
3333
from pendulum.helpers import set_locale
3434
from pendulum.helpers import week_ends_at
3535
from pendulum.helpers import week_starts_at
36+
from pendulum.interval import Interval
3637
from pendulum.parser import parse
37-
from pendulum.period import Period
3838
from pendulum.testing.traveller import Traveller
3939
from pendulum.time import Time
4040
from pendulum.tz import UTC
@@ -286,11 +286,11 @@ def duration(
286286
)
287287

288288

289-
def period(start: DateTime, end: DateTime, absolute: bool = False) -> Period:
289+
def interval(start: DateTime, end: DateTime, absolute: bool = False) -> Interval:
290290
"""
291-
Create a Period instance.
291+
Create an Interval instance.
292292
"""
293-
return Period(start, end, absolute=absolute)
293+
return Interval(start, end, absolute=absolute)
294294

295295

296296
# Testing
@@ -334,16 +334,16 @@ def period(start: DateTime, end: DateTime, absolute: bool = False) -> Period:
334334
"from_timestamp",
335335
"get_locale",
336336
"instance",
337+
"interval",
337338
"local",
338339
"locale",
339340
"naive",
340341
"now",
341-
"period",
342342
"set_locale",
343343
"week_ends_at",
344344
"week_starts_at",
345345
"parse",
346-
"Period",
346+
"Interval",
347347
"Time",
348348
"UTC",
349349
"local_timezone",

pendulum/date.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
from pendulum.constants import YEARS_PER_DECADE
2525
from pendulum.exceptions import PendulumException
2626
from pendulum.helpers import add_duration
27+
from pendulum.interval import Interval
2728
from pendulum.mixins.default import FormattableMixin
28-
from pendulum.period import Period
2929

3030

3131
class Date(FormattableMixin, date):
@@ -267,10 +267,10 @@ def __sub__(self, dt: datetime) -> NoReturn:
267267
...
268268

269269
@overload
270-
def __sub__(self, dt: Date) -> Period:
270+
def __sub__(self, dt: Date) -> Interval:
271271
...
272272

273-
def __sub__(self, other: timedelta | date) -> Date | Period:
273+
def __sub__(self, other: timedelta | date) -> Date | Interval:
274274
if isinstance(other, timedelta):
275275
return self._subtract_timedelta(other)
276276

@@ -283,17 +283,17 @@ def __sub__(self, other: timedelta | date) -> Date | Period:
283283

284284
# DIFFERENCES
285285

286-
def diff(self, dt: date | None = None, abs: bool = True) -> Period:
286+
def diff(self, dt: date | None = None, abs: bool = True) -> Interval:
287287
"""
288-
Returns the difference between two Date objects as a Period.
288+
Returns the difference between two Date objects as an Interval.
289289
290290
:param dt: The date to compare to (defaults to today)
291291
:param abs: Whether to return an absolute interval or not
292292
"""
293293
if dt is None:
294294
dt = self.today()
295295

296-
return Period(self, Date(dt.year, dt.month, dt.day), absolute=abs)
296+
return Interval(self, Date(dt.year, dt.month, dt.day), absolute=abs)
297297

298298
def diff_for_humans(
299299
self,

pendulum/datetime.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
from pendulum.date import Date
3333
from pendulum.exceptions import PendulumException
3434
from pendulum.helpers import add_duration
35-
from pendulum.period import Period
35+
from pendulum.interval import Interval
3636
from pendulum.time import Time
3737
from pendulum.tz import UTC
3838
from pendulum.tz import local_timezone
@@ -645,7 +645,7 @@ def _add_timedelta_(self, delta: datetime.timedelta) -> DateTime:
645645
"""
646646
Add timedelta duration to the instance.
647647
"""
648-
if isinstance(delta, pendulum.Period):
648+
if isinstance(delta, pendulum.Interval):
649649
return self.add(
650650
years=delta.years,
651651
months=delta.months,
@@ -678,14 +678,14 @@ def _subtract_timedelta(self, delta: datetime.timedelta) -> DateTime:
678678

679679
def diff( # type: ignore[override]
680680
self, dt: datetime.datetime | None = None, abs: bool = True
681-
) -> Period:
681+
) -> Interval:
682682
"""
683-
Returns the difference between two DateTime objects represented as a Period.
683+
Returns the difference between two DateTime objects represented as an Interval.
684684
"""
685685
if dt is None:
686686
dt = self.now(self.tz)
687687

688-
return Period(self, dt, absolute=abs)
688+
return Interval(self, dt, absolute=abs)
689689

690690
def diff_for_humans( # type: ignore[override]
691691
self,
@@ -1170,12 +1170,12 @@ def __sub__(self, other: datetime.timedelta) -> DateTime:
11701170
...
11711171

11721172
@overload
1173-
def __sub__(self, other: DateTime) -> Period:
1173+
def __sub__(self, other: DateTime) -> Interval:
11741174
...
11751175

11761176
def __sub__(
11771177
self, other: datetime.datetime | datetime.timedelta
1178-
) -> DateTime | Period:
1178+
) -> DateTime | Interval:
11791179
if isinstance(other, datetime.timedelta):
11801180
return self._subtract_timedelta(other)
11811181

@@ -1198,7 +1198,7 @@ def __sub__(
11981198

11991199
return other.diff(self, False)
12001200

1201-
def __rsub__(self, other: datetime.datetime) -> Period:
1201+
def __rsub__(self, other: datetime.datetime) -> Interval:
12021202
if not isinstance(other, datetime.datetime):
12031203
return NotImplemented
12041204

pendulum/period.py renamed to pendulum/interval.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,9 @@
2424
from pendulum.locales.locale import Locale # noqa
2525

2626

27-
class Period(Duration):
27+
class Interval(Duration):
2828
"""
29-
Duration class that is aware of the datetimes that generated the
30-
time difference.
29+
A period of time between two datetimes.
3130
"""
3231

3332
@overload
@@ -36,7 +35,7 @@ def __new__(
3635
start: pendulum.DateTime | datetime,
3736
end: pendulum.DateTime | datetime,
3837
absolute: bool = False,
39-
) -> Period:
38+
) -> Interval:
4039
...
4140

4241
@overload
@@ -45,15 +44,15 @@ def __new__(
4544
start: pendulum.Date | date,
4645
end: pendulum.Date | date,
4746
absolute: bool = False,
48-
) -> Period:
47+
) -> Interval:
4948
...
5049

5150
def __new__(
5251
cls,
5352
start: pendulum.DateTime | pendulum.Date | datetime | date,
5453
end: pendulum.DateTime | pendulum.Date | datetime | date,
5554
absolute: bool = False,
56-
) -> Period:
55+
) -> Interval:
5756
if (
5857
isinstance(start, datetime)
5958
and not isinstance(end, datetime)
@@ -126,7 +125,7 @@ def __new__(
126125

127126
delta: timedelta = _end - _start # type: ignore[operator]
128127

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

131130
def __init__(
132131
self,
@@ -339,7 +338,7 @@ def __add__(self, other: timedelta) -> Duration:
339338
def __sub__(self, other: timedelta) -> Duration:
340339
return self.as_interval().__sub__(other)
341340

342-
def __neg__(self) -> Period:
341+
def __neg__(self) -> Interval:
343342
return self.__class__(self.end, self.start, self._absolute)
344343

345344
def __mul__(self, other: int | float) -> Duration:
@@ -377,7 +376,7 @@ def __mod__(self, other: timedelta) -> Duration:
377376
def __divmod__(self, other: timedelta) -> tuple[int, Duration]:
378377
return self.as_interval().__divmod__(other)
379378

380-
def __abs__(self) -> Period:
379+
def __abs__(self) -> Interval:
381380
return self.__class__(self.start, self.end, absolute=True)
382381

383382
def __repr__(self) -> str:
@@ -390,7 +389,7 @@ def _cmp(self, other: timedelta) -> int:
390389
# Only needed for PyPy
391390
assert isinstance(other, timedelta)
392391

393-
if isinstance(other, Period):
392+
if isinstance(other, Interval):
394393
other = other.as_timedelta()
395394

396395
td = self.as_timedelta()
@@ -414,7 +413,7 @@ def _getstate(
414413
def __reduce__(
415414
self,
416415
) -> tuple[
417-
type[Period],
416+
type[Interval],
418417
tuple[
419418
pendulum.DateTime | pendulum.Date | datetime | date,
420419
pendulum.DateTime | pendulum.Date | datetime | date,
@@ -426,7 +425,7 @@ def __reduce__(
426425
def __reduce_ex__(
427426
self, protocol: SupportsIndex
428427
) -> tuple[
429-
type[Period],
428+
type[Interval],
430429
tuple[
431430
pendulum.DateTime | pendulum.Date | datetime | date,
432431
pendulum.DateTime | pendulum.Date | datetime | date,
@@ -439,7 +438,7 @@ def __hash__(self) -> int:
439438
return hash((self.start, self.end, self._absolute))
440439

441440
def __eq__(self, other: object) -> bool:
442-
if isinstance(other, Period):
441+
if isinstance(other, Interval):
443442
return (self.start, self.end, self._absolute) == (
444443
other.start,
445444
other.end,

pendulum/parser.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from pendulum.date import Date
1414
from pendulum.datetime import DateTime
1515
from pendulum.duration import Duration
16-
from pendulum.period import Period
16+
from pendulum.interval import Interval
1717
from pendulum.time import Time
1818

1919
try:
@@ -29,7 +29,7 @@ def parse(text: str, **options: t.Any) -> Date | Time | DateTime | Duration:
2929
return _parse(text, **options)
3030

3131

32-
def _parse(text: str, **options: t.Any) -> Date | DateTime | Time | Duration | Period:
32+
def _parse(text: str, **options: t.Any) -> Date | DateTime | Time | Duration | Interval:
3333
"""
3434
Parses a string with the given options.
3535
@@ -68,7 +68,7 @@ def _parse(text: str, **options: t.Any) -> Date | DateTime | Time | Duration | P
6868
if parsed.start is not None:
6969
dt = pendulum.instance(parsed.start, tz=options.get("tz", UTC))
7070

71-
return pendulum.period(
71+
return pendulum.interval(
7272
dt,
7373
dt.add(
7474
years=duration.years,
@@ -86,7 +86,7 @@ def _parse(text: str, **options: t.Any) -> Date | DateTime | Time | Duration | P
8686
t.cast(datetime.datetime, parsed.end), tz=options.get("tz", UTC)
8787
)
8888

89-
return pendulum.period(
89+
return pendulum.interval(
9090
dt.subtract(
9191
years=duration.years,
9292
months=duration.months,
@@ -100,7 +100,7 @@ def _parse(text: str, **options: t.Any) -> Date | DateTime | Time | Duration | P
100100
dt,
101101
)
102102

103-
return pendulum.period(
103+
return pendulum.interval(
104104
pendulum.instance(
105105
t.cast(datetime.datetime, parsed.start), tz=options.get("tz", UTC)
106106
),

pyproject.toml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -147,13 +147,13 @@ module = [
147147
"tests.parsing.test_parsing",
148148
"tests.parsing.test_parsing_duration",
149149
"tests.parsing.test_parse_iso8601",
150-
"tests.period.test_add_subtract",
151-
"tests.period.test_arithmetic",
152-
"tests.period.test_behavior",
153-
"tests.period.test_construct",
154-
"tests.period.test_hashing",
155-
"tests.period.test_in_words",
156-
"tests.period.test_range",
150+
"tests.interval.test_add_subtract",
151+
"tests.interval.test_arithmetic",
152+
"tests.interval.test_behavior",
153+
"tests.interval.test_construct",
154+
"tests.interval.test_hashing",
155+
"tests.interval.test_in_words",
156+
"tests.interval.test_range",
157157
"tests.time.test_add",
158158
"tests.time.test_behavior",
159159
"tests.time.test_comparison",

tests/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def assert_duration(
6262
minutes=None,
6363
seconds=None,
6464
microseconds=None,
65-
):
65+
) -> None:
6666
expected = {}
6767
actual = {}
6868

File renamed without changes.
File renamed without changes.

tests/period/test_arithmetic.py renamed to tests/interval/test_arithmetic.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88
def test_multiply():
99
dt1 = pendulum.DateTime(2016, 8, 7, 12, 34, 56)
1010
dt2 = dt1.add(days=6, seconds=34)
11-
it = pendulum.period(dt1, dt2)
11+
it = pendulum.interval(dt1, dt2)
1212
mul = it * 2
1313
assert isinstance(mul, pendulum.Duration)
1414
assert_duration(mul, 0, 0, 1, 5, 0, 1, 8)
1515

1616
dt1 = pendulum.DateTime(2016, 8, 7, 12, 34, 56)
1717
dt2 = dt1.add(days=6, seconds=34)
18-
it = pendulum.period(dt1, dt2)
18+
it = pendulum.interval(dt1, dt2)
1919
mul = it * 2
2020
assert isinstance(mul, pendulum.Duration)
2121
assert_duration(mul, 0, 0, 1, 5, 0, 1, 8)
@@ -24,14 +24,14 @@ def test_multiply():
2424
def test_divide():
2525
dt1 = pendulum.DateTime(2016, 8, 7, 12, 34, 56)
2626
dt2 = dt1.add(days=2, seconds=34)
27-
it = pendulum.period(dt1, dt2)
27+
it = pendulum.interval(dt1, dt2)
2828
mul = it / 2
2929
assert isinstance(mul, pendulum.Duration)
3030
assert_duration(mul, 0, 0, 0, 1, 0, 0, 17)
3131

3232
dt1 = pendulum.DateTime(2016, 8, 7, 12, 34, 56)
3333
dt2 = dt1.add(days=2, seconds=35)
34-
it = pendulum.period(dt1, dt2)
34+
it = pendulum.interval(dt1, dt2)
3535
mul = it / 2
3636
assert isinstance(mul, pendulum.Duration)
3737
assert_duration(mul, 0, 0, 0, 1, 0, 0, 17)
@@ -40,14 +40,14 @@ def test_divide():
4040
def test_floor_divide():
4141
dt1 = pendulum.DateTime(2016, 8, 7, 12, 34, 56)
4242
dt2 = dt1.add(days=2, seconds=34)
43-
it = pendulum.period(dt1, dt2)
43+
it = pendulum.interval(dt1, dt2)
4444
mul = it // 2
4545
assert isinstance(mul, pendulum.Duration)
4646
assert_duration(mul, 0, 0, 0, 1, 0, 0, 17)
4747

4848
dt1 = pendulum.DateTime(2016, 8, 7, 12, 34, 56)
4949
dt2 = dt1.add(days=2, seconds=35)
50-
it = pendulum.period(dt1, dt2)
50+
it = pendulum.interval(dt1, dt2)
5151
mul = it // 3
5252
assert isinstance(mul, pendulum.Duration)
5353
assert_duration(mul, 0, 0, 0, 0, 16, 0, 11)

0 commit comments

Comments
 (0)