7
7
from typing import Dict
8
8
9
9
import pytest
10
- import pytz
10
+ import zoneinfo
11
11
12
12
from pydantic_core import SchemaError , SchemaValidator , ValidationError , core_schema , validate_core_schema
13
13
@@ -81,8 +81,8 @@ def test_datetime_strict(input_value, expected):
81
81
82
82
83
83
def test_keep_tz ():
84
- tz = pytz . timezone ('Europe/London' )
85
- dt = tz . localize ( datetime (2022 , 6 , 14 , 12 , 13 , 14 ) )
84
+ tz = zoneinfo . ZoneInfo ('Europe/London' )
85
+ dt = datetime (2022 , 6 , 14 , 12 , 13 , 14 , tzinfo = tz )
86
86
v = SchemaValidator ({'type' : 'datetime' })
87
87
88
88
output = v .validate_python (dt )
@@ -94,8 +94,8 @@ def test_keep_tz():
94
94
95
95
96
96
def test_keep_tz_bound ():
97
- tz = pytz . timezone ('Europe/London' )
98
- dt = tz . localize ( datetime (2022 , 6 , 14 , 12 , 13 , 14 ) )
97
+ tz = zoneinfo . ZoneInfo ('Europe/London' )
98
+ dt = datetime (2022 , 6 , 14 , 12 , 13 , 14 , tzinfo = tz )
99
99
v = SchemaValidator ({'type' : 'datetime' , 'gt' : datetime (2022 , 1 , 1 )})
100
100
101
101
output = v .validate_python (dt )
@@ -106,7 +106,7 @@ def test_keep_tz_bound():
106
106
assert output .tzinfo .dst (datetime (2022 , 1 , 1 )) == timedelta (0 )
107
107
108
108
with pytest .raises (ValidationError , match = r'Input should be greater than 2022-01-01T00:00:00 \[type=greater_than' ):
109
- v .validate_python (tz . localize ( datetime (2021 , 6 , 14 ) ))
109
+ v .validate_python (datetime (2021 , 6 , 14 , tzinfo = tz ))
110
110
111
111
112
112
@pytest .mark .parametrize (
@@ -186,8 +186,8 @@ def test_custom_timezone_utc_repr():
186
186
187
187
188
188
def test_tz_comparison ():
189
- tz = pytz . timezone ('Europe/London' )
190
- uk_3pm = tz . localize ( datetime (2022 , 1 , 1 , 15 , 0 , 0 ) )
189
+ tz = zoneinfo . ZoneInfo ('Europe/London' )
190
+ uk_3pm = datetime (2022 , 1 , 1 , 15 , 0 , 0 , tzinfo = tz )
191
191
192
192
# two times are the same instant, therefore le and ge are both ok
193
193
v = SchemaValidator ({'type' : 'datetime' , 'le' : uk_3pm }).validate_python ('2022-01-01T16:00:00+01:00' )
@@ -322,22 +322,22 @@ def test_datetime_past_timezone():
322
322
now_utc = datetime .now (timezone .utc ) - timedelta (seconds = 1 )
323
323
assert v .isinstance_python (now_utc )
324
324
# "later" in the day
325
- assert v .isinstance_python (now_utc .astimezone (pytz . timezone ('Europe/Istanbul' )))
325
+ assert v .isinstance_python (now_utc .astimezone (zoneinfo . ZoneInfo ('Europe/Istanbul' )))
326
326
# "earlier" in the day
327
- assert v .isinstance_python (now_utc .astimezone (pytz . timezone ('America/Los_Angeles' )))
327
+ assert v .isinstance_python (now_utc .astimezone (zoneinfo . ZoneInfo ('America/Los_Angeles' )))
328
328
329
329
soon_utc = now_utc + timedelta (minutes = 1 )
330
330
assert not v .isinstance_python (soon_utc )
331
331
332
332
# "later" in the day
333
- assert not v .isinstance_python (soon_utc .astimezone (pytz . timezone ('Europe/Istanbul' )))
333
+ assert not v .isinstance_python (soon_utc .astimezone (zoneinfo . ZoneInfo ('Europe/Istanbul' )))
334
334
# "earlier" in the day
335
- assert not v .isinstance_python (soon_utc .astimezone (pytz . timezone ('America/Los_Angeles' )))
335
+ assert not v .isinstance_python (soon_utc .astimezone (zoneinfo . ZoneInfo ('America/Los_Angeles' )))
336
336
337
337
# input value is timezone naive, so we do a dumb comparison in these terms the istanbul time is later so fails
338
338
# wile the LA time is earlier so passes
339
- assert not v .isinstance_python (soon_utc .astimezone (pytz . timezone ('Europe/Istanbul' )).replace (tzinfo = None ))
340
- assert v .isinstance_python (soon_utc .astimezone (pytz . timezone ('America/Los_Angeles' )).replace (tzinfo = None ))
339
+ assert not v .isinstance_python (soon_utc .astimezone (zoneinfo . ZoneInfo ('Europe/Istanbul' )).replace (tzinfo = None ))
340
+ assert v .isinstance_python (soon_utc .astimezone (zoneinfo . ZoneInfo ('America/Los_Angeles' )).replace (tzinfo = None ))
341
341
342
342
343
343
@pytest .mark .parametrize (
@@ -368,17 +368,17 @@ def test_datetime_future_timezone():
368
368
assert v .isinstance_python (soon_utc )
369
369
370
370
# "later" in the day
371
- assert v .isinstance_python (soon_utc .astimezone (pytz . timezone ('Europe/Istanbul' )))
371
+ assert v .isinstance_python (soon_utc .astimezone (zoneinfo . ZoneInfo ('Europe/Istanbul' )))
372
372
# "earlier" in the day
373
- assert v .isinstance_python (soon_utc .astimezone (pytz . timezone ('America/Los_Angeles' )))
373
+ assert v .isinstance_python (soon_utc .astimezone (zoneinfo . ZoneInfo ('America/Los_Angeles' )))
374
374
375
375
past_utc = now_utc - timedelta (minutes = 1 )
376
376
assert not v .isinstance_python (past_utc )
377
377
378
378
# "later" in the day
379
- assert not v .isinstance_python (past_utc .astimezone (pytz . timezone ('Europe/Istanbul' )))
379
+ assert not v .isinstance_python (past_utc .astimezone (zoneinfo . ZoneInfo ('Europe/Istanbul' )))
380
380
# "earlier" in the day
381
- assert not v .isinstance_python (past_utc .astimezone (pytz . timezone ('America/Los_Angeles' )))
381
+ assert not v .isinstance_python (past_utc .astimezone (zoneinfo . ZoneInfo ('America/Los_Angeles' )))
382
382
383
383
384
384
def test_mock_utc_offset_8_hours (mocker ):
0 commit comments