From c4623c44765905301c59adc9c0fffb83fc2fc1b2 Mon Sep 17 00:00:00 2001 From: Bibo-Joshi <22366557+Bibo-Joshi@users.noreply.github.com> Date: Fri, 10 May 2024 22:23:19 +0200 Subject: [PATCH] Make `Birthdate.to_date` Return a `datetime.date` Object (#4251) Co-authored-by: Harshil <37377066+harshil21@users.noreply.github.com> --- telegram/_birthdate.py | 12 ++++++++---- tests/test_birthdate.py | 8 ++++---- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/telegram/_birthdate.py b/telegram/_birthdate.py index 23c3ebc47..828590803 100644 --- a/telegram/_birthdate.py +++ b/telegram/_birthdate.py @@ -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 @@ -70,19 +70,23 @@ class Birthdate(TelegramObject): 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] diff --git a/tests/test_birthdate.py b/tests/test_birthdate.py index 4c028661a..a5e90d413 100644 --- a/tests/test_birthdate.py +++ b/tests/test_birthdate.py @@ -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 @@ -72,10 +72,10 @@ class TestBirthdateWithoutRequest(TestBirthdateBase): 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)