Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d6339b5

Browse files
committed
TST: Remove pytz as hard dependency in tests
1 parent eec4a5a commit d6339b5

File tree

2 files changed

+44
-17
lines changed

2 files changed

+44
-17
lines changed

lib/matplotlib/tests/test_axes.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import datetime
66

7-
import pytz
7+
import dateutil.tz as dutz
88

99
import numpy as np
1010
from numpy import ma
@@ -5331,8 +5331,9 @@ def test_bar_uint8():
53315331
@image_comparison(baseline_images=['date_timezone_x'], extensions=['png'])
53325332
def test_date_timezone_x():
53335333
# Tests issue 5575
5334-
time_index = [pytz.timezone('Canada/Eastern').localize(datetime.datetime(
5335-
year=2016, month=2, day=22, hour=x)) for x in range(3)]
5334+
time_index = [datetime.datetime(2016, 2, 22, hour=x,
5335+
tzinfo=dutz.gettz('Canada/Eastern'))
5336+
for x in range(3)]
53365337

53375338
# Same Timezone
53385339
fig = plt.figure(figsize=(20, 12))
@@ -5348,8 +5349,9 @@ def test_date_timezone_x():
53485349
extensions=['png'])
53495350
def test_date_timezone_y():
53505351
# Tests issue 5575
5351-
time_index = [pytz.timezone('Canada/Eastern').localize(datetime.datetime(
5352-
year=2016, month=2, day=22, hour=x)) for x in range(3)]
5352+
time_index = [datetime.datetime(2016, 2, 22, hour=x,
5353+
tzinfo=dutz.gettz('Canada/Eastern'))
5354+
for x in range(3)]
53535355

53545356
# Same Timezone
53555357
fig = plt.figure(figsize=(20, 12))
@@ -5366,8 +5368,9 @@ def test_date_timezone_y():
53665368
extensions=['png'])
53675369
def test_date_timezone_x_and_y():
53685370
# Tests issue 5575
5369-
time_index = [pytz.timezone('UTC').localize(datetime.datetime(
5370-
year=2016, month=2, day=22, hour=x)) for x in range(3)]
5371+
UTC = datetime.timezone.utc
5372+
time_index = [datetime.datetime(2016, 2, 22, hour=x, tzinfo=UTC)
5373+
for x in range(3)]
53715374

53725375
# Same Timezone
53735376
fig = plt.figure(figsize=(20, 12))

lib/matplotlib/tests/test_dates.py

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,25 @@
22
import tempfile
33
from unittest.mock import Mock
44

5-
import dateutil
5+
import dateutil.tz
6+
import dateutil.rrule
67
import numpy as np
78
import pytest
8-
import pytz
99

1010
from matplotlib.testing.decorators import image_comparison
1111
import matplotlib.pyplot as plt
1212
from matplotlib.cbook import MatplotlibDeprecationWarning
1313
import matplotlib.dates as mdates
1414

1515

16+
def __has_pytz():
17+
try:
18+
import pytz
19+
return True
20+
except ImportError:
21+
return False
22+
23+
1624
def test_date_numpyx():
1725
# test that numpy dates work properly...
1826
base = datetime.datetime(2017, 1, 1)
@@ -180,8 +188,8 @@ def test_RRuleLocator():
180188

181189
def test_RRuleLocator_dayrange():
182190
loc = mdates.DayLocator()
183-
x1 = datetime.datetime(year=1, month=1, day=1, tzinfo=pytz.UTC)
184-
y1 = datetime.datetime(year=1, month=1, day=16, tzinfo=pytz.UTC)
191+
x1 = datetime.datetime(year=1, month=1, day=1, tzinfo=mdates.UTC)
192+
y1 = datetime.datetime(year=1, month=1, day=16, tzinfo=mdates.UTC)
185193
loc.tick_values(x1, y1)
186194
# On success, no overflow error shall be thrown
187195

@@ -482,8 +490,8 @@ def test_date_inverted_limit():
482490

483491
def _test_date2num_dst(date_range, tz_convert):
484492
# Timezones
485-
BRUSSELS = pytz.timezone('Europe/Brussels')
486-
UTC = pytz.UTC
493+
BRUSSELS = dateutil.tz.gettz('Europe/Brussels')
494+
UTC = mdates.UTC
487495

488496
# Create a list of timezone-aware datetime objects in UTC
489497
# Interval is 0b0.0000011 days, to prevent float rounding issues
@@ -575,10 +583,7 @@ def tz_convert(*args):
575583
_test_date2num_dst(pd.date_range, tz_convert)
576584

577585

578-
@pytest.mark.parametrize("attach_tz, get_tz", [
579-
(lambda dt, zi: zi.localize(dt), lambda n: pytz.timezone(n)),
580-
(lambda dt, zi: dt.replace(tzinfo=zi), lambda n: dateutil.tz.gettz(n))])
581-
def test_rrulewrapper(attach_tz, get_tz):
586+
def _test_rrulewrapper(attach_tz, get_tz):
582587
SYD = get_tz('Australia/Sydney')
583588

584589
dtstart = attach_tz(datetime.datetime(2017, 4, 1, 0), SYD)
@@ -593,6 +598,25 @@ def test_rrulewrapper(attach_tz, get_tz):
593598
assert act == exp
594599

595600

601+
def test_rrulewrapper():
602+
def attach_tz(dt, zi):
603+
return dt.replace(tzinfo=zi)
604+
605+
_test_rrulewrapper(attach_tz, dateutil.tz.gettz)
606+
607+
608+
@pytest.mark.pytz
609+
@pytest.mark.skipif(not __has_pytz(), reason="Requires pytz")
610+
def test_rrulewrapper_pytz():
611+
# Test to make sure pytz zones are supported in rrules
612+
import pytz
613+
614+
def attach_tz(dt, zi):
615+
return zi.localize(dt)
616+
617+
_test_rrulewrapper(attach_tz, pytz.timezone)
618+
619+
596620
def test_DayLocator():
597621
with pytest.raises(ValueError):
598622
mdates.DayLocator(interval=-1)

0 commit comments

Comments
 (0)