From e12f9d6913ea782acd3d80edb7b078a81ec7a2fb Mon Sep 17 00:00:00 2001 From: Paul Gesel Date: Fri, 22 Mar 2024 12:49:17 -0700 Subject: [PATCH 1/7] fix test on rolling CI Signed-off-by: Paul Gesel --- .../test/YAML_parse_error_test.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/generate_parameter_library_py/generate_parameter_library_py/test/YAML_parse_error_test.py b/generate_parameter_library_py/generate_parameter_library_py/test/YAML_parse_error_test.py index 7afb837..aad425c 100644 --- a/generate_parameter_library_py/generate_parameter_library_py/test/YAML_parse_error_test.py +++ b/generate_parameter_library_py/generate_parameter_library_py/test/YAML_parse_error_test.py @@ -81,6 +81,11 @@ def set_up(yaml_test_file): ], ) def test_expected(test_input, expected): + if sys.flags.optimize: + if test_input == 'wrong_default_type.yaml': + pytest.skip( + 'Evaluating optimized type validation cannot be completed when python is optimized' + ) with pytest.raises(expected) as e: yaml_test_file = test_input set_up(yaml_test_file) From 6b78abb1e2a06b4589c900909b1577d9cd8c29aa Mon Sep 17 00:00:00 2001 From: Paul Gesel Date: Fri, 22 Mar 2024 13:13:59 -0700 Subject: [PATCH 2/7] use __debug__ Signed-off-by: Paul Gesel --- .../generate_parameter_library_py/test/YAML_parse_error_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/generate_parameter_library_py/generate_parameter_library_py/test/YAML_parse_error_test.py b/generate_parameter_library_py/generate_parameter_library_py/test/YAML_parse_error_test.py index aad425c..56b9b4d 100644 --- a/generate_parameter_library_py/generate_parameter_library_py/test/YAML_parse_error_test.py +++ b/generate_parameter_library_py/generate_parameter_library_py/test/YAML_parse_error_test.py @@ -81,7 +81,7 @@ def set_up(yaml_test_file): ], ) def test_expected(test_input, expected): - if sys.flags.optimize: + if not __debug__ or sys.flags.optimize: if test_input == 'wrong_default_type.yaml': pytest.skip( 'Evaluating optimized type validation cannot be completed when python is optimized' From 5c534d6fd58747d8d9fdcd4856810874c170fee3 Mon Sep 17 00:00:00 2001 From: Paul Gesel Date: Fri, 2 Aug 2024 08:52:29 -0600 Subject: [PATCH 3/7] remove Werror Signed-off-by: Paul Gesel --- .github/workflows/ci.yaml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 01c7041..4db4f1c 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -19,10 +19,8 @@ jobs: # Werror is not enabled on humble and iron because of gtest - ROS_DISTRO: rolling ROS_REPO: testing - CMAKE_ARGS: -DCMAKE_CXX_FLAGS=-Werror - ROS_DISTRO: rolling ROS_REPO: main - CMAKE_ARGS: -DCMAKE_CXX_FLAGS=-Werror - ROS_DISTRO: iron ROS_REPO: testing - ROS_DISTRO: iron From 03f753c286b8acc67e10522194adc4da93201226 Mon Sep 17 00:00:00 2001 From: Paul Gesel Date: Fri, 2 Aug 2024 09:49:37 -0600 Subject: [PATCH 4/7] use new type error from typeguard Signed-off-by: Paul Gesel --- .github/workflows/ci.yaml | 1 - .../generate_parameter_library_py/parse_yaml.py | 8 +++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 4db4f1c..41ec546 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -16,7 +16,6 @@ jobs: fail-fast: false matrix: env: - # Werror is not enabled on humble and iron because of gtest - ROS_DISTRO: rolling ROS_REPO: testing - ROS_DISTRO: rolling diff --git a/generate_parameter_library_py/generate_parameter_library_py/parse_yaml.py b/generate_parameter_library_py/generate_parameter_library_py/parse_yaml.py index 1e8e7f4..05b23f8 100644 --- a/generate_parameter_library_py/generate_parameter_library_py/parse_yaml.py +++ b/generate_parameter_library_py/generate_parameter_library_py/parse_yaml.py @@ -31,6 +31,12 @@ from jinja2 import Template, Environment from typeguard import typechecked + +try: + from typeguard import TypeCheckError +except: + TypeCheckError = TypeError + from typing import Any, List, Optional from yaml.parser import ParserError from yaml.scanner import ScannerError @@ -190,7 +196,7 @@ def __init__( func = self.conversation.lang_str_value_func[self.defined_type] try: self.lang_str_value = func(default_value) - except TypeError: + except TypeCheckError: raise compile_error( f'Parameter {param_name} has incorrect type. Expected: {defined_type}, got: {self.get_yaml_type_from_python(default_value)}' ) From 4e6975ea1d22eda43d82e6274fef2546d4b5dcc0 Mon Sep 17 00:00:00 2001 From: Paul Gesel Date: Fri, 2 Aug 2024 10:10:32 -0600 Subject: [PATCH 5/7] replace Optional with Union Signed-off-by: Paul Gesel --- .../cpp_convertions.py | 30 +++++++++---------- .../parse_yaml.py | 4 +-- .../python_convertions.py | 30 +++++++++---------- .../test/YAML_parse_error_test.py | 1 + 4 files changed, 33 insertions(+), 32 deletions(-) diff --git a/generate_parameter_library_py/generate_parameter_library_py/cpp_convertions.py b/generate_parameter_library_py/generate_parameter_library_py/cpp_convertions.py index 01cf619..c1fa76d 100644 --- a/generate_parameter_library_py/generate_parameter_library_py/cpp_convertions.py +++ b/generate_parameter_library_py/generate_parameter_library_py/cpp_convertions.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -from typing import List, Optional +from typing import List, Union from jinja2 import Template from typeguard import typechecked import os @@ -108,18 +108,18 @@ def update_parameter_pass_validation(self) -> str: return '' @typechecked - def no_code(self, s: Optional[str]): + def no_code(self, s: Union[None, str]): return '' # value to c++ string conversion functions @typechecked - def bool_to_str(self, cond: Optional[bool]): + def bool_to_str(self, cond: Union[None, bool]): if cond is None: return '' return 'true' if cond else 'false' @typechecked - def float_to_str(self, num: Optional[float]): + def float_to_str(self, num: Union[None, float]): if num is None: return '' str_num = str(num) @@ -136,65 +136,65 @@ def float_to_str(self, num: Optional[float]): return str_num @typechecked - def int_to_str(self, num: Optional[int]): + def int_to_str(self, num: Union[None, int]): if num is None: return '' return str(num) @typechecked - def str_to_str(self, s: Optional[str]): + def str_to_str(self, s: Union[None, str]): if s is None: return '' return f'"{s}"' @typechecked - def bool_array_to_str(self, values: Optional[list]): + def bool_array_to_str(self, values: Union[None, list]): if values is None: return '' return '{' + ', '.join(self.bool_to_str(x) for x in values) + '}' @typechecked - def float_array_to_str(self, values: Optional[list]): + def float_array_to_str(self, values: Union[None, list]): if values is None: return '' return '{' + ', '.join(self.float_to_str(x) for x in values) + '}' @typechecked - def int_array_to_str(self, values: Optional[list]): + def int_array_to_str(self, values: Union[None, list]): if values is None: return '' return '{' + ', '.join(self.int_to_str(x) for x in values) + '}' @typechecked - def str_array_to_str(self, s: Optional[list]): + def str_array_to_str(self, s: Union[None, list]): if s is None: return '' return '{' + ', '.join(self.str_to_str(x) for x in s) + '}' @typechecked - def str_array_fixed_to_str(self, s: Optional[list]): + def str_array_fixed_to_str(self, s: Union[None, list]): raise compile_error('not implemented') @typechecked - def str_fixed_to_str(self, s: Optional[str]): + def str_fixed_to_str(self, s: Union[None, str]): if s is None: return '' return '{%s}' % self.str_to_str(s) @typechecked - def float_array_fixed_to_str(self, values: Optional[list]): + def float_array_fixed_to_str(self, values: Union[None, list]): if values is None: return '' return '{{' + ', '.join(self.float_to_str(x) for x in values) + '}}' @typechecked - def int_array_fixed_to_str(self, values: Optional[list]): + def int_array_fixed_to_str(self, values: Union[None, list]): if values is None: return '' return '{{' + ', '.join(self.int_to_str(x) for x in values) + '}}' @typechecked - def bool_array_fixed_to_str(self, values: Optional[list]): + def bool_array_fixed_to_str(self, values: Union[None, list]): if values is None: return '' return '{{' + ', '.join(self.bool_to_str(x) for x in values) + '}}' diff --git a/generate_parameter_library_py/generate_parameter_library_py/parse_yaml.py b/generate_parameter_library_py/generate_parameter_library_py/parse_yaml.py index 05b23f8..a762718 100644 --- a/generate_parameter_library_py/generate_parameter_library_py/parse_yaml.py +++ b/generate_parameter_library_py/generate_parameter_library_py/parse_yaml.py @@ -37,7 +37,7 @@ except: TypeCheckError = TypeError -from typing import Any, List, Optional +from typing import Any, List, Union from yaml.parser import ParserError from yaml.scanner import ScannerError import os @@ -320,7 +320,7 @@ class ValidationFunction: def __init__( self, function_name: str, - arguments: Optional[List[Any]], + arguments: Union[None, List[Any]], code_gen_variable: CodeGenVariableBase, ): self.code_gen_variable = code_gen_variable diff --git a/generate_parameter_library_py/generate_parameter_library_py/python_convertions.py b/generate_parameter_library_py/generate_parameter_library_py/python_convertions.py index c171f6c..0a09dd0 100644 --- a/generate_parameter_library_py/generate_parameter_library_py/python_convertions.py +++ b/generate_parameter_library_py/generate_parameter_library_py/python_convertions.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -from typing import List, Optional +from typing import List, Union from jinja2 import Template from typeguard import typechecked import os @@ -107,18 +107,18 @@ def update_parameter_pass_validation(self) -> str: return '' @typechecked - def no_code(self, s: Optional[str]): + def no_code(self, s: Union[None, str]): return '' # value to c++ string conversion functions @typechecked - def bool_to_str(self, cond: Optional[bool]): + def bool_to_str(self, cond: Union[None, bool]): if cond is None: return '' return 'True' if cond else 'False' @typechecked - def float_to_str(self, num: Optional[float]): + def float_to_str(self, num: Union[None, float]): if num is None: return '' str_num = str(num) @@ -135,65 +135,65 @@ def float_to_str(self, num: Optional[float]): return str_num @typechecked - def int_to_str(self, num: Optional[int]): + def int_to_str(self, num: Union[None, int]): if num is None: return '' return str(num) @typechecked - def str_to_str(self, s: Optional[str]): + def str_to_str(self, s: Union[None, str]): if s is None: return '' return f'"{s}"' @typechecked - def bool_array_to_str(self, values: Optional[list]): + def bool_array_to_str(self, values: Union[None, list]): if values is None: return '' return '[' + ', '.join(self.bool_to_str(x) for x in values) + ']' @typechecked - def float_array_to_str(self, values: Optional[list]): + def float_array_to_str(self, values: Union[None, list]): if values is None: return '' return '[' + ', '.join(self.float_to_str(x) for x in values) + ']' @typechecked - def int_array_to_str(self, values: Optional[list]): + def int_array_to_str(self, values: Union[None, list]): if values is None: return '' return '[' + ', '.join(self.int_to_str(x) for x in values) + ']' @typechecked - def str_array_to_str(self, s: Optional[list]): + def str_array_to_str(self, s: Union[None, list]): if s is None: return '' return '[' + ', '.join(self.str_to_str(x) for x in s) + ']' @typechecked - def str_array_fixed_to_str(self, s: Optional[list]): + def str_array_fixed_to_str(self, s: Union[None, list]): raise compile_error('not implemented') @typechecked - def str_fixed_to_str(self, s: Optional[str]): + def str_fixed_to_str(self, s: Union[None, str]): if s is None: return '' return '%s' % self.str_to_str(s) @typechecked - def float_array_fixed_to_str(self, values: Optional[list]): + def float_array_fixed_to_str(self, values: Union[None, list]): if values is None: return '' return '[' + ', '.join(self.float_to_str(x) for x in values) + ']' @typechecked - def int_array_fixed_to_str(self, values: Optional[list]): + def int_array_fixed_to_str(self, values: Union[None, list]): if values is None: return '' return '[' + ', '.join(self.int_to_str(x) for x in values) + ']' @typechecked - def bool_array_fixed_to_str(self, values: Optional[list]): + def bool_array_fixed_to_str(self, values: Union[None, list]): if values is None: return '' return '[' + ', '.join(self.bool_to_str(x) for x in values) + ']' diff --git a/generate_parameter_library_py/generate_parameter_library_py/test/YAML_parse_error_test.py b/generate_parameter_library_py/generate_parameter_library_py/test/YAML_parse_error_test.py index 56b9b4d..9318668 100644 --- a/generate_parameter_library_py/generate_parameter_library_py/test/YAML_parse_error_test.py +++ b/generate_parameter_library_py/generate_parameter_library_py/test/YAML_parse_error_test.py @@ -86,6 +86,7 @@ def test_expected(test_input, expected): pytest.skip( 'Evaluating optimized type validation cannot be completed when python is optimized' ) + with pytest.raises(expected) as e: yaml_test_file = test_input set_up(yaml_test_file) From 1a8322de2093ffc43bbd00a791c3668dfba76de4 Mon Sep 17 00:00:00 2001 From: Paul Gesel Date: Fri, 2 Aug 2024 10:14:25 -0600 Subject: [PATCH 6/7] remove debug check Signed-off-by: Paul Gesel --- .../test/YAML_parse_error_test.py | 6 ------ 1 file changed, 6 deletions(-) diff --git a/generate_parameter_library_py/generate_parameter_library_py/test/YAML_parse_error_test.py b/generate_parameter_library_py/generate_parameter_library_py/test/YAML_parse_error_test.py index 9318668..7afb837 100644 --- a/generate_parameter_library_py/generate_parameter_library_py/test/YAML_parse_error_test.py +++ b/generate_parameter_library_py/generate_parameter_library_py/test/YAML_parse_error_test.py @@ -81,12 +81,6 @@ def set_up(yaml_test_file): ], ) def test_expected(test_input, expected): - if not __debug__ or sys.flags.optimize: - if test_input == 'wrong_default_type.yaml': - pytest.skip( - 'Evaluating optimized type validation cannot be completed when python is optimized' - ) - with pytest.raises(expected) as e: yaml_test_file = test_input set_up(yaml_test_file) From 0760aac9e5ace836bdd3c49cbffed8fc984b9d1d Mon Sep 17 00:00:00 2001 From: Paul Gesel Date: Fri, 2 Aug 2024 10:18:53 -0600 Subject: [PATCH 7/7] add comment to explain TypeError change Signed-off-by: Paul Gesel --- .../generate_parameter_library_py/parse_yaml.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/generate_parameter_library_py/generate_parameter_library_py/parse_yaml.py b/generate_parameter_library_py/generate_parameter_library_py/parse_yaml.py index a762718..7bb1fec 100644 --- a/generate_parameter_library_py/generate_parameter_library_py/parse_yaml.py +++ b/generate_parameter_library_py/generate_parameter_library_py/parse_yaml.py @@ -32,9 +32,11 @@ from jinja2 import Template, Environment from typeguard import typechecked +# try to import TypeCheckError from typeguard. This was breaking and replaced TypeError in 3.0.0 try: from typeguard import TypeCheckError -except: +except ImportError as e: + # otherwise, use the old TypeError TypeCheckError = TypeError from typing import Any, List, Union