Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 8 additions & 4 deletions telegram/_birthdate.py
Original file line number Diff line number Diff line change
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.
harshil21 marked this conversation as resolved.
Show resolved Hide resolved

.. 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
Original file line number Diff line number Diff line change
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