From 00d11855a8ce22fc4c9b3bfb857712bc6f36ca30 Mon Sep 17 00:00:00 2001 From: proneetsharma Date: Fri, 18 Jun 2021 12:35:28 +0200 Subject: [PATCH 1/5] Raise type error for the unsupported object type --- paz/processors/draw.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/paz/processors/draw.py b/paz/processors/draw.py index 991e771c2..f0a6e1235 100644 --- a/paz/processors/draw.py +++ b/paz/processors/draw.py @@ -26,10 +26,19 @@ def __init__(self, class_names=None, colors=None, self.colors = colors self.weighted = weighted self.scale = scale + + if (self.class_names is not None and + not isinstance(self.class_names, list)): + raise TypeError("Class name should be of type 'List of strings'") + + if (self.colors is not None and + not all(isinstance(color, list) for color in self.colors)): + raise TypeError("Colors should be of type 'List of lists'") + if self.colors is None: self.colors = lincolor(len(self.class_names)) - if class_names is not None: + if self.class_names is not None: self.class_to_color = dict(zip(self.class_names, self.colors)) else: self.class_to_color = {None: self.colors, '': self.colors} @@ -47,6 +56,9 @@ def call(self, image, boxes2D): draw_rectangle(image, (x_min, y_min), (x_max, y_max), color, 2) return image + def check(self): + print('check') + class DrawKeypoints2D(Processor): """Draws keypoints into image. @@ -124,3 +136,4 @@ def __init__(self, max_radius_scale=.5): def call(self, image): return draw_random_polygon(image) + From 36605b739bcef30b0ac00cf18dd241d964461800 Mon Sep 17 00:00:00 2001 From: proneetsharma Date: Fri, 18 Jun 2021 12:38:52 +0200 Subject: [PATCH 2/5] Raise type error for the unsupported object type --- paz/processors/draw.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/paz/processors/draw.py b/paz/processors/draw.py index f0a6e1235..881efe8c6 100644 --- a/paz/processors/draw.py +++ b/paz/processors/draw.py @@ -56,9 +56,6 @@ def call(self, image, boxes2D): draw_rectangle(image, (x_min, y_min), (x_max, y_max), color, 2) return image - def check(self): - print('check') - class DrawKeypoints2D(Processor): """Draws keypoints into image. From 84b5a9b2f79d3e14fbfe3eec2d735be2a0c5742c Mon Sep 17 00:00:00 2001 From: proneetsharma Date: Tue, 22 Jun 2021 20:01:21 +0200 Subject: [PATCH 3/5] Add unit test to check for the type of parameters --- tests/paz/processors/draw_test.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 tests/paz/processors/draw_test.py diff --git a/tests/paz/processors/draw_test.py b/tests/paz/processors/draw_test.py new file mode 100644 index 000000000..415e03676 --- /dev/null +++ b/tests/paz/processors/draw_test.py @@ -0,0 +1,29 @@ +import unittest +from paz import processors as pr + + +class TestDrawBoxes2D(unittest.TestCase): + """Test the type of the parameters for DrawBoxes2D class. + class_names: List of strings. + colors: List of lists containing the color values + """ + def test_parameters_type(self): + # class_name is not of required type + self.class_names = 'Face' + self.colors = [[255, 0, 0]] + with self.assertRaises(TypeError) as context: + pr.DrawBoxes2D(self.class_names, self.colors) + self.assertEqual("Class name should be of type 'List of strings'", + str(context.exception)) + + # colors is not of required type + self.class_names = ['Face'] + self.colors = [255, 0, 0] + with self.assertRaises(TypeError) as context: + pr.DrawBoxes2D(self.class_names, self.colors) + self.assertEqual("Colors should be of type 'List of lists'", + str(context.exception)) + + +if __name__ == '__main__': + unittest.main() From c26d01340be0561eb8613e896c1d924bebacac7b Mon Sep 17 00:00:00 2001 From: proneetsharma Date: Tue, 29 Jun 2021 14:35:05 +0200 Subject: [PATCH 4/5] Refactor the tests using pytest --- tests/paz/processors/draw_test.py | 39 ++++++++++--------------------- 1 file changed, 12 insertions(+), 27 deletions(-) diff --git a/tests/paz/processors/draw_test.py b/tests/paz/processors/draw_test.py index 415e03676..24017189a 100644 --- a/tests/paz/processors/draw_test.py +++ b/tests/paz/processors/draw_test.py @@ -1,29 +1,14 @@ -import unittest +import pytest from paz import processors as pr - -class TestDrawBoxes2D(unittest.TestCase): - """Test the type of the parameters for DrawBoxes2D class. - class_names: List of strings. - colors: List of lists containing the color values - """ - def test_parameters_type(self): - # class_name is not of required type - self.class_names = 'Face' - self.colors = [[255, 0, 0]] - with self.assertRaises(TypeError) as context: - pr.DrawBoxes2D(self.class_names, self.colors) - self.assertEqual("Class name should be of type 'List of strings'", - str(context.exception)) - - # colors is not of required type - self.class_names = ['Face'] - self.colors = [255, 0, 0] - with self.assertRaises(TypeError) as context: - pr.DrawBoxes2D(self.class_names, self.colors) - self.assertEqual("Colors should be of type 'List of lists'", - str(context.exception)) - - -if __name__ == '__main__': - unittest.main() +def test_DrawBoxes2D_with_invalid_class_names_type(): + with pytest.raises(TypeError): + class_names = 'Face' + colors = [[255, 0, 0]] + pr.DrawBoxes2D(class_names, colors) + +def test_DrawBoxes2D_with_invalid_colors_type(): + with pytest.raises(TypeError): + class_names = ['Face'] + colors = [255, 0, 0] + pr.DrawBoxes2D(class_names, colors) From d63f2af9f56a15e7cbee5dbd923c330e01b27258 Mon Sep 17 00:00:00 2001 From: proneetsharma Date: Tue, 6 Jul 2021 13:28:27 +0200 Subject: [PATCH 5/5] Refactor code using linter --- paz/processors/draw.py | 1 - tests/paz/processors/draw_test.py | 4 +++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/paz/processors/draw.py b/paz/processors/draw.py index 881efe8c6..41a92ad81 100644 --- a/paz/processors/draw.py +++ b/paz/processors/draw.py @@ -133,4 +133,3 @@ def __init__(self, max_radius_scale=.5): def call(self, image): return draw_random_polygon(image) - diff --git a/tests/paz/processors/draw_test.py b/tests/paz/processors/draw_test.py index 24017189a..7742445e7 100644 --- a/tests/paz/processors/draw_test.py +++ b/tests/paz/processors/draw_test.py @@ -1,12 +1,14 @@ import pytest from paz import processors as pr + def test_DrawBoxes2D_with_invalid_class_names_type(): with pytest.raises(TypeError): class_names = 'Face' colors = [[255, 0, 0]] pr.DrawBoxes2D(class_names, colors) - + + def test_DrawBoxes2D_with_invalid_colors_type(): with pytest.raises(TypeError): class_names = ['Face']