Skip to content

Commit

Permalink
Merge pull request #2622 from samatix/assertregex
Browse files Browse the repository at this point in the history
DEP: Replacing the assertRaisesRegexp which is deprecated by assertRaisesRegex
  • Loading branch information
Scott Sanderson committed Feb 4, 2020
2 parents 1abcb34 + 9340f38 commit b43b558
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 29 deletions.
6 changes: 4 additions & 2 deletions tests/data/bundles/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,8 +275,10 @@ def bundle_ingest_no_create_writers(*args, **kwargs):
called[0] = True

now = pd.Timestamp.utcnow()
with self.assertRaisesRegexp(ValueError,
"ingest .* creates writers .* downgrade"):
with self.assertRaisesRegex(
ValueError,
"ingest .* creates writers .* downgrade"
):
self.ingest('bundle', self.environ, assets_versions=versions,
timestamp=now - pd.Timedelta(seconds=1))
assert_false(called[0])
Expand Down
28 changes: 14 additions & 14 deletions tests/data/test_daily_bars.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,9 +336,9 @@ def test_read_known_and_unknown_sids(self):
# E.g.
# INVALID VALID INVALID VALID ... VALID INVALID
query_assets = (
[self.assets[-1] + 1] +
list(range(self.assets[0], self.assets[-1] + 1)) +
[self.assets[-1] + 3]
[self.assets[-1] + 1] +
list(range(self.assets[0], self.assets[-1] + 1)) +
[self.assets[-1] + 3]
)

columns = [CLOSE, VOLUME]
Expand Down Expand Up @@ -609,10 +609,10 @@ def test_write_attrs(self):
result = self.bcolz_daily_bar_ctable
expected_first_row = {
'1': 0,
'3': 5, # Asset 1 has 5 trading days.
'5': 12, # Asset 3 has 7 trading days.
'7': 33, # Asset 5 has 21 trading days.
'9': 44, # Asset 7 has 11 trading days.
'3': 5, # Asset 1 has 5 trading days.
'5': 12, # Asset 3 has 7 trading days.
'7': 33, # Asset 5 has 21 trading days.
'9': 44, # Asset 7 has 11 trading days.
'11': 49, # Asset 9 has 5 trading days.
}
expected_last_row = {
Expand All @@ -621,14 +621,14 @@ def test_write_attrs(self):
'5': 32,
'7': 43,
'9': 48,
'11': 57, # Asset 11 has 9 trading days.
'11': 57, # Asset 11 has 9 trading days.
}
expected_calendar_offset = {
'1': 0, # Starts on 6-01, 1st trading day of month.
'3': 15, # Starts on 6-22, 16th trading day of month.
'5': 1, # Starts on 6-02, 2nd trading day of month.
'7': 0, # Starts on 6-01, 1st trading day of month.
'9': 9, # Starts on 6-12, 10th trading day of month.
'1': 0, # Starts on 6-01, 1st trading day of month.
'3': 15, # Starts on 6-22, 16th trading day of month.
'5': 1, # Starts on 6-02, 2nd trading day of month.
'7': 0, # Starts on 6-01, 1st trading day of month.
'9': 9, # Starts on 6-12, 10th trading day of month.
'11': 10, # Starts on 6-15, 11th trading day of month.
}
self.assertEqual(result.attrs['first_row'], expected_first_row)
Expand Down Expand Up @@ -706,7 +706,7 @@ def test_missing_values_assertion(self):
"[Timestamp('2015-06-15 00:00:00+0000', tz='UTC')]\n"
"Extra sessions: []"
)
with self.assertRaisesRegexp(AssertionError, expected_msg):
with self.assertRaisesRegex(AssertionError, expected_msg):
writer.write(bar_data)


Expand Down
2 changes: 1 addition & 1 deletion tests/pipeline/test_downsampling.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ def test_yearly(self, base_terms, calendar_name):
# land prior to the first date of 2012. The downsampled terms will fail
# to request enough extra rows.
for i in range(0, 30, 5):
with self.assertRaisesRegexp(
with self.assertRaisesRegex(
NoFurtherDataError,
r'\s*Insufficient data to compute Pipeline'
):
Expand Down
2 changes: 1 addition & 1 deletion tests/pipeline/test_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ def test_bad_dates(self):
p = Pipeline()

msg = "start_date must be before or equal to end_date .*"
with self.assertRaisesRegexp(ValueError, msg):
with self.assertRaisesRegex(ValueError, msg):
self.engine.run_pipeline(p, self.dates[2], self.dates[1])

def test_fail_usefully_on_insufficient_data(self):
Expand Down
7 changes: 6 additions & 1 deletion tests/pipeline/test_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from unittest import TestCase

from mock import patch
from six import PY2

from zipline.pipeline import Factor, Filter, Pipeline
from zipline.pipeline.data import Column, DataSet, USEquityPricing
Expand Down Expand Up @@ -43,6 +44,10 @@ class SomeOtherFilter(Filter):

class PipelineTestCase(TestCase):

if PY2:
def assertRaisesRegex(self, *args, **kwargs):
return self.assertRaisesRegexp(*args, **kwargs)

def test_construction(self):
p0 = Pipeline()
self.assertEqual(p0.columns, {})
Expand Down Expand Up @@ -208,7 +213,7 @@ def mock_display_graph(g, format='svg', include_asset_exists=False):
r"but got 'fizzbuzz' instead."
)

