argos-translate icon indicating copy to clipboard operation
argos-translate copied to clipboard

Use type hints instead of type in annotations

Open dingedi opened this issue 2 years ago • 2 comments

maybe we could use the python type hints for typing rather than in docstrings? for argostranslate v2 maybe?

Exemple:

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)

dingedi avatar Sep 17 '22 07:09 dingedi

Yes 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.

PJ-Finlay avatar Sep 17 '22 20:09 PJ-Finlay

This can go in the master branch, it doesn't need to be in v2.

PJ-Finlay avatar Sep 17 '22 20:09 PJ-Finlay

I'm planning to work on this soon (but can still accept pull requests). I'm going to put it in the "v2" branch after all to avoid merge conflicts.

PJ-Finlay avatar Oct 06 '22 12:10 PJ-Finlay