Skip to content

Commit 39a2294

Browse files
committed
Time, Geography: remove pytz and other Python < 3.9 support
1 parent 4cdfafc commit 39a2294

File tree

6 files changed

+7
-125
lines changed

6 files changed

+7
-125
lines changed

plugins/Geography/plugin.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,6 @@ def timezone_from_uri(irc, uri):
4949
format(_("Could not understand timezone: %s"), e.args[0]),
5050
Raise=True,
5151
)
52-
except utils.time.MissingTimezoneLibrary:
53-
irc.error(
54-
_(
55-
"Timezone-related commands are not available. "
56-
"Your administrator need to either upgrade Python to "
57-
"version 3.9 or greater, or install pytz."
58-
),
59-
Raise=True,
60-
)
6152
except utils.time.TimezoneException as e:
6253
irc.error(e.args[0], Raise=True)
6354

@@ -160,10 +151,6 @@ def timezone(self, irc, msg, args, query):
160151
# instance of zoneinfo.ZoneInfo
161152
irc.reply(format("%s (currently %s)", timezone.key, offset))
162153
return
163-
elif hasattr(timezone, "zone"):
164-
# instance of pytz.timezone
165-
irc.reply(format("%s (currently %s)", timezone.zone, offset))
166-
return
167154
else:
168155
# probably datetime.timezone built from a constant offset
169156
try:

plugins/Geography/test.py

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,9 @@
3030

3131
import datetime
3232
import functools
33-
import contextlib
3433
from unittest import skipIf
3534
from unittest.mock import patch
3635

37-
try:
38-
import pytz
39-
except ImportError:
40-
pytz = None
41-
4236
try:
4337
import zoneinfo
4438
except ImportError:
@@ -64,35 +58,6 @@ def newf(self):
6458
class GeographyTimezoneTestCase(PluginTestCase):
6559
plugins = ("Geography",)
6660

67-
@skipIf(not pytz, "pytz is not available")
68-
@mock
69-
def testTimezonePytz(self):
70-
tz = pytz.timezone("Europe/Paris")
71-
with patch.object(wikidata, "timezone_from_uri", return_value=tz):
72-
self.assertRegexp(
73-
"timezone Foo Bar", r"Europe/Paris \(currently UTC\+[12]\)"
74-
)
75-
76-
tz = pytz.timezone("America/New_York")
77-
with patch.object(wikidata, "timezone_from_uri", return_value=tz):
78-
self.assertRegexp(
79-
"timezone New York", r"America/New_York \(currently UTC-[45]\)"
80-
)
81-
82-
tz = pytz.timezone("America/St_Johns")
83-
with patch.object(wikidata, "timezone_from_uri", return_value=tz):
84-
self.assertRegexp(
85-
"timezone Newfoundland",
86-
r"America/St_Johns \(currently UTC-[23]:30\)",
87-
)
88-
89-
tz = pytz.timezone("Asia/Kolkata")
90-
with patch.object(wikidata, "timezone_from_uri", return_value=tz):
91-
self.assertRegexp(
92-
"timezone Delhi", r"Asia/Kolkata \(currently UTC\+5:30\)"
93-
)
94-
95-
@skipIf(not zoneinfo, "Python is older than 3.9")
9661
@mock
9762
def testTimezoneZoneinfo(self):
9863
tz = zoneinfo.ZoneInfo("Europe/Paris")
@@ -120,7 +85,6 @@ def testTimezoneZoneinfo(self):
12085
"timezone Delhi", r"Asia/Kolkata \(currently UTC\+5:30\)"
12186
)
12287

123-
@skipIf(not zoneinfo, "Python is older than 3.9")
12488
@mock
12589
def testTimezoneAbsolute(self):
12690
tz = datetime.timezone(datetime.timedelta(hours=4))
@@ -150,23 +114,13 @@ def testTimezoneIntegration(self):
150114
class GeographyLocaltimeTestCase(PluginTestCase):
151115
plugins = ("Geography",)
152116

153-
@skipIf(not pytz, "pytz is not available")
154-
@mock
155-
def testLocaltimePytz(self):
156-
tz = pytz.timezone("Europe/Paris")
157-
158-
with patch.object(wikidata, "timezone_from_uri", return_value=tz):
159-
self.assertRegexp("localtime Foo Bar", r".*\+0[12]00$")
160-
161-
@skipIf(not zoneinfo, "Python is older than 3.9")
162117
@mock
163118
def testLocaltimeZoneinfo(self):
164119
tz = zoneinfo.ZoneInfo("Europe/Paris")
165120

166121
with patch.object(wikidata, "timezone_from_uri", return_value=tz):
167122
self.assertRegexp("localtime Foo Bar", r".*\+0[12]00$")
168123

169-
@skipIf(not zoneinfo, "Python is older than 3.9")
170124
@mock
171125
def testLocaltimeAbsolute(self):
172126
tz = datetime.timezone(datetime.timedelta(hours=4))

