1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
|
From: Benjamin Drung <benjamin.drung@canonical.com>
Date: Fri, 14 Nov 2025 00:30:45 +0100
Subject: Replace pytz by zoneinfo
pytz has served the Python community well for many years, but it is no
longer the best option for providing time zones. pytz has a non-standard
interface that is very easy to misuse (see
https://blog.ganssle.io/articles/2018/03/pytz-fastest-footgun.html);
this interface was necessary when pytz was created, because datetime had
no way to represent ambiguous datetimes, but this was solved in Python
3.6, which added a fold attribute to datetimes in PEP 495. With the
addition of the zoneinfo module in Python 3.9 (PEP 615), there has never
been a better time to migrate away from pytz.
Forwarded: https://github.com/flask-restful/flask-restful/pull/989
---
flask_restful/inputs.py | 17 +++++-----
setup.py | 1 -
tests/test_fields.py | 18 +++++-----
tests/test_inputs.py | 89 ++++++++++++++++++++++++-------------------------
4 files changed, 61 insertions(+), 64 deletions(-)
diff --git a/flask_restful/inputs.py b/flask_restful/inputs.py
index 1b36c85..335844a 100644
--- a/flask_restful/inputs.py
+++ b/flask_restful/inputs.py
@@ -1,14 +1,13 @@
from calendar import timegm
-from datetime import datetime, time, timedelta
+from datetime import datetime, time, timedelta, timezone
from email.utils import parsedate_tz, mktime_tz
import re
import aniso8601
-import pytz
# Constants for upgrading date-based intervals to full datetimes.
-START_OF_DAY = time(0, 0, 0, tzinfo=pytz.UTC)
-END_OF_DAY = time(23, 59, 59, 999999, tzinfo=pytz.UTC)
+START_OF_DAY = time(0, 0, 0, tzinfo=timezone.utc)
+END_OF_DAY = time(23, 59, 59, 999999, tzinfo=timezone.utc)
# https://code.djangoproject.com/browser/django/trunk/django/core/validators.py
# basic auth added by frank
@@ -93,11 +92,11 @@ def _normalize_interval(start, end, value):
end = datetime.combine(end, START_OF_DAY)
if start.tzinfo is None:
- start = pytz.UTC.localize(start)
- end = pytz.UTC.localize(end)
+ start = start.replace(tzinfo=timezone.utc)
+ end = end.replace(tzinfo=timezone.utc)
else:
- start = start.astimezone(pytz.UTC)
- end = end.astimezone(pytz.UTC)
+ start = start.astimezone(timezone.utc)
+ end = end.astimezone(timezone.utc)
return start, end
@@ -265,7 +264,7 @@ def datetime_from_rfc822(datetime_str):
:type datetime_str: str
:return: A datetime
"""
- return datetime.fromtimestamp(mktime_tz(parsedate_tz(datetime_str)), pytz.utc)
+ return datetime.fromtimestamp(mktime_tz(parsedate_tz(datetime_str)), timezone.utc)
def datetime_from_iso8601(datetime_str):
diff --git a/setup.py b/setup.py
index 676450f..935beb3 100755
--- a/setup.py
+++ b/setup.py
@@ -9,7 +9,6 @@ from setuptools import setup, find_packages
requirements = [
'aniso8601>=0.82',
'Flask>=0.8',
- 'pytz',
]
diff --git a/tests/test_fields.py b/tests/test_fields.py
index cc6e6ee..38cab49 100644
--- a/tests/test_fields.py
+++ b/tests/test_fields.py
@@ -1,12 +1,12 @@
from decimal import Decimal
from functools import partial
-import pytz
import unittest
+import zoneinfo
from unittest.mock import Mock
from flask_restful.fields import MarshallingException
from flask_restful.utils import OrderedDict
from flask_restful import fields
-from datetime import datetime, timedelta, tzinfo
+from datetime import datetime, timedelta, timezone, tzinfo
from flask import Flask, Blueprint
#noinspection PyUnresolvedReferences
from tests import assert_equals
@@ -52,9 +52,9 @@ def test_rfc822_datetime_formatters():
(datetime(2011, 1, 1), "Sat, 01 Jan 2011 00:00:00 -0000"),
(datetime(2011, 1, 1, 23, 59, 59),
"Sat, 01 Jan 2011 23:59:59 -0000"),
- (datetime(2011, 1, 1, 23, 59, 59, tzinfo=pytz.utc),
+ (datetime(2011, 1, 1, 23, 59, 59, tzinfo=timezone.utc),
"Sat, 01 Jan 2011 23:59:59 -0000"),
- (datetime(2011, 1, 1, 23, 59, 59, tzinfo=pytz.timezone('CET')),
+ (datetime(2011, 1, 1, 23, 59, 59, tzinfo=zoneinfo.ZoneInfo('Africa/Lagos')),
"Sat, 01 Jan 2011 22:59:59 -0000")
]
for date_obj, expected in dates:
@@ -68,11 +68,11 @@ def test_iso8601_datetime_formatters():
"2011-01-01T23:59:59"),
(datetime(2011, 1, 1, 23, 59, 59, 1000),
"2011-01-01T23:59:59.001000"),
- (datetime(2011, 1, 1, 23, 59, 59, tzinfo=pytz.utc),
+ (datetime(2011, 1, 1, 23, 59, 59, tzinfo=timezone.utc),
"2011-01-01T23:59:59+00:00"),
- (datetime(2011, 1, 1, 23, 59, 59, 1000, tzinfo=pytz.utc),
+ (datetime(2011, 1, 1, 23, 59, 59, 1000, tzinfo=timezone.utc),
"2011-01-01T23:59:59.001000+00:00"),
- (datetime(2011, 1, 1, 23, 59, 59, tzinfo=pytz.timezone('CET')),
+ (datetime(2011, 1, 1, 23, 59, 59, tzinfo=zoneinfo.ZoneInfo('Africa/Lagos')),
"2011-01-01T23:59:59+01:00")
]
for date_obj, expected in dates:
@@ -363,7 +363,7 @@ class FieldsTestCase(unittest.TestCase):
self.assertEqual("Mon, 22 Aug 2011 20:58:45 -0000", field.output("bar", obj))
def test_rfc822_date_field_with_offset(self):
- obj = {"bar": datetime(2011, 8, 22, 20, 58, 45, tzinfo=pytz.timezone('CET'))}
+ obj = {"bar": datetime(2011, 8, 22, 20, 58, 45, tzinfo=zoneinfo.ZoneInfo('Africa/Lagos'))}
field = fields.DateTime()
self.assertEqual("Mon, 22 Aug 2011 19:58:45 -0000", field.output("bar", obj))
@@ -373,7 +373,7 @@ class FieldsTestCase(unittest.TestCase):
self.assertEqual("2011-08-22T20:58:45", field.output("bar", obj))
def test_iso8601_date_field_with_offset(self):
- obj = {"bar": datetime(2011, 8, 22, 20, 58, 45, tzinfo=pytz.timezone('CET'))}
+ obj = {"bar": datetime(2011, 8, 22, 20, 58, 45, tzinfo=zoneinfo.ZoneInfo('Africa/Lagos'))}
field = fields.DateTime(dt_format='iso8601')
self.assertEqual("2011-08-22T20:58:45+01:00", field.output("bar", obj))
diff --git a/tests/test_inputs.py b/tests/test_inputs.py
index a1f0557..d1e7f70 100644
--- a/tests/test_inputs.py
+++ b/tests/test_inputs.py
@@ -1,6 +1,5 @@
-from datetime import datetime, timedelta, tzinfo
+from datetime import datetime, timedelta, timezone, tzinfo
import unittest
-import pytz
import re
#noinspection PyUnresolvedReferences
@@ -12,9 +11,9 @@ from tests import assert_equal
def test_reverse_rfc822_datetime():
dates = [
- ("Sat, 01 Jan 2011 00:00:00 -0000", datetime(2011, 1, 1, tzinfo=pytz.utc)),
- ("Sat, 01 Jan 2011 23:59:59 -0000", datetime(2011, 1, 1, 23, 59, 59, tzinfo=pytz.utc)),
- ("Sat, 01 Jan 2011 21:59:59 -0200", datetime(2011, 1, 1, 23, 59, 59, tzinfo=pytz.utc)),
+ ("Sat, 01 Jan 2011 00:00:00 -0000", datetime(2011, 1, 1, tzinfo=timezone.utc)),
+ ("Sat, 01 Jan 2011 23:59:59 -0000", datetime(2011, 1, 1, 23, 59, 59, tzinfo=timezone.utc)),
+ ("Sat, 01 Jan 2011 21:59:59 -0200", datetime(2011, 1, 1, 23, 59, 59, tzinfo=timezone.utc)),
]
for date_string, expected in dates:
@@ -23,10 +22,10 @@ def test_reverse_rfc822_datetime():
def test_reverse_iso8601_datetime():
dates = [
- ("2011-01-01T00:00:00+00:00", datetime(2011, 1, 1, tzinfo=pytz.utc)),
- ("2011-01-01T23:59:59+00:00", datetime(2011, 1, 1, 23, 59, 59, tzinfo=pytz.utc)),
- ("2011-01-01T23:59:59.001000+00:00", datetime(2011, 1, 1, 23, 59, 59, 1000, tzinfo=pytz.utc)),
- ("2011-01-01T23:59:59+02:00", datetime(2011, 1, 1, 21, 59, 59, tzinfo=pytz.utc))
+ ("2011-01-01T00:00:00+00:00", datetime(2011, 1, 1, tzinfo=timezone.utc)),
+ ("2011-01-01T23:59:59+00:00", datetime(2011, 1, 1, 23, 59, 59, tzinfo=timezone.utc)),
+ ("2011-01-01T23:59:59.001000+00:00", datetime(2011, 1, 1, 23, 59, 59, 1000, tzinfo=timezone.utc)),
+ ("2011-01-01T23:59:59+02:00", datetime(2011, 1, 1, 21, 59, 59, tzinfo=timezone.utc))
]
for date_string, expected in dates:
@@ -245,64 +244,64 @@ def test_isointerval():
# Full precision with explicit UTC.
"2013-01-01T12:30:00Z/P1Y2M3DT4H5M6S",
(
- datetime(2013, 1, 1, 12, 30, 0, tzinfo=pytz.utc),
- datetime(2014, 3, 5, 16, 35, 6, tzinfo=pytz.utc),
+ datetime(2013, 1, 1, 12, 30, 0, tzinfo=timezone.utc),
+ datetime(2014, 3, 5, 16, 35, 6, tzinfo=timezone.utc),
),
),
(
# Full precision with alternate UTC indication
"2013-01-01T12:30+00:00/P2D",
(
- datetime(2013, 1, 1, 12, 30, 0, tzinfo=pytz.utc),
- datetime(2013, 1, 3, 12, 30, 0, tzinfo=pytz.utc),
+ datetime(2013, 1, 1, 12, 30, 0, tzinfo=timezone.utc),
+ datetime(2013, 1, 3, 12, 30, 0, tzinfo=timezone.utc),
),
),
(
# Implicit UTC with time
"2013-01-01T15:00/P1M",
(
- datetime(2013, 1, 1, 15, 0, 0, tzinfo=pytz.utc),
- datetime(2013, 1, 31, 15, 0, 0, tzinfo=pytz.utc),
+ datetime(2013, 1, 1, 15, 0, 0, tzinfo=timezone.utc),
+ datetime(2013, 1, 31, 15, 0, 0, tzinfo=timezone.utc),
),
),
(
# TZ conversion
"2013-01-01T17:00-05:00/P2W",
(
- datetime(2013, 1, 1, 22, 0, 0, tzinfo=pytz.utc),
- datetime(2013, 1, 15, 22, 0, 0, tzinfo=pytz.utc),
+ datetime(2013, 1, 1, 22, 0, 0, tzinfo=timezone.utc),
+ datetime(2013, 1, 15, 22, 0, 0, tzinfo=timezone.utc),
),
),
(
# Date upgrade to midnight-midnight period
"2013-01-01/P3D",
(
- datetime(2013, 1, 1, 0, 0, 0, tzinfo=pytz.utc),
- datetime(2013, 1, 4, 0, 0, 0, 0, tzinfo=pytz.utc),
+ datetime(2013, 1, 1, 0, 0, 0, tzinfo=timezone.utc),
+ datetime(2013, 1, 4, 0, 0, 0, 0, tzinfo=timezone.utc),
),
),
(
# Start/end with UTC
"2013-01-01T12:00:00Z/2013-02-01T12:00:00Z",
(
- datetime(2013, 1, 1, 12, 0, 0, tzinfo=pytz.utc),
- datetime(2013, 2, 1, 12, 0, 0, tzinfo=pytz.utc),
+ datetime(2013, 1, 1, 12, 0, 0, tzinfo=timezone.utc),
+ datetime(2013, 2, 1, 12, 0, 0, tzinfo=timezone.utc),
),
),
(
# Start/end with time upgrade
"2013-01-01/2013-06-30",
(
- datetime(2013, 1, 1, tzinfo=pytz.utc),
- datetime(2013, 6, 30, tzinfo=pytz.utc),
+ datetime(2013, 1, 1, tzinfo=timezone.utc),
+ datetime(2013, 6, 30, tzinfo=timezone.utc),
),
),
(
# Start/end with TZ conversion
"2013-02-17T12:00:00-07:00/2013-02-28T15:00:00-07:00",
(
- datetime(2013, 2, 17, 19, 0, 0, tzinfo=pytz.utc),
- datetime(2013, 2, 28, 22, 0, 0, tzinfo=pytz.utc),
+ datetime(2013, 2, 17, 19, 0, 0, tzinfo=timezone.utc),
+ datetime(2013, 2, 28, 22, 0, 0, tzinfo=timezone.utc),
),
),
# Resolution expansion for single date(time)
@@ -310,72 +309,72 @@ def test_isointerval():
# Second with UTC
"2013-01-01T12:30:45Z",
(
- datetime(2013, 1, 1, 12, 30, 45, tzinfo=pytz.utc),
- datetime(2013, 1, 1, 12, 30, 46, tzinfo=pytz.utc),
+ datetime(2013, 1, 1, 12, 30, 45, tzinfo=timezone.utc),
+ datetime(2013, 1, 1, 12, 30, 46, tzinfo=timezone.utc),
),
),
(
# Second with tz conversion
"2013-01-01T12:30:45+02:00",
(
- datetime(2013, 1, 1, 10, 30, 45, tzinfo=pytz.utc),
- datetime(2013, 1, 1, 10, 30, 46, tzinfo=pytz.utc),
+ datetime(2013, 1, 1, 10, 30, 45, tzinfo=timezone.utc),
+ datetime(2013, 1, 1, 10, 30, 46, tzinfo=timezone.utc),
),
),
(
# Second with implicit UTC
"2013-01-01T12:30:45",
(
- datetime(2013, 1, 1, 12, 30, 45, tzinfo=pytz.utc),
- datetime(2013, 1, 1, 12, 30, 46, tzinfo=pytz.utc),
+ datetime(2013, 1, 1, 12, 30, 45, tzinfo=timezone.utc),
+ datetime(2013, 1, 1, 12, 30, 46, tzinfo=timezone.utc),
),
),
(
# Minute with UTC
"2013-01-01T12:30+00:00",
(
- datetime(2013, 1, 1, 12, 30, tzinfo=pytz.utc),
- datetime(2013, 1, 1, 12, 31, tzinfo=pytz.utc),
+ datetime(2013, 1, 1, 12, 30, tzinfo=timezone.utc),
+ datetime(2013, 1, 1, 12, 31, tzinfo=timezone.utc),
),
),
(
# Minute with conversion
"2013-01-01T12:30+04:00",
(
- datetime(2013, 1, 1, 8, 30, tzinfo=pytz.utc),
- datetime(2013, 1, 1, 8, 31, tzinfo=pytz.utc),
+ datetime(2013, 1, 1, 8, 30, tzinfo=timezone.utc),
+ datetime(2013, 1, 1, 8, 31, tzinfo=timezone.utc),
),
),
(
# Minute with implicit UTC
"2013-01-01T12:30",
(
- datetime(2013, 1, 1, 12, 30, tzinfo=pytz.utc),
- datetime(2013, 1, 1, 12, 31, tzinfo=pytz.utc),
+ datetime(2013, 1, 1, 12, 30, tzinfo=timezone.utc),
+ datetime(2013, 1, 1, 12, 31, tzinfo=timezone.utc),
),
),
(
# Hour, explicit UTC
"2013-01-01T12Z",
(
- datetime(2013, 1, 1, 12, tzinfo=pytz.utc),
- datetime(2013, 1, 1, 13, tzinfo=pytz.utc),
+ datetime(2013, 1, 1, 12, tzinfo=timezone.utc),
+ datetime(2013, 1, 1, 13, tzinfo=timezone.utc),
),
),
(
# Hour with offset
"2013-01-01T12-07:00",
(
- datetime(2013, 1, 1, 19, tzinfo=pytz.utc),
- datetime(2013, 1, 1, 20, tzinfo=pytz.utc),
+ datetime(2013, 1, 1, 19, tzinfo=timezone.utc),
+ datetime(2013, 1, 1, 20, tzinfo=timezone.utc),
),
),
(
# Hour with implicit UTC
"2013-01-01T12",
(
- datetime(2013, 1, 1, 12, tzinfo=pytz.utc),
- datetime(2013, 1, 1, 13, tzinfo=pytz.utc),
+ datetime(2013, 1, 1, 12, tzinfo=timezone.utc),
+ datetime(2013, 1, 1, 13, tzinfo=timezone.utc),
),
),
(
@@ -383,8 +382,8 @@ def test_isointerval():
# be accepted.
"2013-01-01T12:00:00.0/2013-01-01T12:30:00.000000",
(
- datetime(2013, 1, 1, 12, tzinfo=pytz.utc),
- datetime(2013, 1, 1, 12, 30, tzinfo=pytz.utc),
+ datetime(2013, 1, 1, 12, tzinfo=timezone.utc),
+ datetime(2013, 1, 1, 12, 30, tzinfo=timezone.utc),
),
),
]
|