From 9fec426acdf5935d67f8fbdf6d7d05f7e7f2b114 Mon Sep 17 00:00:00 2001 From: makoeppel Date: Fri, 21 Jun 2024 16:40:31 +0200 Subject: [PATCH 1/5] add type check for input name of resolve_datatype() --- src/qonnx/core/datatype.py | 4 ++++ tests/core/test_datatypes.py | 39 ++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/src/qonnx/core/datatype.py b/src/qonnx/core/datatype.py index f37d4eea..4bdffdf9 100644 --- a/src/qonnx/core/datatype.py +++ b/src/qonnx/core/datatype.py @@ -376,6 +376,10 @@ def get_canonical_name(self): def resolve_datatype(name): + + if not isinstance(name, str): + raise TypeError(f"Input 'name' must be of type 'str', but got type '{type(name).__name__}'") + _special_types = { "BINARY": IntType(1, False), "BIPOLAR": BipolarType(), diff --git a/tests/core/test_datatypes.py b/tests/core/test_datatypes.py index 1bd0fece..efd590b3 100644 --- a/tests/core/test_datatypes.py +++ b/tests/core/test_datatypes.py @@ -29,6 +29,7 @@ import numpy as np from qonnx.core.datatype import DataType +from qonnx.core.datatype import resolve_datatype def test_datatypes(): @@ -97,3 +98,41 @@ def test_smallest_possible(): assert DataType.get_smallest_possible(-1) == DataType["BIPOLAR"] assert DataType.get_smallest_possible(-3) == DataType["INT3"] assert DataType.get_smallest_possible(-3.2) == DataType["FLOAT32"] + + +def test_resolve_datatype(): + assert resolve_datatype("BIPOLAR") + assert resolve_datatype("BINARY") + assert resolve_datatype("TERNARY") + assert resolve_datatype("UINT2") + assert resolve_datatype("UINT3") + assert resolve_datatype("UINT4") + assert resolve_datatype("UINT8") + assert resolve_datatype("UINT16") + assert resolve_datatype("UINT32") + assert resolve_datatype("INT2") + assert resolve_datatype("INT3") + assert resolve_datatype("INT4") + assert resolve_datatype("INT8") + assert resolve_datatype("INT16") + assert resolve_datatype("INT32") + assert resolve_datatype("BINARY") + assert resolve_datatype("FLOAT32") + + +def test_input_type_error(): + # test with invalid input to check if the TypeError works + try: + resolve_datatype(123) # This should raise a TypeError + except TypeError as e: + pass + else: + print("Test with invalid input failed: No TypeError was raised.") + + # test with invalid input to check if the TypeError works + try: + resolve_datatype(1.23) # This should raise a TypeError + except TypeError as e: + pass + else: + print("Test with invalid input failed: No TypeError was raised.") From af8249cb529401e74dbf9d5124a10742390b6ded Mon Sep 17 00:00:00 2001 From: makoeppel Date: Fri, 21 Jun 2024 16:52:37 +0200 Subject: [PATCH 2/5] improve test_input_type_error --- tests/core/test_datatypes.py | 42 +++++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/tests/core/test_datatypes.py b/tests/core/test_datatypes.py index efd590b3..e98a885f 100644 --- a/tests/core/test_datatypes.py +++ b/tests/core/test_datatypes.py @@ -116,23 +116,35 @@ def test_resolve_datatype(): assert resolve_datatype("INT8") assert resolve_datatype("INT16") assert resolve_datatype("INT32") - assert resolve_datatype("BINARY") assert resolve_datatype("FLOAT32") def test_input_type_error(): - # test with invalid input to check if the TypeError works - try: - resolve_datatype(123) # This should raise a TypeError - except TypeError as e: - pass - else: - print("Test with invalid input failed: No TypeError was raised.") - # test with invalid input to check if the TypeError works - try: - resolve_datatype(1.23) # This should raise a TypeError - except TypeError as e: - pass - else: - print("Test with invalid input failed: No TypeError was raised.") + def test_resolve_datatype(input): + # test with invalid input to check if the TypeError works + try: + resolve_datatype(input) # This should raise a TypeError + except TypeError as e: + pass + else: + print("Test with invalid input failed: No TypeError was raised.") + + test_resolve_datatype(123) + test_resolve_datatype(1.23) + test_resolve_datatype(DataType["BIPOLAR"]) + test_resolve_datatype(DataType["BINARY"]) + test_resolve_datatype(DataType["TERNARY"]) + test_resolve_datatype(DataType["UINT2"]) + test_resolve_datatype(DataType["UINT3"]) + test_resolve_datatype(DataType["UINT4"]) + test_resolve_datatype(DataType["UINT8"]) + test_resolve_datatype(DataType["UINT16"]) + test_resolve_datatype(DataType["UINT32"]) + test_resolve_datatype(DataType["INT2"]) + test_resolve_datatype(DataType["INT3"]) + test_resolve_datatype(DataType["INT4"]) + test_resolve_datatype(DataType["INT8"]) + test_resolve_datatype(DataType["INT16"]) + test_resolve_datatype(DataType["INT32"]) + test_resolve_datatype(DataType["FLOAT32"]) From 32013c9b7bd5cdbb65b8b22d67b9e7e7d6170440 Mon Sep 17 00:00:00 2001 From: Yaman Umuroglu Date: Fri, 20 Dec 2024 10:10:12 +0100 Subject: [PATCH 3/5] trigger CI From 9e8f40e203c3969cbbd41bccecee490097f3e04e Mon Sep 17 00:00:00 2001 From: Yaman Umuroglu Date: Fri, 20 Dec 2024 10:15:47 +0100 Subject: [PATCH 4/5] [Lint, Test] run pre-commit, remove unused variable --- src/qonnx/core/datatype.py | 1 - tests/core/test_datatypes.py | 6 ++---- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/src/qonnx/core/datatype.py b/src/qonnx/core/datatype.py index 4bdffdf9..343416ba 100644 --- a/src/qonnx/core/datatype.py +++ b/src/qonnx/core/datatype.py @@ -376,7 +376,6 @@ def get_canonical_name(self): def resolve_datatype(name): - if not isinstance(name, str): raise TypeError(f"Input 'name' must be of type 'str', but got type '{type(name).__name__}'") diff --git a/tests/core/test_datatypes.py b/tests/core/test_datatypes.py index e98a885f..722a0ac4 100644 --- a/tests/core/test_datatypes.py +++ b/tests/core/test_datatypes.py @@ -28,8 +28,7 @@ import numpy as np -from qonnx.core.datatype import DataType -from qonnx.core.datatype import resolve_datatype +from qonnx.core.datatype import DataType, resolve_datatype def test_datatypes(): @@ -120,12 +119,11 @@ def test_resolve_datatype(): def test_input_type_error(): - def test_resolve_datatype(input): # test with invalid input to check if the TypeError works try: resolve_datatype(input) # This should raise a TypeError - except TypeError as e: + except TypeError: pass else: print("Test with invalid input failed: No TypeError was raised.") From 7e747d76720f0f68c93f49af6e1e03748a7a2d47 Mon Sep 17 00:00:00 2001 From: Yaman Umuroglu Date: Fri, 20 Dec 2024 10:16:42 +0100 Subject: [PATCH 5/5] [Test] make test_input_type_error actually assert failure --- tests/core/test_datatypes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/core/test_datatypes.py b/tests/core/test_datatypes.py index 722a0ac4..c33da477 100644 --- a/tests/core/test_datatypes.py +++ b/tests/core/test_datatypes.py @@ -126,7 +126,7 @@ def test_resolve_datatype(input): except TypeError: pass else: - print("Test with invalid input failed: No TypeError was raised.") + assert False, "Test with invalid input failed: No TypeError was raised." test_resolve_datatype(123) test_resolve_datatype(1.23)