Make Birthdate.to_date Return a datetime.date Object (#4251)

Co-authored-by: Harshil <37377066+harshil21@users.noreply.github.com>
This commit is contained in:
Bibo-Joshi 2024-05-10 22:23:19 +02:00 committed by GitHub
parent ee6e82d7ad
commit c4623c4476
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 12 additions and 8 deletions

View file

@ -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]

View file

@ -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)