From 2953dd946e77e261fd0249578fce99ea0b34a0cf Mon Sep 17 00:00:00 2001 From: Hinrich Mahler <22366557+Bibo-Joshi@users.noreply.github.com> Date: Fri, 10 May 2024 21:41:13 +0200 Subject: [PATCH 1/3] Make `Birthdate.to_date` Return a `datetime.date` Object --- 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 23c3ebc4764..217721448d8 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 @@ 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. + .. versionadded:: 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 datetime 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 4c028661ac8..a5e90d413f8 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 @@ 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) From 5943b363f1d38c01286fc118b8817f4116472ee0 Mon Sep 17 00:00:00 2001 From: Bibo-Joshi <22366557+Bibo-Joshi@users.noreply.github.com> Date: Fri, 10 May 2024 21:54:40 +0200 Subject: [PATCH 2/3] Update telegram/_birthdate.py Co-authored-by: Harshil <37377066+harshil21@users.noreply.github.com> --- telegram/_birthdate.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/telegram/_birthdate.py b/telegram/_birthdate.py index 217721448d8..dc155504e13 100644 --- a/telegram/_birthdate.py +++ b/telegram/_birthdate.py @@ -82,7 +82,7 @@ def to_date(self, year: Optional[int] = None) -> date: present. Returns: - :obj:`datetime.date`: 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( From dc0114d22501439d7ae7ae90ddfb35ed4d3bdd9e Mon Sep 17 00:00:00 2001 From: Hinrich Mahler <22366557+Bibo-Joshi@users.noreply.github.com> Date: Fri, 10 May 2024 21:55:10 +0200 Subject: [PATCH 3/3] Review --- telegram/_birthdate.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/telegram/_birthdate.py b/telegram/_birthdate.py index dc155504e13..82859080370 100644 --- a/telegram/_birthdate.py +++ b/telegram/_birthdate.py @@ -73,7 +73,7 @@ def __init__( def to_date(self, year: Optional[int] = None) -> date: """Return the birthdate as a datetime object. - .. versionadded:: NEXT.VERSION + .. versionchanged:: NEXT.VERSION Now returns a :obj:`datetime.date` object instead of a :obj:`datetime.datetime` object, as was originally intended.