Skip to content

Commit

Permalink
Make Birthdate.to_date Return a datetime.date Object (#4251)
Browse files Browse the repository at this point in the history
Co-authored-by: Harshil <37377066+harshil21@users.noreply.github.com>
  • Loading branch information
Bibo-Joshi and harshil21 committed May 10, 2024
1 parent ee6e82d commit c4623c4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
12 changes: 8 additions & 4 deletions telegram/_birthdate.py
Expand Up @@ -17,7 +17,7 @@
# You should have received a copy of the GNU Lesser Public License
# along with this program. If not, see [http://www.gnu.org/licenses/].
"""This module contains an object that represents a Telegram Birthday."""
from datetime import datetime
from datetime import date
from typing import Optional

from telegram._telegramobject import TelegramObject
Expand Down Expand Up @@ -70,19 +70,23 @@ def __init__(

self._freeze()

def to_date(self, year: Optional[int] = None) -> datetime:
def to_date(self, year: Optional[int] = None) -> date:
"""Return the birthdate as a datetime object.
.. versionchanged:: NEXT.VERSION
Now returns a :obj:`datetime.date` object instead of a :obj:`datetime.datetime` object,
as was originally intended.
Args:
year (:obj:`int`, optional): The year to use. Required, if the :attr:`year` was not
present.
Returns:
:obj:`datetime.datetime`: The birthdate as a datetime object.
:obj:`datetime.date`: The birthdate as a date object.
"""
if self.year is None and year is None:
raise ValueError(
"The `year` argument is required if the `year` attribute was not present."
)

return datetime(year or self.year, self.month, self.day) # type: ignore[arg-type]
return date(year or self.year, self.month, self.day) # type: ignore[arg-type]
8 changes: 4 additions & 4 deletions tests/test_birthdate.py
Expand Up @@ -16,7 +16,7 @@
#
# You should have received a copy of the GNU Lesser Public License
# along with this program. If not, see [http://www.gnu.org/licenses/].
from datetime import datetime
from datetime import date

import pytest

Expand Down Expand Up @@ -72,10 +72,10 @@ def test_equality(self):
assert hash(bd1) != hash(bd4)

def test_to_date(self, birthdate):
assert isinstance(birthdate.to_date(), datetime)
assert birthdate.to_date() == datetime(self.year, self.month, self.day)
assert isinstance(birthdate.to_date(), date)
assert birthdate.to_date() == date(self.year, self.month, self.day)
new_bd = birthdate.to_date(2023)
assert new_bd == datetime(2023, self.month, self.day)
assert new_bd == date(2023, self.month, self.day)

def test_to_date_no_year(self):
bd = Birthdate(1, 1)
Expand Down

0 comments on commit c4623c4

Please sign in to comment.