Skip to content

Commit

Permalink
Add type annotations to python files. (#93)
Browse files Browse the repository at this point in the history
* Add type annotations

Signed-off-by: Michael Carlstrom <[email protected]>
  • Loading branch information
InvincibleRMC authored Mar 21, 2024
1 parent 076cef0 commit fbccc98
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 17 deletions.
19 changes: 13 additions & 6 deletions ament_index_python/ament_index_python/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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)
Expand All @@ -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 []
Expand Down
10 changes: 6 additions & 4 deletions ament_index_python/ament_index_python/packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand Down
Empty file.
18 changes: 12 additions & 6 deletions ament_index_python/ament_index_python/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion ament_index_python/ament_index_python/search_paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}'.
Expand Down

0 comments on commit fbccc98

Please sign in to comment.