From 8b3cfab676874a4460a49d605bd2ed2775fd28a4 Mon Sep 17 00:00:00 2001 From: SigureMo Date: Wed, 5 Apr 2023 09:30:23 +0000 Subject: [PATCH 1/3] [CodeStyle][B017] catch more specific exceptions in unittests --- pyproject.toml | 41 +------------------ .../paddle/distributed/fleet/base/graphviz.py | 18 ++++---- .../check_pass_conflict_example.py | 2 +- .../ir/inference/test_trt_dynamic_shape.py | 4 +- .../test_convert_call_generator.py | 14 ++++++- test/legacy_test/test_transforms.py | 33 ++++++++++----- 6 files changed, 46 insertions(+), 66 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 8c27dfbe3328f..ce5a8e010d3a6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -43,10 +43,6 @@ select = [ "B002", "B003", "B004", - # "B005", - # "B006", - # "B007", - # "B008", "B009", "B010", "B011", @@ -55,65 +51,30 @@ select = [ "B014", "B015", "B016", - # "B017", + "B017", "B018", "B019", "B020", "B021", "B022", - # "B023", - # "B024", "B025", - # "B026", - # "B027", - # "B028", "B029", - # "B030", "B032", - # "B904", # Pylint "PLC0414", - # "PLC1901", "PLC3002", "PLE0100", "PLE0101", - # "PLE0116", - # "PLE0117", - # "PLE0118", "PLE0604", "PLE0605", "PLE1142", "PLE1205", "PLE1206", "PLE1307", - # "PLE1310", - # "PLE1507", "PLE2502", - # "PLE2510", - # "PLE2512", - # "PLE2513", - # "PLE2514", - # "PLE2515", - # "PLR0133", "PLR0206", "PLR0402", - # "PLR0911", - # "PLR0912", - # "PLR0913", - # "PLR0915", - # "PLR1701", - # "PLR1711", - # "PLR1722", - # "PLR2004", - # "PLR5501", - # "PLW0120", - # "PLW0129", - # "PLW0602", - # "PLW0603", - # "PLW0711", - # "PLW1508", - # "PLW2901", ] unfixable = [ "NPY001" diff --git a/python/paddle/distributed/fleet/base/graphviz.py b/python/paddle/distributed/fleet/base/graphviz.py index c24fd4516bdf5..5e0b0c186b9b8 100644 --- a/python/paddle/distributed/fleet/base/graphviz.py +++ b/python/paddle/distributed/fleet/base/graphviz.py @@ -43,7 +43,7 @@ def __str__(self): return ( '{' - + 'rank={};'.format(self.kind) + + f'rank={self.kind};' + ','.join([node.name for node in self.nodes]) + '}' ) @@ -97,7 +97,7 @@ def compile(self, dot_path): stdout=subprocess.PIPE, stderr=subprocess.PIPE, ) - logging.warning("write block debug graph to {}".format(image_path)) + logging.warning(f"write block debug graph to {image_path}") return image_path def show(self, dot_path): @@ -125,13 +125,11 @@ def _rank_repr(self): def __str__(self): reprs = [ 'digraph G {', - 'title = {}'.format(crepr(self.title)), + f'title = {crepr(self.title)}', ] for attr in self.attrs: - reprs.append( - "{key}={value};".format(key=attr, value=crepr(self.attrs[attr])) - ) + reprs.append(f"{attr}={crepr(self.attrs[attr])};") reprs.append(self._rank_repr()) @@ -161,8 +159,7 @@ def __str__(self): label=self.label, extra=',' + ','.join( - "%s=%s" % (key, crepr(value)) - for key, value in self.attrs.items() + f"{key}={crepr(value)}" for key, value in self.attrs.items() ) if self.attrs else "", @@ -191,8 +188,7 @@ def __str__(self): if not self.attrs else "[" + ','.join( - "{}={}".format(attr[0], crepr(attr[1])) - for attr in self.attrs.items() + f"{attr[0]}={crepr(attr[1])}" for attr in self.attrs.items() ) + "]", ) @@ -292,5 +288,5 @@ def add_edge(self, source, target, **kwargs): source, target, color="#00000" if not highlight else "orange", - **kwargs + **kwargs, ) diff --git a/python/paddle/fluid/tests/unittests/distributed_passes/check_pass_conflict_example.py b/python/paddle/fluid/tests/unittests/distributed_passes/check_pass_conflict_example.py index 2e3db7633f46d..ce7889522031c 100644 --- a/python/paddle/fluid/tests/unittests/distributed_passes/check_pass_conflict_example.py +++ b/python/paddle/fluid/tests/unittests/distributed_passes/check_pass_conflict_example.py @@ -39,7 +39,7 @@ def pass_config(self): ] def test_resnet(self): - with self.assertRaises(Exception): + with self.assertRaises(Exception): # noqa: B017 self.check_main(resnet_model, batch_size=32) diff --git a/python/paddle/fluid/tests/unittests/ir/inference/test_trt_dynamic_shape.py b/python/paddle/fluid/tests/unittests/ir/inference/test_trt_dynamic_shape.py index a2950ad459904..07fd86f0ec149 100644 --- a/python/paddle/fluid/tests/unittests/ir/inference/test_trt_dynamic_shape.py +++ b/python/paddle/fluid/tests/unittests/ir/inference/test_trt_dynamic_shape.py @@ -72,7 +72,7 @@ def set_feeds(self): def test_check_output(self): if core.is_compiled_with_cuda(): use_gpu = True - with self.assertRaises(Exception): + with self.assertRaises(Exception): # noqa: B017 self.check_output_with_option(use_gpu) @@ -99,7 +99,7 @@ def set_feeds(self): def test_check_output(self): if core.is_compiled_with_cuda(): use_gpu = True - with self.assertRaises(Exception): + with self.assertRaises(Exception): # noqa: B017 self.check_output_with_option(use_gpu) diff --git a/test/dygraph_to_static/test_convert_call_generator.py b/test/dygraph_to_static/test_convert_call_generator.py index a938d67915854..07597119204b1 100644 --- a/test/dygraph_to_static/test_convert_call_generator.py +++ b/test/dygraph_to_static/test_convert_call_generator.py @@ -16,11 +16,12 @@ import paddle from paddle.jit import to_static +from paddle.jit.dy2static.convert_call_func import translator_logger def dyfunc_generator(): for i in range(100): - yield paddle.fluid.dygraph.to_variable([i] * 10) + yield paddle.to_tensor([i] * 10) def main_func(): @@ -31,8 +32,17 @@ def main_func(): class TestConvertGenerator(unittest.TestCase): def test_raise_error(self): - with self.assertRaises(Exception): + translator_logger.verbosity_level = 1 + with self.assertLogs( + translator_logger.logger_name, level='WARNING' + ) as cm: to_static(main_func)() + self.assertRegex( + cm.output[0], + "Your function:`dyfunc_generator` doesn't support " + "to transform to static function because it is a " + "generator function", + ) if __name__ == '__main__': diff --git a/test/legacy_test/test_transforms.py b/test/legacy_test/test_transforms.py index 7d1280b8944dd..229b6ced84de2 100644 --- a/test/legacy_test/test_transforms.py +++ b/test/legacy_test/test_transforms.py @@ -299,10 +299,10 @@ def test_exception(self): trans_batch = transforms.Compose([transforms.Resize(-1)]) - with self.assertRaises(Exception): + with self.assertRaises((cv2.error, AssertionError, ValueError)): self.do_transform(trans) - with self.assertRaises(Exception): + with self.assertRaises((cv2.error, AssertionError, ValueError)): self.do_transform(trans_batch) with self.assertRaises(ValueError): @@ -411,22 +411,35 @@ def test_exception(self): with self.assertRaises(NotImplementedError): transform = transforms.BrightnessTransform('0.1', keys='a') - with self.assertRaises(Exception): + with self.assertRaisesRegex( + AssertionError, "scale should be a tuple or list" + ): transform = transforms.RandomErasing(scale=0.5) - with self.assertRaises(Exception): + with self.assertRaisesRegex( + AssertionError, "ratio should be a tuple or list" + ): transform = transforms.RandomErasing(ratio=0.8) - with self.assertRaises(Exception): + with self.assertRaisesRegex( + AssertionError, + r"scale should be of kind \(min, max\) and in range \[0, 1\]", + ): transform = transforms.RandomErasing(scale=(10, 0.4)) - with self.assertRaises(Exception): + with self.assertRaisesRegex( + AssertionError, r"ratio should be of kind \(min, max\)" + ): transform = transforms.RandomErasing(ratio=(3.3, 0.3)) - with self.assertRaises(Exception): + with self.assertRaisesRegex( + AssertionError, r"The probability should be in range \[0, 1\]" + ): transform = transforms.RandomErasing(prob=1.5) - with self.assertRaises(Exception): + with self.assertRaisesRegex( + ValueError, r"value must be 'random' when type is str" + ): transform = transforms.RandomErasing(value="0") def test_info(self): @@ -571,10 +584,10 @@ def test_exception(self): trans_batch = transforms.Compose([transforms.Resize(-1)]) - with self.assertRaises(Exception): + with self.assertRaises((cv2.error, AssertionError, ValueError)): self.do_transform(trans) - with self.assertRaises(Exception): + with self.assertRaises((cv2.error, AssertionError, ValueError)): self.do_transform(trans_batch) with self.assertRaises(ValueError): From 9b33c3ae0a10681053d0c467f89cbc58be1536e6 Mon Sep 17 00:00:00 2001 From: SigureMo Date: Wed, 5 Apr 2023 11:18:14 +0000 Subject: [PATCH 2/3] temporarily remove assertraises to get exception type --- .../distributed_passes/check_pass_conflict_example.py | 4 ++-- .../unittests/ir/inference/test_trt_dynamic_shape.py | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/python/paddle/fluid/tests/unittests/distributed_passes/check_pass_conflict_example.py b/python/paddle/fluid/tests/unittests/distributed_passes/check_pass_conflict_example.py index ce7889522031c..538f6b5d63f8f 100644 --- a/python/paddle/fluid/tests/unittests/distributed_passes/check_pass_conflict_example.py +++ b/python/paddle/fluid/tests/unittests/distributed_passes/check_pass_conflict_example.py @@ -39,8 +39,8 @@ def pass_config(self): ] def test_resnet(self): - with self.assertRaises(Exception): # noqa: B017 - self.check_main(resnet_model, batch_size=32) + # with self.assertRaises(Exception): # noqa: B017 + self.check_main(resnet_model, batch_size=32) if __name__ == "__main__": diff --git a/python/paddle/fluid/tests/unittests/ir/inference/test_trt_dynamic_shape.py b/python/paddle/fluid/tests/unittests/ir/inference/test_trt_dynamic_shape.py index 07fd86f0ec149..edba0a4885651 100644 --- a/python/paddle/fluid/tests/unittests/ir/inference/test_trt_dynamic_shape.py +++ b/python/paddle/fluid/tests/unittests/ir/inference/test_trt_dynamic_shape.py @@ -72,8 +72,8 @@ def set_feeds(self): def test_check_output(self): if core.is_compiled_with_cuda(): use_gpu = True - with self.assertRaises(Exception): # noqa: B017 - self.check_output_with_option(use_gpu) + # with self.assertRaises(Exception): # noqa: B017 + self.check_output_with_option(use_gpu) # (wanghaipeng03) temporarily disable this test, in some cases, this test code @@ -99,8 +99,8 @@ def set_feeds(self): def test_check_output(self): if core.is_compiled_with_cuda(): use_gpu = True - with self.assertRaises(Exception): # noqa: B017 - self.check_output_with_option(use_gpu) + # with self.assertRaises(Exception): # noqa: B017 + self.check_output_with_option(use_gpu) if __name__ == "__main__": From e0d4ea93418c489d0dbd4eeedfbd77b4e8690a5a Mon Sep 17 00:00:00 2001 From: SigureMo Date: Wed, 5 Apr 2023 12:59:59 +0000 Subject: [PATCH 3/3] update exception type --- .../check_pass_conflict_example.py | 4 ++-- .../unittests/ir/inference/test_trt_dynamic_shape.py | 12 ++++++++---- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/python/paddle/fluid/tests/unittests/distributed_passes/check_pass_conflict_example.py b/python/paddle/fluid/tests/unittests/distributed_passes/check_pass_conflict_example.py index 538f6b5d63f8f..ce7889522031c 100644 --- a/python/paddle/fluid/tests/unittests/distributed_passes/check_pass_conflict_example.py +++ b/python/paddle/fluid/tests/unittests/distributed_passes/check_pass_conflict_example.py @@ -39,8 +39,8 @@ def pass_config(self): ] def test_resnet(self): - # with self.assertRaises(Exception): # noqa: B017 - self.check_main(resnet_model, batch_size=32) + with self.assertRaises(Exception): # noqa: B017 + self.check_main(resnet_model, batch_size=32) if __name__ == "__main__": diff --git a/python/paddle/fluid/tests/unittests/ir/inference/test_trt_dynamic_shape.py b/python/paddle/fluid/tests/unittests/ir/inference/test_trt_dynamic_shape.py index edba0a4885651..0a5ef451b93b8 100644 --- a/python/paddle/fluid/tests/unittests/ir/inference/test_trt_dynamic_shape.py +++ b/python/paddle/fluid/tests/unittests/ir/inference/test_trt_dynamic_shape.py @@ -72,8 +72,10 @@ def set_feeds(self): def test_check_output(self): if core.is_compiled_with_cuda(): use_gpu = True - # with self.assertRaises(Exception): # noqa: B017 - self.check_output_with_option(use_gpu) + with self.assertRaisesRegex( + ValueError, "The fed Variable 'data' should have dimensions" + ): + self.check_output_with_option(use_gpu) # (wanghaipeng03) temporarily disable this test, in some cases, this test code @@ -99,8 +101,10 @@ def set_feeds(self): def test_check_output(self): if core.is_compiled_with_cuda(): use_gpu = True - # with self.assertRaises(Exception): # noqa: B017 - self.check_output_with_option(use_gpu) + with self.assertRaisesRegex( + ValueError, "The fed Variable 'data' should have dimensions" + ): + self.check_output_with_option(use_gpu) if __name__ == "__main__":