From 9450bfeea989b2e11885066c5986e9719e12dc34 Mon Sep 17 00:00:00 2001 From: Christophe Bedard Date: Sun, 1 Mar 2020 11:50:31 -0500 Subject: [PATCH] Add mypy tests through ament_mypy Signed-off-by: Christophe Bedard --- ros2trace/package.xml | 1 + ros2trace/test/test_mypy.py | 22 +++++++++++++++++++ tracetools_launch/package.xml | 1 + tracetools_launch/test/test_mypy.py | 22 +++++++++++++++++++ .../tracetools_launch/test_trace_action.py | 9 ++++---- tracetools_launch/tracetools_launch/action.py | 2 +- tracetools_read/package.xml | 1 + tracetools_read/test/test_mypy.py | 22 +++++++++++++++++++ tracetools_read/tracetools_read/trace.py | 12 +++++----- tracetools_test/package.xml | 1 + tracetools_trace/package.xml | 1 + tracetools_trace/test/test_mypy.py | 22 +++++++++++++++++++ .../tracetools_trace/tools/args.py | 16 +++++++------- .../tracetools_trace/tools/lttng.py | 4 ++-- .../tracetools_trace/tools/lttng_impl.py | 4 ++-- 15 files changed, 117 insertions(+), 23 deletions(-) create mode 100644 ros2trace/test/test_mypy.py create mode 100644 tracetools_launch/test/test_mypy.py create mode 100644 tracetools_read/test/test_mypy.py create mode 100644 tracetools_trace/test/test_mypy.py diff --git a/ros2trace/package.xml b/ros2trace/package.xml index d78f60d..e7e83e4 100644 --- a/ros2trace/package.xml +++ b/ros2trace/package.xml @@ -14,6 +14,7 @@ ament_copyright ament_flake8 + ament_mypy ament_pep257 ament_xmllint python3-pytest diff --git a/ros2trace/test/test_mypy.py b/ros2trace/test/test_mypy.py new file mode 100644 index 0000000..331a3b8 --- /dev/null +++ b/ros2trace/test/test_mypy.py @@ -0,0 +1,22 @@ +# Copyright 2019 Canonical Ltd +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from ament_mypy.main import main +import pytest + + +@pytest.mark.mypy +@pytest.mark.linter +def test_mypy(): + assert main(argv=[]) == 0, 'Found errors' diff --git a/tracetools_launch/package.xml b/tracetools_launch/package.xml index 774e5ef..0f9e6da 100644 --- a/tracetools_launch/package.xml +++ b/tracetools_launch/package.xml @@ -15,6 +15,7 @@ ament_copyright ament_flake8 + ament_mypy ament_pep257 ament_xmllint python3-pytest diff --git a/tracetools_launch/test/test_mypy.py b/tracetools_launch/test/test_mypy.py new file mode 100644 index 0000000..331a3b8 --- /dev/null +++ b/tracetools_launch/test/test_mypy.py @@ -0,0 +1,22 @@ +# Copyright 2019 Canonical Ltd +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from ament_mypy.main import main +import pytest + + +@pytest.mark.mypy +@pytest.mark.linter +def test_mypy(): + assert main(argv=[]) == 0, 'Found errors' diff --git a/tracetools_launch/test/tracetools_launch/test_trace_action.py b/tracetools_launch/test/tracetools_launch/test_trace_action.py index ed346ad..f7ae381 100644 --- a/tracetools_launch/test/tracetools_launch/test_trace_action.py +++ b/tracetools_launch/test/tracetools_launch/test_trace_action.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +from typing import List import unittest from tracetools_launch.action import Trace @@ -26,7 +27,7 @@ class TestTraceAction(unittest.TestCase): ) def test_has_profiling_events(self) -> None: - events_lists_match = [ + events_lists_match: List[List[str]] = [ [ 'lttng_ust_cyg_profile_fast:func_entry', 'hashtag:yopo', @@ -37,7 +38,7 @@ class TestTraceAction(unittest.TestCase): 'lttng_ust_cyg_profile:func_exit', ], ] - events_lists_no_match = [ + events_lists_no_match: List[List[str]] = [ [ 'lttng_ust_statedump:bin_info', 'ros2:event', @@ -50,7 +51,7 @@ class TestTraceAction(unittest.TestCase): self.assertFalse(Trace.has_profiling_events(events)) def test_has_ust_memory_events(self) -> None: - events_lists_match = [ + events_lists_match: List[List[str]] = [ [ 'hashtag:yopo', 'lttng_ust_libc:malloc', @@ -60,7 +61,7 @@ class TestTraceAction(unittest.TestCase): 'lttng_ust_libc:still_a_match', ], ] - events_lists_no_match = [ + events_lists_no_match: List[List[str]] = [ [], [ 'my_random:event', diff --git a/tracetools_launch/tracetools_launch/action.py b/tracetools_launch/tracetools_launch/action.py index 14864e8..d22da5f 100644 --- a/tracetools_launch/tracetools_launch/action.py +++ b/tracetools_launch/tracetools_launch/action.py @@ -77,7 +77,7 @@ class Trace(Action): self.__context_names = context_names self.__profile_fast = profile_fast self.__logger = logging.get_logger(__name__) - self.__ld_preload_actions = [] + self.__ld_preload_actions: List[LdPreload] = [] # Add LD_PRELOAD actions if corresponding events are enabled if self.has_profiling_events(self.__events_ust): self.__ld_preload_actions.append( diff --git a/tracetools_read/package.xml b/tracetools_read/package.xml index 6b552de..b323797 100644 --- a/tracetools_read/package.xml +++ b/tracetools_read/package.xml @@ -13,6 +13,7 @@ ament_copyright ament_flake8 + ament_mypy ament_pep257 ament_xmllint python3-pytest diff --git a/tracetools_read/test/test_mypy.py b/tracetools_read/test/test_mypy.py new file mode 100644 index 0000000..331a3b8 --- /dev/null +++ b/tracetools_read/test/test_mypy.py @@ -0,0 +1,22 @@ +# Copyright 2019 Canonical Ltd +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from ament_mypy.main import main +import pytest + + +@pytest.mark.mypy +@pytest.mark.linter +def test_mypy(): + assert main(argv=[]) == 0, 'Found errors' diff --git a/tracetools_read/tracetools_read/trace.py b/tracetools_read/tracetools_read/trace.py index 73ae513..01bf3c0 100644 --- a/tracetools_read/tracetools_read/trace.py +++ b/tracetools_read/tracetools_read/trace.py @@ -23,9 +23,6 @@ import babeltrace from . import DictEvent -BabeltraceEvent = babeltrace.babeltrace.Event - - def is_trace_directory(path: str) -> bool: """ Check recursively if a path is a trace directory. @@ -42,7 +39,7 @@ def is_trace_directory(path: str) -> bool: return traces is not None and len(traces) > 0 -def get_trace_ctf_events(trace_directory: str) -> Iterable[BabeltraceEvent]: +def get_trace_ctf_events(trace_directory: str) -> Iterable[babeltrace.babeltrace.Event]: """ Get the events of a trace. @@ -61,7 +58,10 @@ def get_trace_events(trace_directory: str) -> List[DictEvent]: :param trace_directory: the path to the main/top trace directory :return: events """ - return [event_to_dict(event) for event in get_trace_ctf_events(trace_directory)] + events: List[DictEvent] = [ + event_to_dict(event) for event in get_trace_ctf_events(trace_directory) + ] + return events # List of ignored CTF fields @@ -82,7 +82,7 @@ _IGNORED_FIELDS = [ _DISCARD = 'events_discarded' -def event_to_dict(event: BabeltraceEvent) -> DictEvent: +def event_to_dict(event: babeltrace.babeltrace.Event) -> DictEvent: """ Convert name, timestamp, and all other keys except those in IGNORED_FIELDS into a dictionary. diff --git a/tracetools_test/package.xml b/tracetools_test/package.xml index 180dbf6..8421443 100644 --- a/tracetools_test/package.xml +++ b/tracetools_test/package.xml @@ -20,6 +20,7 @@ std_msgs std_srvs + ament_cmake_mypy ament_cmake_pytest ament_lint_auto ament_lint_common diff --git a/tracetools_trace/package.xml b/tracetools_trace/package.xml index bad23ae..8dc55d8 100644 --- a/tracetools_trace/package.xml +++ b/tracetools_trace/package.xml @@ -13,6 +13,7 @@ ament_copyright ament_flake8 + ament_mypy ament_pep257 ament_xmllint python3-pytest diff --git a/tracetools_trace/test/test_mypy.py b/tracetools_trace/test/test_mypy.py new file mode 100644 index 0000000..331a3b8 --- /dev/null +++ b/tracetools_trace/test/test_mypy.py @@ -0,0 +1,22 @@ +# Copyright 2019 Canonical Ltd +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from ament_mypy.main import main +import pytest + + +@pytest.mark.mypy +@pytest.mark.linter +def test_mypy(): + assert main(argv=[]) == 0, 'Found errors' diff --git a/tracetools_trace/tracetools_trace/tools/args.py b/tracetools_trace/tracetools_trace/tools/args.py index 7a98af7..18ec0b8 100644 --- a/tracetools_trace/tracetools_trace/tools/args.py +++ b/tracetools_trace/tracetools_trace/tools/args.py @@ -31,14 +31,14 @@ class DefaultArgValueCompleter: return self.list -def parse_args(): +def parse_args() -> argparse.Namespace: """Parse args for tracing.""" parser = argparse.ArgumentParser(description='Setup and launch an LTTng tracing session.') add_arguments(parser) return parser.parse_args() -def add_arguments(parser: argparse.ArgumentParser): +def add_arguments(parser: argparse.ArgumentParser) -> None: parser.add_argument( '-s', '--session-name', dest='session_name', default=path.append_timestamp('session'), @@ -47,27 +47,27 @@ def add_arguments(parser: argparse.ArgumentParser): '-p', '--path', dest='path', default=path.DEFAULT_BASE_PATH, help='path of the base directory for trace data (default: %(default)s)') - arg = parser.add_argument( + events_ust_arg = parser.add_argument( # type: ignore '-u', '--ust', nargs='*', dest='events_ust', default=names.DEFAULT_EVENTS_ROS, help='the ROS UST events to enable (default: see tracetools_trace.tools.names) ' '[to disable all UST events, ' 'provide this flag without any event name]') - arg.completer = DefaultArgValueCompleter(arg) - arg = parser.add_argument( + events_ust_arg.completer = DefaultArgValueCompleter(events_ust_arg) # type: ignore + events_kernel_arg = parser.add_argument( # type: ignore '-k', '--kernel', nargs='*', dest='events_kernel', default=names.DEFAULT_EVENTS_KERNEL, help='the kernel events to enable (default: see tracetools_trace.tools.names) ' '[to disable all kernel events, ' 'provide this flag without any event name]') - arg.completer = DefaultArgValueCompleter(arg) - arg = parser.add_argument( + events_kernel_arg.completer = DefaultArgValueCompleter(events_kernel_arg) # type: ignore + context_arg = parser.add_argument( # type: ignore '-c', '--context', nargs='*', dest='context_names', default=names.DEFAULT_CONTEXT, help='the context names to enable (default: see tracetools_trace.tools.names) ' '[to disable all context names, ' 'provide this flag without any name]') - arg.completer = DefaultArgValueCompleter(arg) + context_arg.completer = DefaultArgValueCompleter(context_arg) # type: ignore parser.add_argument( '-l', '--list', dest='list', action='store_true', help='display lists of enabled events and context names (default: %(default)s)') diff --git a/tracetools_trace/tracetools_trace/tools/lttng.py b/tracetools_trace/tracetools_trace/tools/lttng.py index 782b9cf..ad402ef 100644 --- a/tracetools_trace/tracetools_trace/tools/lttng.py +++ b/tracetools_trace/tracetools_trace/tools/lttng.py @@ -22,7 +22,7 @@ from typing import Optional try: from . import lttng_impl - _lttng = lttng_impl + _lttng = lttng_impl # type: ignore # Check lttng module version from distutils.version import StrictVersion @@ -37,7 +37,7 @@ except ImportError: # Fall back on stub functions so that this still passes linter checks from . import lttng_stub - _lttng = lttng_stub + _lttng = lttng_stub # type: ignore from .names import DEFAULT_CONTEXT from .names import DEFAULT_EVENTS_KERNEL diff --git a/tracetools_trace/tracetools_trace/tools/lttng_impl.py b/tracetools_trace/tracetools_trace/tools/lttng_impl.py index 578c4a6..2fca29c 100644 --- a/tracetools_trace/tracetools_trace/tools/lttng_impl.py +++ b/tracetools_trace/tracetools_trace/tools/lttng_impl.py @@ -41,7 +41,7 @@ def get_version() -> Union[StrictVersion, None]: :return: the version as a StrictVersion object, or `None` if it cannot be extracted """ doc_lines = lttng.__doc__.split('\n') - first_line = list(filter(None, doc_lines))[0] + first_line: str = list(filter(None, doc_lines))[0] version_string = first_line.split(' ')[1] if not re.compile(r'^[0-9]+\.[0-9]+\.[0-9]+$').match(version_string): return None @@ -144,7 +144,7 @@ def setup( # TODO make it possible to add context in userspace and kernel separately, since some context # types might only apply to userspace OR kernel; only consider userspace contexts for now handles_context = [handle_ust] - enabled_handles = list(filter(None, handles_context)) + enabled_handles: List[lttng.Handle] = list(filter(None, handles_context)) _add_context(enabled_handles, context_list) return full_path