with self.assertRaisesRegexp(ValueError, expected):
with self.assertRaisesRegex(ValueError, expected):
p.show_graph(format='fizzbuzz')

def test_infer_domain_no_terms(self):
Expand Down
8 changes: 4 additions & 4 deletions tests/test_assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -1267,7 +1267,7 @@ def test_lookup_by_supplementary_field(self):
self.assertEqual(asset_1.sid, 1)

# We don't know about this ALT_ID yet.
with self.assertRaisesRegexp(
with self.assertRaisesRegex(
ValueNotFoundForField,
"Value '{}' was not found for field '{}'.".format(
'100000002',
Expand All @@ -1294,7 +1294,7 @@ def test_lookup_by_supplementary_field(self):
"Multiple occurrences of the value '{}' found for field '{}'."
).format('100000000', 'ALT_ID')

with self.assertRaisesRegexp(
with self.assertRaisesRegex(
MultipleValuesFoundForField,
expected_in_repr,
):
Expand Down Expand Up @@ -1377,7 +1377,7 @@ def test_get_supplementary_field(self):

# Since sid 2 has not yet started, we don't know about its
# ALT_ID.
with self.assertRaisesRegexp(
with self.assertRaisesRegex(
NoValueForSid,
"No '{}' value found for sid '{}'.".format('ALT_ID', 2),
):
Expand All @@ -1395,7 +1395,7 @@ def test_get_supplementary_field(self):
)

# Sid 0 has historically held two values for ALT_ID by this dt.
with self.assertRaisesRegexp(
with self.assertRaisesRegex(
MultipleValuesFoundForSid,
"Multiple '{}' values found for sid '{}'.".format('ALT_ID', 0),
):
Expand Down
10 changes: 5 additions & 5 deletions tests/test_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,7 @@ def test_negative_bar_count(self):
"""
Negative bar counts leak future information.
"""
with self.assertRaisesRegexp(
with self.assertRaisesRegex(
ValueError,
"bar_count must be >= 1, but got -1"
):
Expand Down Expand Up @@ -1388,7 +1388,7 @@ def test_history_window_before_first_trading_day(self):
'window, start the backtest on or after 2014-01-06.'
)
for field in OHLCP:
with self.assertRaisesRegexp(
with self.assertRaisesRegex(
HistoryWindowStartsBeforeData, exp_msg):
self.data_portal.get_history_window(
[self.ASSET1],
Expand Down Expand Up @@ -2030,7 +2030,7 @@ def test_history_window_before_first_trading_day(self):
'window, start the backtest on or after 2014-01-09.'
)

with self.assertRaisesRegexp(HistoryWindowStartsBeforeData, exp_msg):
with self.assertRaisesRegex(HistoryWindowStartsBeforeData, exp_msg):
self.data_portal.get_history_window(
[self.ASSET1],
second_day,
Expand All @@ -2040,7 +2040,7 @@ def test_history_window_before_first_trading_day(self):
'daily',
)[self.ASSET1]

with self.assertRaisesRegexp(HistoryWindowStartsBeforeData, exp_msg):
with self.assertRaisesRegex(HistoryWindowStartsBeforeData, exp_msg):
self.data_portal.get_history_window(
[self.ASSET1],
second_day,
Expand All @@ -2054,7 +2054,7 @@ def test_history_window_before_first_trading_day(self):
first_minute = \
self.trading_calendar.schedule.market_open[self.TRADING_START_DT]

with self.assertRaisesRegexp(HistoryWindowStartsBeforeData, exp_msg):
with self.assertRaisesRegex(HistoryWindowStartsBeforeData, exp_msg):
self.data_portal.get_history_window(
[self.ASSET2],
first_minute,
Expand Down
6 changes: 5 additions & 1 deletion zipline/testing/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from logbook import NullHandler, Logger
import numpy as np
import pandas as pd
from six import with_metaclass, iteritems, itervalues
from six import with_metaclass, iteritems, itervalues, PY2
import responses
from toolz import flip, groupby, merge
from trading_calendars import (
Expand Down Expand Up @@ -251,6 +251,10 @@ def add_instance_callback(self, callback):
"""
return self._instance_teardown_stack.callback(callback)

if PY2:
def assertRaisesRegex(self, *args, **kwargs):
return self.assertRaisesRegexp(*args, **kwargs)


def alias(attr_name):
"""Make a fixture attribute an alias of another fixture's attribute by
Expand Down

0 comments on commit b43b558

Please sign in to comment.