Use type hints instead of type annotations for Argos Translate

Example:

old:

def get_translation_from_codes(from_code, to_code):
    """Gets a translation object from codes for from and to languages

    An exception will be thrown if an installed translation between the from lang
    and to lang can not be found.

    Args:
        from_code (str): The ISO 639 code of the source language
        to_code (str): The ISO 639 code of the target language

    Returns:
        translate.ITranslation: The translation object
    """
    from_lang = get_language_from_code(from_code)
    to_lang = get_language_from_code(to_code)
    return from_lang.get_translation(to_lang)

new:

def get_translation_from_codes(from_code: str, to_code: str) -> ITranslation:
    """Gets a translation object from codes for from and to languages

    An exception will be thrown if an installed translation between the from lang
    and to lang can not be found.

    Args:
        from_code: The ISO 639 code of the source language
        to_code: The ISO 639 code of the target language

    Returns:
        The translation object
    """
    from_lang = get_language_from_code(from_code)
    to_lang = get_language_from_code(to_code)
    return from_lang.get_translation(to_lang)

Suggested by @dingedi

I think moving to type hints is a good idea. I think it will break Python versions earlier than 3.5 but that’s fine. LibreTranslate already only supports >3.7.

There’s some value in adding type annotations, especially if one uses tools/IDEs that support reading them. It certainly adds robustness, but can also require a lot of work to add them if they haven’t been used from day one.

1 Like

I agree, I also think the process of adding them would help find bugs.

Thanks @dingedi :rocket:

1 Like