Skip to content

Commit

Permalink
Improve Python typing annotations (#152)
Browse files Browse the repository at this point in the history
Signed-off-by: Christophe Bedard <[email protected]>
  • Loading branch information
christophebedard authored Feb 24, 2025
1 parent 4422a68 commit 66d2c69
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 20 deletions.
2 changes: 2 additions & 0 deletions tracetools_launch/setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
script_dir=$base/lib/tracetools_launch
[install]
install_scripts=$base/lib/tracetools_launch
[mypy]
warn-unused-ignores = True
37 changes: 21 additions & 16 deletions tracetools_launch/tracetools_launch/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from typing import Dict
from typing import Iterable
from typing import List
from typing import Mapping
from typing import Optional
from typing import Text
from typing import Union
Expand All @@ -34,6 +35,7 @@
from launch.frontend import Parser
from launch.launch_context import LaunchContext
from launch.some_substitutions_type import SomeSubstitutionsType
from launch.substitution import Substitution
from launch.substitutions import TextSubstitution
from launch.utilities import normalize_to_list_of_substitutions
from launch.utilities import perform_substitutions
Expand Down Expand Up @@ -159,59 +161,62 @@ def __init__(
self._base_path = base_path \
if base_path is None else normalize_to_list_of_substitutions(base_path)
self._append_trace = append_trace
self._trace_directory = None
self._trace_directory: Optional[str] = None
self._events_ust = [normalize_to_list_of_substitutions(x) for x in events_ust]
self._events_kernel = [normalize_to_list_of_substitutions(x) for x in events_kernel]
self._syscalls = [normalize_to_list_of_substitutions(x) for x in syscalls]
self._context_fields = \
self._context_fields = (
{
domain: [normalize_to_list_of_substitutions(field) for field in fields]
for domain, fields in context_fields.items()
} \
if isinstance(context_fields, dict) \
}
if isinstance(context_fields, dict)
else [normalize_to_list_of_substitutions(field) for field in context_fields]
self._ld_preload_actions: List[LdPreload] = []
)
self._ld_preload_actions: List[Action] = []
self._subbuffer_size_ust = subbuffer_size_ust
self._subbuffer_size_kernel = subbuffer_size_kernel

@property
def session_name(self):
def session_name(self) -> List[Substitution]:
return self._session_name

@property
def base_path(self):
def base_path(self) -> Optional[List[Substitution]]:
return self._base_path

@property
def append_trace(self):
def append_trace(self) -> bool:
return self._append_trace

@property
def trace_directory(self):
def trace_directory(self) -> Optional[str]:
return self._trace_directory

@property
def events_ust(self):
def events_ust(self) -> Iterable[List[Substitution]]:
return self._events_ust

@property
def events_kernel(self):
def events_kernel(self) -> Iterable[List[Substitution]]:
return self._events_kernel

@property
def syscalls(self):
def syscalls(self) -> Iterable[List[Substitution]]:
return self._syscalls

@property
def context_fields(self):
def context_fields(
self,
) -> Union[Mapping[str, Iterable[List[Substitution]]], Iterable[List[Substitution]]]:
return self._context_fields

@property
def subbuffer_size_ust(self):
def subbuffer_size_ust(self) -> int:
return self._subbuffer_size_ust

@property
def subbuffer_size_kernel(self):
def subbuffer_size_kernel(self) -> int:
return self._subbuffer_size_kernel

@classmethod
Expand Down Expand Up @@ -459,7 +464,7 @@ def _destroy(self, event: Event, context: LaunchContext) -> None:
self._logger.debug(f'Finalizing tracing session: {self._session_name}')
lttng.lttng_fini(session_name=self._session_name)

def __repr__(self):
def __repr__(self) -> Text:
return (
'Trace('
f'session_name={self._session_name}, '
Expand Down
15 changes: 13 additions & 2 deletions tracetools_launch/tracetools_launch/actions/ld_preload.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import subprocess
from typing import List
from typing import Optional
from typing import Text

from launch import logging
from launch.action import Action
Expand All @@ -45,7 +46,7 @@ def __init__(
"""
super().__init__(**kwargs)
self._lib_name = lib_name
self._env_action = None
self._env_action: Optional[Action] = None
# Try to find lib
self._lib_path = self.get_shared_lib_path(self._lib_name)
# And create action if found
Expand All @@ -65,9 +66,19 @@ def lib_name(self) -> str:

@property
def lib_path(self) -> Optional[str]:
"""
Get the resolved shared lib path, if found.
:return: the full path to the shared lib if found, or `None` if not found
"""
return self._lib_path

def lib_found(self) -> bool:
"""
Check if the shared lib was found.
:return: `True` if the shared lib was found, or `False` if not found
"""
return self._env_action is not None

def execute(self, context: LaunchContext) -> Optional[List[Action]]:
Expand Down Expand Up @@ -113,7 +124,7 @@ def get_shared_lib_path(cls, lib_name: str) -> Optional[str]:
shared_lib = shared_lib_paths[0]
return shared_lib

def __repr__(self):
def __repr__(self) -> Text:
return (
'LdPreload('
f'lib_name={self._lib_name}, '
Expand Down
2 changes: 2 additions & 0 deletions tracetools_read/setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
script_dir=$base/lib/tracetools_read
[install]
install_scripts=$base/lib/tracetools_read
[mypy]
warn-unused-ignores = True
2 changes: 2 additions & 0 deletions tracetools_test/setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
script_dir=$base/lib/tracetools_test
[install]
install_scripts=$base/lib/tracetools_test
[mypy]
warn-unused-ignores = True
4 changes: 2 additions & 2 deletions tracetools_test/tracetools_test/case.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def __init__(
self._additional_actions = additional_actions or []
self._namespace = namespace

def setUp(self):
def setUp(self) -> None:
# Get timestamp before trace (ns)
timestamp_before = int(time.time() * 1000000000.0)

Expand Down Expand Up @@ -133,7 +133,7 @@ def setUp(self):
# Check that the launched nodes are present as processes
self.assertProcessNamesExist(self._nodes)

def tearDown(self):
def tearDown(self) -> None:
if not os.environ.get(self.ENV_VAR_DEBUG, None):
cleanup_trace(self._full_path)

Expand Down
2 changes: 2 additions & 0 deletions tracetools_trace/setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
script_dir=$base/lib/tracetools_trace
[install]
install_scripts=$base/lib/tracetools_trace
[mypy]
warn-unused-ignores = True

0 comments on commit 66d2c69

Please sign in to comment.