Skip to content

Commit

Permalink
[CodeStyle][UP005] replace deprecated unittest aliases (#49522)
Browse files Browse the repository at this point in the history
  • Loading branch information
SigureMo committed Jan 6, 2023
1 parent 419c2d1 commit d00c2ca
Show file tree
Hide file tree
Showing 33 changed files with 81 additions and 81 deletions.
2 changes: 1 addition & 1 deletion python/paddle/fluid/tests/custom_op/test_check_abi.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def fake():
class TestRunCMDException(unittest.TestCase):
def test_exception(self):
for verbose in [True, False]:
with self.assertRaisesRegexp(RuntimeError, "Failed to run command"):
with self.assertRaisesRegex(RuntimeError, "Failed to run command"):
cmd = "fake cmd"
utils.run_cmd(cmd, verbose)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -396,17 +396,17 @@ def test_raise_error(self):
paddle.enable_static()

net = SimpleNet()
with self.assertRaisesRegexp(
with self.assertRaisesRegex(
RuntimeError, "only available in dynamic mode"
):
net.forward.concrete_program

with self.assertRaisesRegexp(
with self.assertRaisesRegex(
RuntimeError, "only available in dynamic mode"
):
net.forward.inputs

with self.assertRaisesRegexp(
with self.assertRaisesRegex(
RuntimeError, "only available in dynamic mode"
):
net.forward.outputs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -357,12 +357,12 @@ def test_raise_error(self):
net = Net()

self.program_translator.enable(True)
with self.assertRaisesRegexp(
with self.assertRaisesRegex(
RuntimeError, "only available in dynamic mode"
):
self.program_translator.get_output(net.forward, self.x)

with self.assertRaisesRegexp(
with self.assertRaisesRegex(
RuntimeError, "only available in dynamic mode"
):
self.program_translator.get_program(net.forward, self.x)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def to_idx(name):
return name_ids[name]

mode = [to_idx(name) for name in in_names]
self.assertEquals(mode, expect_mode)
self.assertEqual(mode, expect_mode)


if __name__ == '__main__':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,12 @@ def test_network(self):
self.network
)

self.assertAlmostEquals(
self.assertAlmostEqual(
np.mean(baseline_last_loss),
np.mean(cur_last_loss),
delta=1e-6,
)
self.assertAlmostEquals(
self.assertAlmostEqual(
np.mean(baseline_first_loss),
np.mean(cur_first_loss),
delta=1e-6,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def test_failure(self):
paddle.enable_static()

data = fluid.data(shape=Xshape, dtype='float64', name=cls_name)
with self.assertRaisesRegexp(
with self.assertRaisesRegex(
eval(expected.split(':')[-1]), errmsg[expected]
):
getattr(tensor, op_type)(x=data, diagonal=diagonal)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def test_failure(self):
paddle.enable_static()

data = fluid.data(shape=Xshape, dtype='float32', name=cls_name)
with self.assertRaisesRegexp(
with self.assertRaisesRegex(
eval(expected.split(':')[-1]), errmsg[expected]
):
getattr(tensor, op_type)(x=data, diagonal=diagonal)
Expand Down
8 changes: 4 additions & 4 deletions python/paddle/fluid/tests/unittests/seresnext_test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,16 @@ def _compare_result_with_origin_model(

if compare_separately:
for loss in zip(func_1_first_loss, func_2_first_loss):
self.assertAlmostEquals(loss[0], loss[1], delta=1e-5)
self.assertAlmostEqual(loss[0], loss[1], delta=1e-5)
for loss in zip(func_1_last_loss, func_2_last_loss):
self.assertAlmostEquals(loss[0], loss[1], delta=delta2)
self.assertAlmostEqual(loss[0], loss[1], delta=delta2)
else:
np.testing.assert_allclose(
func_1_loss_area, func_2_loss_area, rtol=delta2
)
self.assertAlmostEquals(
self.assertAlmostEqual(
np.mean(func_1_first_loss), func_2_first_loss[0], delta=1e-5
)
self.assertAlmostEquals(
self.assertAlmostEqual(
np.mean(func_1_last_loss), func_2_last_loss[0], delta=delta2
)
12 changes: 6 additions & 6 deletions python/paddle/fluid/tests/unittests/test_base_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,12 @@ def test_register_buffer_with_error(self):
net = fluid.Layer()
var = to_variable(np.zeros([1]))

with self.assertRaisesRegexp(
with self.assertRaisesRegex(
TypeError, "name of buffer should be a string"
):
net.register_buffer(12, var)

with self.assertRaisesRegexp(
with self.assertRaisesRegex(
TypeError, "buffer should be a Paddle.Tensor"
):
if in_dygraph_mode():
Expand All @@ -194,26 +194,26 @@ def test_register_buffer_with_error(self):
"buffer_name", ParamBase([2, 2], 'float32')
)

with self.assertRaisesRegexp(
with self.assertRaisesRegex(
KeyError, "name of buffer can not contain"
):
net.register_buffer("buffer.name", var)

with self.assertRaisesRegexp(
with self.assertRaisesRegex(
KeyError, "name of buffer can not be empty"
):
net.register_buffer("", var)

net.attr_name = 10
with self.assertRaisesRegexp(KeyError, "already exists"):
with self.assertRaisesRegex(KeyError, "already exists"):
net.register_buffer("attr_name", var)

del net.attr_name
if in_dygraph_mode():
net.attr_name = EagerParamBase([2, 2], 'float32')
else:
net.attr_name = ParamBase([2, 2], 'float32')
with self.assertRaisesRegexp(KeyError, "already exists"):
with self.assertRaisesRegex(KeyError, "already exists"):
net.register_buffer("attr_name", var)

def test_register_buffer_same_name(self):
Expand Down
2 changes: 1 addition & 1 deletion python/paddle/fluid/tests/unittests/test_crypto.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class CipherUtilsTestCase(unittest.TestCase):
def test_gen_key(self):
key1 = CipherUtils.gen_key(256)
key2 = CipherUtils.gen_key_to_file(256, "paddle_aes_test.keyfile")
self.assertNotEquals(key1, key2)
self.assertNotEqual(key1, key2)
key3 = CipherUtils.read_key_from_file("paddle_aes_test.keyfile")
self.assertEqual(key2, key3)
self.assertEqual(len(key1), 32)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ def test_main(self):
read_num = 0
for data in cyclic_reader():
read_num += 1
self.assertEquals(len(data), 2)
self.assertEqual(len(data), 2)
if read_num == sample_num * 2:
break

self.assertEquals(read_num, sample_num * 2)
self.assertEqual(read_num, sample_num * 2)


if __name__ == '__main__':
Expand Down
12 changes: 6 additions & 6 deletions python/paddle/fluid/tests/unittests/test_dataset_dataloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ def check_batch_number(self, place, randomize_batch_num=False):
for _ in range(EPOCH_NUM):
has_complete_batch = False
for batch_id, data in enumerate(dataloader):
self.assertEquals(len(places), len(data))
self.assertEqual(len(places), len(data))
for idx, data_on_each_device in enumerate(data):
image = data_on_each_device["image"]
label = data_on_each_device["label"]
Expand All @@ -166,32 +166,32 @@ def check_batch_number(self, place, randomize_batch_num=False):
else:
batch_size = BATCH_SIZE

self.assertEquals(image.shape()[1:], IMAGE_SHAPE)
self.assertEqual(image.shape()[1:], IMAGE_SHAPE)
self.assertTrue(
image._place()._equals(places[idx]),
msg=get_place_string(image._place())
+ ' vs '
+ get_place_string(places[idx]),
)
if self.drop_last:
self.assertEquals(image.shape()[0], BATCH_SIZE)
self.assertEqual(image.shape()[0], BATCH_SIZE)
else:
self.assertTrue(
image.shape()[0] == BATCH_SIZE
or image.shape()[0] == BATCH_SIZE / 2
)

self.assertEquals(label.shape()[1:], LABEL_SHAPE)
self.assertEqual(label.shape()[1:], LABEL_SHAPE)
self.assertTrue(label._place()._equals(places[idx]))
if self.drop_last:
self.assertEquals(label.shape()[0], BATCH_SIZE)
self.assertEqual(label.shape()[0], BATCH_SIZE)
else:
self.assertTrue(
label.shape()[0] == BATCH_SIZE
or label.shape()[0] == BATCH_SIZE / 2
)

self.assertEquals(image.shape()[0], label.shape()[0])
self.assertEqual(image.shape()[0], label.shape()[0])

if image.shape()[0] == BATCH_SIZE:
has_complete_batch = True
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,21 @@ def build_network(self, call_interface):

def assert_program_equal(self, prog1, prog2):
block_num = prog1.num_blocks
self.assertEquals(block_num, prog2.num_blocks)
self.assertEqual(block_num, prog2.num_blocks)

for block_id in range(block_num):
block1 = prog1.block(block_id)
block2 = prog2.block(block_id)
self.assertEquals(len(block1.ops), len(block2.ops))
self.assertEqual(len(block1.ops), len(block2.ops))
for op1, op2 in zip(block1.ops, block2.ops):
self.assertEquals(op1.input_arg_names, op2.input_arg_names)
self.assertEquals(op1.output_arg_names, op2.output_arg_names)
self.assertEqual(op1.input_arg_names, op2.input_arg_names)
self.assertEqual(op1.output_arg_names, op2.output_arg_names)

self.assertEquals(len(block1.vars), len(block2.vars))
self.assertEqual(len(block1.vars), len(block2.vars))
for var1 in block1.vars.values():
self.assertTrue(var1.name in block2.vars)
var2 = block2.vars.get(var1.name)
self.assertEquals(var1.name, var2.name)
self.assertEqual(var1.name, var2.name)

def test_main(self):
prog1 = self.build_network(False)
Expand Down
2 changes: 1 addition & 1 deletion python/paddle/fluid/tests/unittests/test_detach.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ def test_backward_error(self):
var_d = var_b**2

loss = paddle.nn.functional.relu(var_c + var_d)
with self.assertRaisesRegexp(
with self.assertRaisesRegex(
RuntimeError,
"received tensor_version:{} != wrapper_version_snapshot:{}".format(
1, 0
Expand Down
4 changes: 2 additions & 2 deletions python/paddle/fluid/tests/unittests/test_dist_tree_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ def test_tree_index(self):
node.id() for node in tree.get_nodes(travel_path_codes)
]

self.assertEquals(travel_path_ids + [travel_ids[-1]], travel_ids)
self.assertEquals(travel_path_codes + [travel_codes[-1]], travel_codes)
self.assertEqual(travel_path_ids + [travel_ids[-1]], travel_ids)
self.assertEqual(travel_path_codes + [travel_codes[-1]], travel_codes)

# get_children
children_codes = tree.get_children_codes(travel_codes[1], height - 1)
Expand Down
14 changes: 7 additions & 7 deletions python/paddle/fluid/tests/unittests/test_egr_python_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@ def test_retain_grad_and_run_backward_raises(self):

out_eager = core.eager.scale(data_eager, 1.0, 0.9, True, True)
self.assertIsNone(data_eager.grad)
with self.assertRaisesRegexp(
with self.assertRaisesRegex(
AssertionError, "The type of grad_tensor must be paddle.Tensor"
):
out_eager.backward(grad_data, False)

with self.assertRaisesRegexp(
with self.assertRaisesRegex(
AssertionError,
"Tensor shape not match, Tensor of grad_tensor /*",
):
Expand Down Expand Up @@ -265,17 +265,17 @@ def constructor(self, place):
zero_dim_param = EagerParamBase(shape=[], dtype="float32")
self.assertEqual(zero_dim_param.shape, [])

with self.assertRaisesRegexp(
with self.assertRaisesRegex(
ValueError, "The shape of Parameter should not be None"
):
eager_param = EagerParamBase(shape=None, dtype="float32")

with self.assertRaisesRegexp(
with self.assertRaisesRegex(
ValueError, "The dtype of Parameter should not be None"
):
eager_param = EagerParamBase(shape=[1, 1], dtype=None)

with self.assertRaisesRegexp(
with self.assertRaisesRegex(
ValueError,
"Each dimension of shape for Parameter must be greater than 0, but received /*",
):
Expand All @@ -285,7 +285,7 @@ def constructor(self, place):
self.assertTrue(eager_param.trainable)
eager_param.trainable = False
self.assertFalse(eager_param.trainable)
with self.assertRaisesRegexp(
with self.assertRaisesRegex(
ValueError, "The type of trainable MUST be bool, but the type is /*"
):
eager_param.trainable = "False"
Expand All @@ -296,7 +296,7 @@ def constructor(self, place):
self.assertTrue(eager_param_2.trainable)
eager_param_2.trainable = False
self.assertFalse(eager_param_2.trainable)
with self.assertRaisesRegexp(
with self.assertRaisesRegex(
ValueError, "The type of trainable MUST be bool, but the type is /*"
):
eager_param_2.trainable = "False"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ def compare_fuse_all_reduce_ops(
)

for loss in zip(not_fuse_op_first_loss, fuse_op_first_loss):
self.assertAlmostEquals(loss[0], loss[1], delta=1e-6)
self.assertAlmostEqual(loss[0], loss[1], delta=1e-6)
for loss in zip(not_fuse_op_last_loss, fuse_op_last_loss):
self.assertAlmostEquals(loss[0], loss[1], delta=1e-6)
self.assertAlmostEqual(loss[0], loss[1], delta=1e-6)

def optimizer(self, learning_rate=1e-3):
optimizer = fluid.optimizer.SGD(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ def _optimizer(learning_rate=1e-6):
)

for loss in zip(not_fuse_op_first_loss, fuse_op_first_loss):
self.assertAlmostEquals(loss[0], loss[1], delta=1e-6)
self.assertAlmostEqual(loss[0], loss[1], delta=1e-6)
for loss in zip(not_fuse_op_last_loss, fuse_op_last_loss):
self.assertAlmostEquals(loss[0], loss[1], delta=1e-6)
self.assertAlmostEqual(loss[0], loss[1], delta=1e-6)

def test_simple_fc_with_fuse_op(self):
self._compare_fuse_elewise_add_act_ops(simple_fc_net, DeviceType.CUDA)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ def _compare_fused_optimizer_ops(
)

for loss in zip(not_fuse_op_first_loss, fuse_op_first_loss):
self.assertAlmostEquals(loss[0], loss[1], delta=1e-6)
self.assertAlmostEqual(loss[0], loss[1], delta=1e-6)
for loss in zip(not_fuse_op_last_loss, fuse_op_last_loss):
self.assertAlmostEquals(loss[0], loss[1], delta=1e-6)
self.assertAlmostEqual(loss[0], loss[1], delta=1e-6)

def _decorate_compare_fused_optimizer_ops(
self, model, use_device, optimizer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,9 @@ def _optimizer(learning_rate=1e-6):
)

for loss in zip(not_fuse_op_first_loss, fuse_op_first_loss):
self.assertAlmostEquals(loss[0], loss[1], delta=1e-6)
self.assertAlmostEqual(loss[0], loss[1], delta=1e-6)
for loss in zip(not_fuse_op_last_loss, fuse_op_last_loss):
self.assertAlmostEquals(loss[0], loss[1], delta=1e-6)
self.assertAlmostEqual(loss[0], loss[1], delta=1e-6)

def test_simple_depthwise_with_fuse_op(self):
self._compare(simple_depthwise_net, DeviceType.CUDA)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def test_main(self):
self.assertFalse(name in g)
self.assertFalse(name in g.keys())
self.assertIsNone(g.get(name, None))
self.assertEquals(g.get(name, -1), -1)
self.assertEqual(g.get(name, -1), -1)


if __name__ == '__main__':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def setUp(self):

def test_not_capacity(self):
with fluid.dygraph.guard():
with self.assertRaisesRegexp(
with self.assertRaisesRegex(
ValueError, "Please give value to capacity."
):
fluid.io.DataLoader.from_generator()
Expand Down
Loading

0 comments on commit d00c2ca

Please sign in to comment.