ros2 / rosidl_python

rosidl support for Python
Apache License 2.0
20 stars 45 forks source link

Generated file may fail flake8 import order #171

Closed chapulina closed 2 years ago

chapulina commented 2 years ago

Bug report

Required Info:

Steps to reproduce issue

There are cases where the generated __init__.py file may fail the import order, triggering I100 flake8 warnings. For example, if you have these services:

PCVTargMaxCommand.srv
PCWindCurrCommand.srv

The generated file has them ordered like this:

from buoy_msgs.srv._pcv_targ_max_command import PCVTargMaxCommand  # noqa: F401
from buoy_msgs.srv._pc_wind_curr_command import PCWindCurrCommand  # noqa: F401

Which doesn't satisfy flake8-import-order.

Expected behavior

Generated files don't give warnings.

Actual behavior

There are corner cases where warnings are triggered.

Additional information

I believe this happens because the sorting order for the module names changes after the call to convert_camel_case_to_lower_case_underscore here:

https://github.com/ros2/rosidl_python/blob/619e43fab77081d0d35664d514fbe9c1e679a56c/rosidl_generator_py/rosidl_generator_py/generate_py_impl.py#L124-L129

One solution could be to copy the converted names to another variable and sort that with the final names before writing to the file.

Another possible solution is to just suppress that warning for every generated line, i.e.

diff --git a/rosidl_generator_py/rosidl_generator_py/generate_py_impl.py b/rosidl_generator_py/rosidl_generator_py/generate_py_impl.py
index dfa2d94..e255772 100644
--- a/rosidl_generator_py/rosidl_generator_py/generate_py_impl.py
+++ b/rosidl_generator_py/rosidl_generator_py/generate_py_impl.py
@@ -126,7 +126,7 @@ def generate_py(generator_arguments_file, typesupport_impls):
                     convert_camel_case_to_lower_case_underscore(idl_stem)
                 f.write(
                     f'from {package_name}.{subfolder}.{module_name} import '
-                    f'{idl_stem}  # noqa: F401\n')
+                    f'{idl_stem}  # noqa: F401, I100\n')

     # expand templates per available typesupport implementation
     template_dir = args['template_dir']