From fbccc987dceeeb50784ff8f7d8c8ad2b1b3a9a0c Mon Sep 17 00:00:00 2001 From: Michael Carlstrom <36806982+InvincibleRMC@users.noreply.github.com> Date: Thu, 21 Mar 2024 04:32:27 -0700 Subject: [PATCH] Add type annotations to python files. (#93) * Add type annotations Signed-off-by: Michael Carlstrom --- ament_index_python/ament_index_python/cli.py | 19 +++++++++++++------ .../ament_index_python/packages.py | 10 ++++++---- .../ament_index_python/py.typed | 0 .../ament_index_python/resources.py | 18 ++++++++++++------ .../ament_index_python/search_paths.py | 3 ++- 5 files changed, 33 insertions(+), 17 deletions(-) create mode 100644 ament_index_python/ament_index_python/py.typed diff --git a/ament_index_python/ament_index_python/cli.py b/ament_index_python/ament_index_python/cli.py index 1edfd62..4fa8f67 100644 --- a/ament_index_python/ament_index_python/cli.py +++ b/ament_index_python/ament_index_python/cli.py @@ -14,13 +14,14 @@ import argparse import sys +from typing import Any, Dict, Generator, List, Optional, Tuple, Union from ament_index_python.resources import get_resource from ament_index_python.resources import get_resource_types from ament_index_python.resources import get_resources -def main(argv=sys.argv[1:]): +def main(argv: List[str] = sys.argv[1:]) -> Optional[str]: parser = argparse.ArgumentParser( description='Query the ament resource index.') arg = parser.add_argument( @@ -44,13 +45,13 @@ def main(argv=sys.argv[1:]): if args.resource_type is None: for resource_type in sorted(get_resource_types()): print(resource_type) - return + return None if args.resource_name is None: resources = get_resources(args.resource_type) for resource_name in sorted(resources.keys()): - print(resource_name + '\t' + resources[resource_name]) - return + print(f'{resource_name}\t{resources[resource_name]}') + return None try: content, path = get_resource(args.resource_type, args.resource_name) @@ -62,12 +63,18 @@ def main(argv=sys.argv[1:]): print(content) print('>>>') + return None -def resource_type_completer(prefix, **kwargs): + +def resource_type_completer(prefix: Union[str, Tuple[str, ...]], + **kwarg: Dict[str, Any]) -> Generator[str, None, None]: return (t for t in get_resource_types() if t.startswith(prefix)) -def resource_name_completer(prefix, parsed_args, **kwargs): +def resource_name_completer(prefix: Union[str, Tuple[str, ...]], + parsed_args: argparse.Namespace, + **kwargs: Dict[str, Any]) -> Union[Generator[str, None, None], + List[str]]: resource_type = getattr(parsed_args, 'resource_type', None) if not resource_type: return [] diff --git a/ament_index_python/ament_index_python/packages.py b/ament_index_python/ament_index_python/packages.py index a9b2956..e669687 100644 --- a/ament_index_python/ament_index_python/packages.py +++ b/ament_index_python/ament_index_python/packages.py @@ -15,8 +15,10 @@ import os import pathlib import re +from typing import Dict import warnings + from .resources import get_resource from .resources import get_resources from .search_paths import get_search_paths @@ -26,7 +28,7 @@ class PackageNotFoundError(KeyError): pass -def get_packages_with_prefixes(): +def get_packages_with_prefixes() -> Dict[str, str]: """ Return a dict of package names to the prefixes in which they are found. @@ -36,7 +38,7 @@ def get_packages_with_prefixes(): return get_resources('packages') -def get_package_prefix(package_name): +def get_package_prefix(package_name: str) -> str: """ Return the installation prefix directory of the given package. @@ -62,7 +64,7 @@ def get_package_prefix(package_name): return package_prefix -def get_package_share_directory(package_name, print_warning=True): +def get_package_share_directory(package_name: str, print_warning: bool = True) -> str: """ Return the share directory of the given package. @@ -83,7 +85,7 @@ def get_package_share_directory(package_name, print_warning=True): return path -def get_package_share_path(package_name, print_warning=True): +def get_package_share_path(package_name: str, print_warning: bool = True) -> pathlib.Path: """ Return the share directory of the given package as a pathlib.Path. diff --git a/ament_index_python/ament_index_python/py.typed b/ament_index_python/ament_index_python/py.typed new file mode 100644 index 0000000..e69de29 diff --git a/ament_index_python/ament_index_python/resources.py b/ament_index_python/ament_index_python/resources.py index 248291e..90b1bd9 100644 --- a/ament_index_python/ament_index_python/resources.py +++ b/ament_index_python/ament_index_python/resources.py @@ -13,9 +13,15 @@ # limitations under the License. import os +import sys +from typing import Dict, Literal, Tuple, Union -from .constants import RESOURCE_INDEX_SUBFOLDER +if sys.version_info >= (3, 10): + Set = set +else: + from typing import Set +from .constants import RESOURCE_INDEX_SUBFOLDER from .search_paths import get_search_paths @@ -31,7 +37,7 @@ class InvalidResourceNameError(ValueError): pass -def _name_is_invalid(resource_name): +def _name_is_invalid(resource_name: str) -> bool: """ Get the whether a resource name or a resource type name is invalid. @@ -49,7 +55,7 @@ def _name_is_invalid(resource_name): return ('/' in resource_name) or ('\\' in resource_name) -def get_resource(resource_type, resource_name): +def get_resource(resource_type: str, resource_name: str) -> Tuple[str, str]: """ Get the content of a specific resource and its prefix path. @@ -87,7 +93,7 @@ def get_resource(resource_type, resource_name): "Could not find the resource '%s' of type '%s'" % (resource_name, resource_type)) -def get_resources(resource_type): +def get_resources(resource_type: str) -> Dict[str, str]: """ Get the resource names of all resources of the specified type. @@ -115,7 +121,7 @@ def get_resources(resource_type): return resources -def get_resource_types(): +def get_resource_types() -> Set[str]: """ Get the resource types. @@ -135,7 +141,7 @@ def get_resource_types(): return resource_types -def has_resource(resource_type, resource_name): +def has_resource(resource_type: str, resource_name: str) -> Union[str, Literal[False]]: """ Check if a specific resource exists. diff --git a/ament_index_python/ament_index_python/search_paths.py b/ament_index_python/ament_index_python/search_paths.py index 44cd1e1..80982a3 100644 --- a/ament_index_python/ament_index_python/search_paths.py +++ b/ament_index_python/ament_index_python/search_paths.py @@ -13,11 +13,12 @@ # limitations under the License. import os +from typing import List from .constants import AMENT_PREFIX_PATH_ENV_VAR -def get_search_paths(): +def get_search_paths() -> List[str]: """ Get the paths from the environment variable '{AMENT_PREFIX_PATH_ENV_VAR}'.