Skip to content

Commit be347f1

Browse files
Use Absolute names for type hints (#258)
Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>
1 parent 828b323 commit be347f1

1 file changed

Lines changed: 28 additions & 18 deletions

File tree

rosidl_generator_py/rosidl_generator_py/generate_py_impl.py

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,23 @@ def get_type_annotation_constant_default(constant: Union[Constant, Member],
387387
assert False, f"unknown type '{type_}'"
388388

389389

390+
def get_absolute_namespaced_typehint(type_: NamespacedType, type_imports: set[str]) -> str:
391+
joined_type_namespaces = '.'.join(type_.namespaces)
392+
if (
393+
type_.name.endswith(ACTION_GOAL_SUFFIX)
394+
or type_.name.endswith(ACTION_RESULT_SUFFIX)
395+
or type_.name.endswith(ACTION_FEEDBACK_SUFFIX)
396+
):
397+
398+
type_name_rsplit = type_.name.rsplit('_', 1)
399+
lower_case_name = convert_camel_case_to_lower_case_underscore(type_name_rsplit[0])
400+
type_imports.add(f'import {joined_type_namespaces}._{lower_case_name}')
401+
return f'{joined_type_namespaces}._{lower_case_name}.{type_.name}'
402+
else:
403+
type_imports.add(f'import {joined_type_namespaces}')
404+
return f'{joined_type_namespaces}.{type_.name}'
405+
406+
390407
def get_setter_and_getter_type(member: Member, type_imports: set[str]) -> tuple[str, str]:
391408
"""From a member return the setter and getter type annotations. Add needed type_imports."""
392409
type_ = member.type
@@ -402,14 +419,17 @@ def get_setter_and_getter_type(member: Member, type_imports: set[str]) -> tuple[
402419
if (
403420
isinstance(member.type, AbstractNestedType)
404421
):
422+
if isinstance(type_, NamespacedType):
423+
python_type = get_absolute_namespaced_typehint(type_, type_imports)
424+
405425
if (
406426
isinstance(type_, BasicType) and
407427
type_.typename in SPECIAL_NESTED_BASIC_TYPES
408428
):
409429
if isinstance(member.type, Array):
410-
type_imports.add('from numpy.typing import NDArray')
430+
type_imports.add('import numpy.typing')
411431
dtype = SPECIAL_NESTED_BASIC_TYPES[type_.typename]['dtype']
412-
type_annotation = f'NDArray[{dtype}]'
432+
type_annotation = f'numpy.typing.NDArray[{dtype}]'
413433
elif isinstance(member.type, AbstractSequence):
414434
type_annotation = f'array.array[{python_type}]'
415435
else:
@@ -431,28 +451,18 @@ def get_setter_and_getter_type(member: Member, type_imports: set[str]) -> tuple[
431451
type_annotation = sequence_type
432452

433453
elif isinstance(member.type, AbstractGenericString) and member.type.has_maximum_size():
434-
type_imports.add('from collections import UserString')
435-
type_annotation = 'typing.Union[str, UserString]'
454+
type_imports.add('import collections')
455+
type_annotation = 'typing.Union[str, collections.UserString]'
436456
elif isinstance(type_, BasicType) and type_.typename == 'char':
437-
type_imports.add('from collections import UserString')
438-
type_annotation = 'typing.Union[str, UserString]'
457+
type_imports.add('import collections')
458+
type_annotation = 'typing.Union[str, collections.UserString]'
439459
elif isinstance(type_, BasicType) and type_.typename == 'octet':
440460
type_annotation = 'typing.Union[bytes, collections.abc.ByteString]'
461+
elif isinstance(type_, NamespacedType):
462+
type_annotation = get_absolute_namespaced_typehint(type_, type_imports)
441463
else:
442464
type_annotation = python_type
443465

444-
if isinstance(type_, NamespacedType):
445-
joined_type_namespaces = '.'.join(type_.namespaces)
446-
if type_.name.endswith(ACTION_GOAL_SUFFIX) or type_.name.endswith(ACTION_RESULT_SUFFIX) \
447-
or type_.name.endswith(ACTION_FEEDBACK_SUFFIX):
448-
449-
type_name_rsplit = type_.name.rsplit('_', 1)
450-
lower_case_name = convert_camel_case_to_lower_case_underscore(type_name_rsplit[0])
451-
type_imports.add(f'from {joined_type_namespaces}._{lower_case_name} '
452-
f'import {type_.name}')
453-
else:
454-
type_imports.add(f'from {joined_type_namespaces} import {type_.name}')
455-
456466
type_annotations_setter = type_annotation
457467

458468
if type_annotations_getter == '':

0 commit comments

Comments
 (0)