8000 Make `Birthdate.to_date` Return a `datetime.date` Object (#4251) · gtkacz/python-telegram-bot@c4623c4 · GitHub
[go: up one dir, main page]

Skip to content

Commit c4623c4

Browse files
Make Birthdate.to_date Return a datetime.date Object (python-telegram-bot#4251)
Co-authored-by: Harshil <37377066+harshil21@users.noreply.github.com>
1 parent ee6e82d commit c4623c4

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

telegram/_birthdate.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
# You should have received a copy of the GNU Lesser Public License
1818
# along with this program. If not, see [http://www.gnu.org/licenses/].
1919
"""This module contains an object that represents a Telegram Birthday."""
20-
from datetime import datetime
20+
from datetime import date
2121
from typing import Optional
2222

2323
from telegram._telegramobject import TelegramObject
@@ -70,19 +70,23 @@ def __init__(
7070

7171
self._freeze()
7272

73-
def to_date(self, year: Optional[int] = None) -> datetime:
73+
def to_date(self, year: Optional[int] = None) -> date:
7474
"""Return the birthdate as a datetime object.
7575
76+
.. versionchanged:: NEXT.VERSION
77+
Now returns a :obj:`datetime.date` object instead of a :obj:`datetime.datetime` object,
78+
as was originally intended.
79+
7680
Args:
7781
year (:obj:`int`, optional): The year to use. Required, if the :attr:`year` was not
7882
present.
7983
8084
Returns:
81-
:obj:`datetime.datetime`: The birthdate as a datetime object.
85+
:obj:`datetime.date`: The birthdate as a date object.
8286
"""
8387
if self.year is None and year is None:
8488
raise ValueError(
8589
"The `year` argument is required if the `year` attribute was not present."
8690
)
8791

88-
return datetime(year or self.year, self.month, self.day) # type: ignore[arg-type]
92+
return date(year or self.year, self.month, self.day) # type: ignore[arg-type]

tests/test_birthdate.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#
1717
# You should have received a copy of the GNU Lesser Public License
1818
# along with this program. If not, see [http://www.gnu.org/licenses/].
19-
from datetime import datetime
19+
from datetime import date
2020

2121
import pytest
2222

@@ -72,10 +72,10 @@ def test_equality(self):
7272
assert hash(bd1) != hash(bd4)
7373

7474
def test_to_date(self, birthdate):
75-
assert isinstance(birthdate.to_date(), datetime)
76-
assert birthdate.to_date() == datetime(self.year, self.month, self.day)
75+
assert isinstance(birthdate.to_date(), date)
76+
assert birthdate.to_date() == date(self.year, self.month, self.day)
7777
new_bd = birthdate.to_date(2023)
78-
assert new_bd == datetime(2023, self.month, self.day)
78+
assert new_bd == date(2023, self.month, self.day)
7979

8080
def test_to_date_no_year(self):
8181
bd = Birthdate(1, 1)

0 commit comments

Comments
 (0)
0