8000 Removing Python<3.9 dependencies/functionality from #4194 · gtkacz/python-telegram-bot@d979028 · GitHub
[go: up one dir, main page]

Skip to content

Commit d979028

Browse files
committed
Removing Python<3.9 dependencies/functionality from python-telegram-bot#4194
1 parent b325ae0 commit d979028

File tree

3 files changed

+91
-96
lines changed

3 files changed

+91
-96
lines changed

pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ classifiers = [
4040
]
4141
dependencies = [
4242
"httpx ~= 0.27",
43-
'backports.zoneinfo;python_version<"3.9"'
4443
]
4544

4645
[project.urls]

telegram/_business.py

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@
2020
"""This module contains the Telegram Business related classes."""
2121

2222
from datetime import date, datetime, time
23-
from sys import version_info
2423
from typing import TYPE_CHECKING, Optional, Sequence, Tuple, Union
2524

25+
import zoneinfo
26+
2627
from telegram._chat import Chat
2728
from telegram._files.location import Location
2829
from telegram._files.sticker import Sticker
@@ -32,11 +33,6 @@
3233
from telegram._utils.datetime import extract_tzinfo_from_defaults, from_timestamp
3334
from telegram._utils.types import JSONDict
3435

35-
try:
36-
import zoneinfo
37-
except ImportError:
38-
from backports import zoneinfo
39-
4036
if TYPE_CHECKING:
4137
from telegram import Bot
4238