plugins/Time/plugin.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -222,12 +222,6 @@ def tztime(self, irc, msg, args, tz):
222222
tz = utils.time.iana_timezone(tz)
223223
except utils.time.UnknownTimeZone:
224224
irc.error(_('Unknown timezone'), Raise=True)
225-
except utils.time.MissingTimezoneLibrary:
226-
irc.error(_(
227-
'Timezone-related commands are not available. '
228-
'Your administrator need to either upgrade Python to '
229-
'version 3.9 or greater, or install pytz.'),
230-
Raise=True)
231225
except utils.time.TimezoneException as e:
232226
irc.error(e.args[0], Raise=True)
233227

plugins/Time/test.py

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,6 @@
3131
import sys
3232
from supybot.test import *
3333

34-
if sys.version_info >= (3, 9):
35-
has_tz_lib = True
36-
else:
37-
try:
38-
import pytz
39-
except ImportError:
40-
has_tz_lib = False
41-
else:
42-
has_tz_lib = True
43-
4434
try:
4535
import dateutil
4636
except ImportError:
@@ -55,20 +45,7 @@
5545
else:
5646
has_ddate = True
5747

58-
try:
59-
from unittest import skipIf
60-
except ImportError: # Python 2.6
61-
def skipIf(cond, reason):
62-
if cond:
63-
print('Skipped: %s' % reason)
64-
def decorator(f):
65-
return None
66-
else:
67-
def decorator(f):
68-
return f
69-
return decorator
70-
71-
48+
from unittest import skipIf
7249

7350
class TimeTestCase(PluginTestCase):
7451
plugins = ('Time','Utilities')
@@ -88,9 +65,6 @@ def testSeconds(self):
8865
self.assertResponse('seconds 1y 1s', '31536001')
8966
self.assertResponse('seconds 1w 1s', '604801')
9067

91-
@skipIf(sys.version_info < (3, 7, 0),
92-
"Python 3.6 does not support empty pattern matches, see: "
93-
"https://docs.python.org/3/library/re.html#re.split")
9468
def testSecondsNoSpace(self):
9569
self.assertResponse('seconds 1m1s', '61')
9670
self.assertResponse('seconds 1h1s', '3601')
@@ -103,7 +77,6 @@ def testNoErrors(self):
10377
self.assertNotError('ctime')
10478
self.assertNotError('time %Y')
10579

106-
@skipIf(not has_tz_lib, 'python version is older than 3.9 and pytz is missing')
10780
def testTztime(self):
10881
self.assertNotError('tztime Europe/Paris')
10982
self.assertNotError('tztime America/Indiana/Knox')

requirements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,5 @@ pyxmpp2-scram # for the scram-sha-256 SASL mechanism
1313

1414
cryptography # required to load the Fediverse plugin (used to implement HTTP signatures to support Mastodon instances with AUTHORIZED_FETCH=true)
1515
feedparser # required to load the RSS plugin
16-
pytz;python_version<'3.9' # enables timezone manipulation in the Time and Geography plugins. On Python >=3.9, the standard library is used instead
1716
python-dateutil # enable fancy time string parsing in the Time plugin
1817
ddate # required for the ddate command in the Time plugin

src/utils/time.py

Lines changed: 6 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -29,53 +29,28 @@
2929
###
3030

3131
import re
32-
import sys
33-
34-
if sys.version_info >= (3, 9):
35-
import zoneinfo
36-
else:
37-
zoneinfo = None
38-
39-
try:
40-
import pytz
41-
except ImportError:
42-
pytz = None
32+
import zoneinfo
4333

4434
_IANA_TZ_RE = re.compile(r"([\w_-]+/)*[\w_-]+")
4535

4636
class TimezoneException(Exception):
4737
pass
4838

49-
class MissingTimezoneLibrary(TimezoneException):
50-
pass
51-
5239
class UnknownTimeZone(TimezoneException):
5340
pass
5441

5542
def iana_timezone(name):
5643
"""Returns a :class:datetime.tzinfo object, given an IANA timezone name,
5744
eg. ``"Europe/Paris"``.
5845
59-
This uses :class:``zoneinfo.ZoneInfo`` if available,
60-
:func:``pytz.timezone`` otherwise.
46+
This uses :class:``zoneinfo.ZoneInfo`.
6147
6248
May raise instances of :exc:`TimezoneException`.
6349
"""
6450
if not _IANA_TZ_RE.match(name):
6551
raise UnknownTimeZone(name)
6652

67-
if zoneinfo:
68-
try:
69-
return zoneinfo.ZoneInfo(name)
70-
except zoneinfo.ZoneInfoNotFoundError as e:
71-
raise UnknownTimeZone(e.args[0]) from None
72-
elif pytz:
73-
try:
74-
return pytz.timezone(name)
75-
except pytz.UnknownTimeZoneError as e:
76-
raise UnknownTimeZone(e.args[0]) from None
77-
else:
78-
raise MissingTimezoneLibrary(
79-
"Could not find a timezone library. "
80-
"Update Python to version 3.9 or newer, or install pytz."
81-
)
53+
try:
54+
return zoneinfo.ZoneInfo(name)
55+
except zoneinfo.ZoneInfoNotFoundError as e:
56+
raise UnknownTimeZone(e.args[0]) from None

0 commit comments

Comments
 (0)