Skip to content

Commit

Permalink
Update assertRaisesRegexp -> assertRaisesRegex
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 614713466
  • Loading branch information
jburnim authored and tensorflower-gardener committed Mar 11, 2024
1 parent 0103d3c commit 5e568e1
Show file tree
Hide file tree
Showing 88 changed files with 278 additions and 278 deletions.
4 changes: 2 additions & 2 deletions UNITTEST.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,13 @@ class _DistributionTest(tf.test.TestCase):
class DistributionTest_StaticShape(_DistributionTest):
...
def assertRaisesError(self, msg):
return self.assertRaisesRegexp(Exception, msg)
return self.assertRaisesRegex(Exception, msg)

class DistributionTest_DynamicShape(_DistributionTest):
...
def assertRaisesError(self, msg):
if tf.executing_eagerly():
return self.assertRaisesRegexp(Exception, msg)
return self.assertRaisesRegex(Exception, msg)
return self.assertRaisesOpError(msg)

del _DistributionTest # Don't run tests for the base class.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ def testCorrectGroundTruthWithHMC(self):
def testBadGroundTruthWithHMC(self):
"""Tests that an error is raised if the ground truth is wrong."""
model = TestModel(ground_truth_mean=-1000.)
with self.assertRaisesRegexp(AssertionError, 'Not equal to tolerance'):
with self.assertRaisesRegex(AssertionError, 'Not equal to tolerance'):
self.validate_ground_truth_using_hmc(
model,
num_chains=4,
Expand All @@ -195,7 +195,7 @@ def testCorrectGroundTruthWithMC(self):
def testBadGroundTruthWithMC(self):
"""Tests that an error is raised if the ground truth is wrong."""
model = TestModel(ground_truth_mean=-10.)
with self.assertRaisesRegexp(AssertionError, 'Not equal to tolerance'):
with self.assertRaisesRegex(AssertionError, 'Not equal to tolerance'):
self.validate_ground_truth_using_monte_carlo(
model,
num_samples=4000,
Expand All @@ -210,8 +210,8 @@ def testCorrectMaterialization(self):
def testBadMaterialization(self):
"""Tests that an error is raised if dataset materialization is wrong."""
dataset = np.zeros(5)
with self.assertRaisesRegexp(AssertionError,
'Erroneously materialized dataset'):
with self.assertRaisesRegex(AssertionError,
'Erroneously materialized dataset'):
self.validate_deferred_materialization(
functools.partial(TestModelWithDataset, materialize_dataset=True),
dataset=dataset)
Expand Down
34 changes: 17 additions & 17 deletions tensorflow_probability/python/bijectors/bijector_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,20 +88,20 @@ def __init__(self):
self.assertAllEqual(shape, inverse_event_shape_)
self.assertAllEqual(shape, bij.inverse_event_shape(shape))

with self.assertRaisesRegexp(NotImplementedError,
'inverse not implemented'):
with self.assertRaisesRegex(NotImplementedError,
'inverse not implemented'):
bij.inverse(0)

with self.assertRaisesRegexp(NotImplementedError,
'forward not implemented'):
with self.assertRaisesRegex(NotImplementedError,
'forward not implemented'):
bij.forward(0)

with self.assertRaisesRegexp(
with self.assertRaisesRegex(
NotImplementedError,
'Cannot derive `inverse_log_det_jacobian`'):
bij.inverse_log_det_jacobian(0, event_ndims=0)

with self.assertRaisesRegexp(
with self.assertRaisesRegex(
NotImplementedError,
'Cannot derive `forward_log_det_jacobian`'):
bij.forward_log_det_jacobian(0, event_ndims=0)
Expand Down Expand Up @@ -140,14 +140,14 @@ def _forward(self, x):
b32 = _TypedIdentity(tf.float32)
self.assertEqual(tf.float32, b32(0).dtype)
self.assertEqual(tf.float32, b32(x32).dtype)
with self.assertRaisesRegexp(
with self.assertRaisesRegex(
error_clazz, 'Tensor conversion requested dtype'):
b32.forward(x64)

b64 = _TypedIdentity(tf.float64)
self.assertEqual(tf.float64, b64(0).dtype)
self.assertEqual(tf.float64, b64(x64).dtype)
with self.assertRaisesRegexp(
with self.assertRaisesRegex(
error_clazz, 'Tensor conversion requested dtype'):
b64.forward(x32)

Expand Down Expand Up @@ -583,20 +583,20 @@ def _forward_log_det_jacobian(self, x):
class BijectorTestEventNdims(test_util.TestCase):

def assertRaisesError(self, msg):
return self.assertRaisesRegexp(Exception, msg)
return self.assertRaisesRegex(Exception, msg)

def testBijectorNonIntegerEventNdims(self):
bij = ExpOnlyJacobian()
with self.assertRaisesRegexp(ValueError, 'Expected integer'):
with self.assertRaisesRegex(ValueError, 'Expected integer'):
bij.forward_log_det_jacobian(1., event_ndims=1.5)
with self.assertRaisesRegexp(ValueError, 'Expected integer'):
with self.assertRaisesRegex(ValueError, 'Expected integer'):
bij.inverse_log_det_jacobian(1., event_ndims=1.5)

def testBijectorArrayEventNdims(self):
bij = ExpOnlyJacobian()
with self.assertRaisesRegexp(ValueError, 'Expected scalar'):
with self.assertRaisesRegex(ValueError, 'Expected scalar'):
bij.forward_log_det_jacobian(1., event_ndims=(1, 2))
with self.assertRaisesRegexp(ValueError, 'Expected scalar'):
with self.assertRaisesRegex(ValueError, 'Expected scalar'):
bij.inverse_log_det_jacobian(1., event_ndims=(1, 2))

def testBijectorDynamicEventNdims(self):
Expand Down Expand Up @@ -1034,9 +1034,9 @@ def testInverseWithEventDimsOmitted(self):
def testReduceEventNdimsForwardRaiseError(self):
x = [[[1., 2.], [3., 4.]]]
bij = ExpOnlyJacobian(forward_min_event_ndims=1)
with self.assertRaisesRegexp(ValueError, 'must be at least'):
with self.assertRaisesRegex(ValueError, 'must be at least'):
bij.forward_log_det_jacobian(x, event_ndims=0)
with self.assertRaisesRegexp(ValueError, 'Input must have rank at least'):
with self.assertRaisesRegex(ValueError, 'Input must have rank at least'):
bij.forward_log_det_jacobian(x, event_ndims=4)

def testReduceEventNdimsInverse(self):
Expand All @@ -1055,9 +1055,9 @@ def testReduceEventNdimsInverse(self):
def testReduceEventNdimsInverseRaiseError(self):
x = [[[1., 2.], [3., 4.]]]
bij = ExpOnlyJacobian(forward_min_event_ndims=1)
with self.assertRaisesRegexp(ValueError, 'must be at least'):
with self.assertRaisesRegex(ValueError, 'must be at least'):
bij.inverse_log_det_jacobian(x, event_ndims=0)
with self.assertRaisesRegexp(ValueError, 'Input must have rank at least'):
with self.assertRaisesRegex(ValueError, 'Input must have rank at least'):
bij.inverse_log_det_jacobian(x, event_ndims=4)

def testReduceEventNdimsForwardConstJacobian(self):
Expand Down
4 changes: 2 additions & 2 deletions tensorflow_probability/python/bijectors/blockwise_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,11 +240,11 @@ def testNameOneBijector(self):
self.assertStartsWith(bijector.name, 'blockwise_of_exp')

def testRaisesEmptyBijectors(self):
with self.assertRaisesRegexp(ValueError, '`bijectors` must not be empty'):
with self.assertRaisesRegex(ValueError, '`bijectors` must not be empty'):
blockwise.Blockwise(bijectors=[])

def testRaisesBadBlocks(self):
with self.assertRaisesRegexp(
with self.assertRaisesRegex(
ValueError,
r'`block_sizes` must be `None`, or a vector of the same length as '
r'`bijectors`. Got a `Tensor` with shape \(2L?,\) and `bijectors` of '
Expand Down
2 changes: 1 addition & 1 deletion tensorflow_probability/python/bijectors/chain_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ def testMinEventNdimsWithPartiallyDependentJointMap(self):
dependent_as_chain._parts_interact)

def testInvalidChainNdimsRaisesError(self):
with self.assertRaisesRegexp(
with self.assertRaisesRegex(
ValueError,
"Differences between `event_ndims` and `min_event_ndims must be equal"):
chain.Chain(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,14 @@ def testShapeError(self):
b = fill_triangular.FillTriangular(validate_args=True)

x_shape_bad = tf.TensorShape([5, 4, 7])
with self.assertRaisesRegexp(ValueError, 'is not a triangular number'):
with self.assertRaisesRegex(ValueError, 'is not a triangular number'):
b.forward_event_shape(x_shape_bad)
with self.assertRaisesOpError('is not a triangular number'):
self.evaluate(
b.forward_event_shape_tensor(tensorshape_util.as_list(x_shape_bad)))

y_shape_bad = tf.TensorShape([5, 4, 4, 3])
with self.assertRaisesRegexp(ValueError, 'Matrix must be square'):
with self.assertRaisesRegex(ValueError, 'Matrix must be square'):
b.inverse_event_shape(y_shape_bad)
with self.assertRaisesOpError('Matrix must be square'):
self.evaluate(
Expand Down
8 changes: 4 additions & 4 deletions tensorflow_probability/python/bijectors/cumsum_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ class _CumsumBijectorTest(test_util.TestCase):
"""Tests correctness of the cumsum bijector."""

def testInvalidAxis(self):
with self.assertRaisesRegexp(ValueError,
r'Argument `axis` must be negative.*'):
with self.assertRaisesRegex(ValueError,
r'Argument `axis` must be negative.*'):
cumsum.Cumsum(axis=0, validate_args=True)
with self.assertRaisesRegexp(TypeError,
r'Argument `axis` is not an `int` type\.*'):
with self.assertRaisesRegex(TypeError,
r'Argument `axis` is not an `int` type\.*'):
cumsum.Cumsum(axis=-1., validate_args=True)

def testBijector(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,14 @@ def testShapeError(self):
b = fill_triangular.FillTriangular(validate_args=True)

x_shape_bad = tf.TensorShape([5, 4, 7])
with self.assertRaisesRegexp(ValueError, 'is not a triangular number'):
with self.assertRaisesRegex(ValueError, 'is not a triangular number'):
b.forward_event_shape(x_shape_bad)
with self.assertRaisesOpError('is not a triangular number'):
self.evaluate(
b.forward_event_shape_tensor(tensorshape_util.as_list(x_shape_bad)))

y_shape_bad = tf.TensorShape([5, 4, 3, 2])
with self.assertRaisesRegexp(ValueError, 'Matrix must be square'):
with self.assertRaisesRegex(ValueError, 'Matrix must be square'):
b.inverse_event_shape(y_shape_bad)
with self.assertRaisesOpError('Matrix must be square'):
self.evaluate(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ def testInvertMutuallyConsistent(self):
rtol=0.03)

def testVectorBijectorRaises(self):
with self.assertRaisesRegexp(
with self.assertRaisesRegex(
ValueError,
"Bijectors with `forward_min_event_ndims` > 0 are not supported"):

Expand All @@ -450,7 +450,7 @@ def bijector_fn(*args, **kwargs):
maf.forward([1., 2.])

def testRankChangingBijectorRaises(self):
with self.assertRaisesRegexp(
with self.assertRaisesRegex(
ValueError, "Bijectors which alter `event_ndims` are not supported."):

def bijector_fn(*args, **kwargs):
Expand Down Expand Up @@ -931,15 +931,15 @@ def test_doc_string_images_case_2(self):
class ConditionalTests(test_util.TestCase):

def test_conditional_missing_event_shape(self):
with self.assertRaisesRegexp(
with self.assertRaisesRegex(
ValueError,
"`event_shape` must be provided when `conditional` is True"):

masked_autoregressive.AutoregressiveNetwork(
params=2, conditional=True, conditional_event_shape=[4])

def test_conditional_missing_conditional_shape(self):
with self.assertRaisesRegexp(
with self.assertRaisesRegex(
ValueError,
"`conditional_event_shape` must be provided when `conditional` is True"
):
Expand All @@ -948,7 +948,7 @@ def test_conditional_missing_conditional_shape(self):
params=2, conditional=True, event_shape=[4])

def test_conditional_incorrect_layers(self):
with self.assertRaisesRegexp(
with self.assertRaisesRegex(
ValueError,
"`conditional_input_layers` must be \"first_layers\" or \"all_layers\""
):
Expand All @@ -961,15 +961,15 @@ def test_conditional_incorrect_layers(self):
conditional_input_layers="non-existent-option")

def test_conditional_false_with_shape(self):
with self.assertRaisesRegexp(
with self.assertRaisesRegex(
ValueError,
"`conditional_event_shape` passed but `conditional` is set to False."):

masked_autoregressive.AutoregressiveNetwork(
params=2, conditional_event_shape=[4])

def test_conditional_wrong_shape(self):
with self.assertRaisesRegexp(
with self.assertRaisesRegex(
ValueError,
"Parameter `conditional_event_shape` must describe a rank-1 shape"):

Expand All @@ -980,7 +980,7 @@ def test_conditional_wrong_shape(self):
conditional_event_shape=[10, 4])

def test_conditional_missing_tensor(self):
with self.assertRaisesRegexp(
with self.assertRaisesRegex(
ValueError, "`conditional_input` must be passed as a named argument"):

made = masked_autoregressive.AutoregressiveNetwork(
Expand Down
10 changes: 5 additions & 5 deletions tensorflow_probability/python/bijectors/permute_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class PermuteBijectorTest(test_util.TestCase):
"""Tests correctness of the Permute bijector."""

def assertRaisesError(self, msg):
return self.assertRaisesRegexp(Exception, msg)
return self.assertRaisesRegex(Exception, msg)

def setUp(self):
self._rng = np.random.RandomState(42)
Expand Down Expand Up @@ -101,7 +101,7 @@ def testPreservesShape(self):

def testNonPermutationAssertion(self):
message = 'must contain exactly one of each of'
with self.assertRaisesRegexp(Exception, message):
with self.assertRaisesRegex(Exception, message):
permutation = np.int32([1, 0, 1])
bijector = permute.Permute(permutation=permutation, validate_args=True)
x = np.random.randn(4, 2, 3)
Expand All @@ -111,7 +111,7 @@ def testVariableNonPermutationAssertion(self):
message = 'must contain exactly one of each of'
permutation = tf.Variable(np.int32([1, 0, 1]))
self.evaluate(permutation.initializer)
with self.assertRaisesRegexp(Exception, message):
with self.assertRaisesRegex(Exception, message):
bijector = permute.Permute(permutation=permutation, validate_args=True)
x = np.random.randn(4, 2, 3)
_ = self.evaluate(bijector.forward(x))
Expand All @@ -121,14 +121,14 @@ def testModifiedVariableNonPermutationAssertion(self):
permutation = tf.Variable(np.int32([1, 0, 2]))
self.evaluate(permutation.initializer)
bijector = permute.Permute(permutation=permutation, validate_args=True)
with self.assertRaisesRegexp(Exception, message):
with self.assertRaisesRegex(Exception, message):
with tf.control_dependencies([permutation.assign([1, 0, 1])]):
x = np.random.randn(4, 2, 3)
_ = self.evaluate(bijector.forward(x))

def testPermutationTypeAssertion(self):
message = 'should be `int`-like'
with self.assertRaisesRegexp(Exception, message):
with self.assertRaisesRegex(Exception, message):
permutation = np.float32([2, 0, 1])
bijector = permute.Permute(permutation=permutation, validate_args=True)
x = np.random.randn(4, 2, 3)
Expand Down
2 changes: 1 addition & 1 deletion tensorflow_probability/python/bijectors/power_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def testPowerOddInteger(self):
rtol=1e-7)

def testZeroPowerRaisesError(self):
with self.assertRaisesRegexp(Exception, 'must be non-zero'):
with self.assertRaisesRegex(Exception, 'must be non-zero'):
b = power_lib.Power(power=0., validate_args=True)
b.forward(1.)

Expand Down
14 changes: 7 additions & 7 deletions tensorflow_probability/python/bijectors/real_nvp_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ def _real_nvp_kwargs(self):
class RealNVPTestCommon(test_util.TestCase):

def testMatrixBijectorRaises(self):
with self.assertRaisesRegexp(
with self.assertRaisesRegex(
ValueError,
'Bijectors with `forward_min_event_ndims` > 1 are not supported'):

Expand All @@ -263,7 +263,7 @@ def bijector_fn(*args, **kwargs):
rnvp.forward([1., 2.])

def testRankChangingBijectorRaises(self):
with self.assertRaisesRegexp(
with self.assertRaisesRegex(
ValueError, 'Bijectors which alter `event_ndims` are not supported.'):

def bijector_fn(*args, **kwargs):
Expand All @@ -275,13 +275,13 @@ def bijector_fn(*args, **kwargs):
rnvp.forward([1., 2.])

def testNonIntegerNumMaskedRaises(self):
with self.assertRaisesRegexp(TypeError, '`num_masked` must be an integer'):
with self.assertRaisesRegex(TypeError, '`num_masked` must be an integer'):
real_nvp.RealNVP(
num_masked=0.5, shift_and_log_scale_fn=lambda x, _: (x, x))

def testNonFloatFractionMaskedRaises(self):
with self.assertRaisesRegexp(TypeError,
'`fraction_masked` must be a float'):
with self.assertRaisesRegex(TypeError,
'`fraction_masked` must be a float'):
real_nvp.RealNVP(
fraction_masked=1, shift_and_log_scale_fn=lambda x, _: (x, x))

Expand All @@ -292,7 +292,7 @@ def testNonFloatFractionMaskedRaises(self):
('UpperBoundary', 1.),
)
def testBadFractionRaises(self, fraction_masked):
with self.assertRaisesRegexp(ValueError, '`fraction_masked` must be in'):
with self.assertRaisesRegex(ValueError, '`fraction_masked` must be in'):
real_nvp.RealNVP(
fraction_masked=fraction_masked,
shift_and_log_scale_fn=lambda x, _: (x, x))
Expand All @@ -304,7 +304,7 @@ def testBadFractionRaises(self, fraction_masked):
('UpperBoundary', 1),
)
def testBadNumMaskRaises(self, num_masked):
with self.assertRaisesRegexp(
with self.assertRaisesRegex(
ValueError,
'Number of masked units {} must be smaller than the event size 1'
.format(num_masked)):
Expand Down
Loading

0 comments on commit 5e568e1

Please sign in to comment.