@@ -460,7 +456,9 @@ def de_json(
460456

461457
return super().de_json(data=data, bot=bot)
462458

463-
def get_opening_hours_for_day(self, target_date: Union[date, datetime], tzinfo: Optional[zoneinfo.ZoneInfo] = None) -> Tuple[Tuple[datetime, datetime], ...]:
459+
def get_opening_hours_for_day(
460+
self, target_date: Union[date, datetime], tzinfo: Optional[zoneinfo.ZoneInfo] = None
461+
) -> Tuple[Tuple[datetime, datetime], ...]:
464462
"""
465463
Get the opening hours for a specific day.
466464
@@ -479,8 +477,8 @@ def get_opening_hours_for_day(self, target_date: Union[date, datetime], tzinfo:
479477
output = []
480478

481479
for interval in self.opening_hours:
482-
opening_time = datetime.combine(target_date, time(*interval.opening_time), tzinfo)
483-
closing_time = datetime.combine(target_date, time(*interval.closing_time), tzinfo)
480+
opening_time = datetime.combine(target_date, time(*interval.opening_time[1:]), tzinfo)
481+
closing_time = datetime.combine(target_date, time(*interval.closing_time[1:]), tzinfo)
484482

485483
output.append((opening_time, closing_time))
486484

@@ -498,16 +496,27 @@ def is_open(self, target_datetime: datetime) -> bool:
498496
Returns:
499497
bool: True, if the business is open at the given time. False otherwise.
500498
"""
499+
500+
def time_to_minutes(weekday: int, hour: int, minute: int) -> int:
501+
return (weekday * 24 * 60) + (hour * 60) + minute
502+
501503
if target_datetime.tzinfo is not None:
502504
target_datetime = target_datetime.astimezone(zoneinfo.ZoneInfo(self.time_zone_name))
503505

504-
if any(
505-
interval.opening_time[0] <= target_datetime.weekday() <= interval.closing_time[0]
506-
and interval.opening_time[1] * 60 + interval.opening_time[2]
507-
<= target_datetime.hour * 60 + target_datetime.minute
508-
< interval.closing_time[1] * 60 + interval.closing_time[2]
506+
return any(
507+
time_to_minutes(
508+
target_datetime.weekday(), target_datetime.hour, target_datetime.minute
509+
)
510+
>= time_to_minutes(*interval.opening_time)
511+
or time_to_minutes(
512+
target_datetime.weekday(), target_datetime.hour, target_datetime.minute
513+
)
514+
<= time_to_minutes(*interval.closing_time)
515+
if time_to_minutes(*interval.closing_time) < time_to_minutes(*interval.opening_time)
516+
else time_to_minutes(*interval.opening_time)
517+
<= time_to_minutes(
518+
target_datetime.weekday(), target_datetime.hour, target_datetime.minute
519+
)
520+
<= time_to_minutes(*interval.closing_time)
509521
for interval in self.opening_hours
510-
):
511-
return True
512-
513-
return False
522+
)

tests/test_business.py

Lines changed: 64 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@
1616
#
1717
# You should have received a copy of the GNU Lesser Public License
1818
# along with this program. If not, see [http://www.gnu.org/licenses/].
19-
from datetime import datetime
19+
from datetime import date, datetime
2020

2121
import pytest
22+
import zoneinfo
2223

2324
from telegram import (
2425
BusinessConnection,
@@ -35,11 +36,6 @@
3536
from telegram._utils.datetime import UTC, to_timestamp
3637
from tests.auxil.slots import mro_slots
3738

10000
38-
try:
39-
import zoneinfo
40-
except ImportError:
41-
from backports import zoneinfo
42-
4339

4440
class TestBusinessBase:
4541
id_ = "123"
@@ -118,6 +114,20 @@ def business_opening_hours():
118114
)
119115

120116

117+
@pytest.fixture(scope="module")
118+
def business_hours():
119+
tz_name = "Europe/Berlin"
120+
opening_hours = [
121+
BusinessOpeningHoursInterval(
122+
opening_minute=420, closing_minute=1320
123+
), # Monday 07:00 - 22:00
124+
BusinessOpeningHoursInterval(
125+
opening_minute=1860, closing_minute=2760
126+
), # Tuesday 07:00 - 22:00
127+
]
128+
return BusinessOpeningHours(time_zone_name=tz_name, opening_hours=opening_hours)
129+
130+
121131
class TestBusinessConnectionWithoutRequest(TestBusinessBase):
122132
def test_slots(self, business_connection):
123133
bc = business_connection
@@ -415,74 +425,51 @@ def test_equality(self):
415425

416426
assert boh1 != boh3
417427
assert hash(boh1) != hash(boh3)
418-
419-
# def test_get_opening_hours_for_day_success_without_timezone(self):
420-
# boh = BusinessOpeningHours("Europe/Berlin", [
421-
# BusinessOpeningHoursInterval(0, 60),
422-
# BusinessOpeningHoursInterval(60, 120),
423-
# BusinessOpeningHoursInterval(120, 180),
424-
# BusinessOpeningHoursInterval(180, 240),
425-
# BusinessOpeningHoursInterval(240, 300),
426-
# BusinessOpeningHoursInterval(300, 360),
427-
# BusinessOpeningHoursInterval(360, 420),
428-
# ])
429-
430-
# assert boh.get_opening_hours_for_day(datetime(2021, 1, 1, 0, 0, 0)) == (0, 60)
431-
432-
# def test_get_opening_hours_for_day_success_with_timezone(self):
433-
# boh = BusinessOpeningHours("Europe/Berlin", [
434-
# BusinessOpeningHoursInterval(0, 60),
435-
# BusinessOpeningHoursInterval(60, 120),
436-
# BusinessOpeningHoursInterval(120, 180),
437-
# BusinessOpeningHoursInterval(180, 240),
438-
# BusinessOpeningHoursInterval(240, 300),
439-
# BusinessOpeningHoursInterval(300, 360),
440-
# BusinessOpeningHoursInterval(360, 420),
441-
# ])
442-
443-
# assert boh.get_opening_hours_for_day(datetime(2021, 1, 1, 0, 0, 0), zoneinfo.ZoneInfo("Europe/Berlin")) == (0, 60)
444-
445-
def test_is_open_tznaive_closed(self):
446-
boh = BusinessOpeningHours("Europe/Berlin", [
447-
BusinessOpeningHoursInterval(0, 6969),
448-
])
449-
450-
# (0, 0, 0) -> (4, 20, 9)
451-
assert boh.is_open(datetime(2024, 6, 29, 0, 0, 0)) == False
452-
assert boh.is_open(datetime(2024, 6, 28, 21, 0, 0)) == False
453-
assert boh.is_open(datetime(2024, 6, 28, 20, 10, 0)) == False
454-
455-
def test_is_open_tznaive_open(self):
456-
boh = BusinessOpeningHours("Europe/Berlin", [
457-
BusinessOpeningHoursInterval(0, 6969),
458-
])
459-
460-
# (0, 0, 0) -> (4, 20, 9)
461-
assert boh.is_open(datetime(2024, 6, 24, 0, 0, 0)) == True
462-
assert boh.is_open(datetime(2024, 6, 28, 10, 0, 0)) == True
463-
assert boh.is_open(datetime(2024, 6, 25, 20, 8, 0)) == True
464-
465-
def test_is_open_tzaware_closed(self):
466-
boh = BusinessOpeningHours("Europe/Berlin", [
467-
BusinessOpeningHoursInterval(0, 6969),
468-
])
469-
470-
# London is 2 hours behind of Berlin
471-
tz = zoneinfo.ZoneInfo("Europe/London")
472-
473-
# (0, 0, 0) -> (4, 20, 9)
474-
assert boh.is_open(datetime(2024, 6, 23, 23, 0, 0, tzinfo=tz)) == True
475-
assert boh.is_open(datetime(2024, 6, 28, 10, 0, 0, tzinfo=tz)) == True
476-
477-
def test_is_open_tzaware_open(self):
478-
boh = BusinessOpeningHours("Europe/Berlin", [
479-
BusinessOpeningHoursInterval(0, 6969),
480-
])
481-
482-
# London is 2 hours behind of Berlin
483-
tz = zoneinfo.ZoneInfo("Europe/London")
484-
485-
# (0, 0, 0) -> (4, 20, 9)
486-
assert boh.is_open(datetime(2024, 6, 24, 2, 0, 0, tzinfo=tz)) == True
487-
assert boh.is_open(datetime(2024, 6, 28, 12, 0, 0, tzinfo=tz)) == True
488-
assert boh.is_open(datetime(2024, 6, 25, 21, 8, 0, tzinfo=tz)) == True
428+
429+
def test_get_opening_hours_for_day_success_without_timezone(
430+
self, business_hours: BusinessOpeningHours
431+
):
432+
target_date = date(2023, 7, 17) # Monday
433+
expected_tz = zoneinfo.ZoneInfo(business_hours.time_zone_name)
434+
expected_opening_time = datetime(2023, 7, 17, 7, 0, tzinfo=expected_tz)
435+
expected_closing_time = datetime(2023, 7, 17, 22, 0, tzinfo=expected_tz)
436+
437+
opening_hours_for_day = business_hours.get_opening_hours_for_day(target_date)
438+
439+
assert opening_hours_for_day[0][0] == expected_opening_time
440+
assert opening_hours_for_day[0][1] == expected_closing_time
441+
442+
def test_get_opening_hours_for_day_success_with_timezone(
443+
self, business_hours: BusinessOpeningHours
444+
):
445+
target_date = date(2023, 7, 17) # Monday
446+
test_tz = zoneinfo.ZoneInfo("UTC")
447+
expected_opening_time = datetime(2023, 7, 17, 7, 0, tzinfo=test_tz)
448+
expected_closing_time = datetime(2023, 7, 17, 22, 0, tzinfo=test_tz)
449+
450+
opening_hours_for_day = business_hours.get_opening_hours_for_day(
451+
target_date, tzinfo=test_tz
452+
)
453+
454+
assert opening_hours_for_day[0][0] == expected_opening_time
455+
assert opening_hours_for_day[0][1] == expected_closing_time
456+
457+
def test_is_open_tznaive_closed(self, business_hours: BusinessOpeningHours):
458+
test_datetime = datetime(2023, 7, 19, 12, 0) # Wednesday 12:00, tz naive
459+
assert business_hours.is_open(test_datetime) is False
460+
461+
def test_is_open_tznaive_open(self, business_hours: BusinessOpeningHours):
462+
test_datetime = datetime(2023, 7, 17, 12, 0) # Monday 12:00, tz naive
463+
assert business_hours.is_open(test_datetime) is True
464+
465+
def test_is_open_tzaware_closed(self, business_hours: BusinessOpeningHours):
466+
test_datetime = datetime(
467+
2023, 7, 19, 12, 0, tzinfo=zoneinfo.ZoneInfo("UTC")
468+
) # Wednesday 12:00 UTC
469+
assert business_hours.is_open(test_datetime) is False
470+
471+
def test_is_open_tzaware_open(self, business_hours: BusinessOpeningHours):
472+
test_datetime = datetime(
473+
2023, 7, 17, 12, 0, tzinfo=zoneinfo.ZoneInfo("UTC")
474+
) # Monday 12:00 UTC
475+
assert business_hours.is_open(test_datetime) is True

0 commit comments

Comments
 (0)
0