Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

translated_text should use method overload to have better typing #84

Open
DanieleIsoni opened this issue Nov 20, 2023 · 1 comment
Open

Comments

@DanieleIsoni
Copy link

DanieleIsoni commented Nov 20, 2023

In the typing of the translate_text it would be better to use method overloading, so that the return type is not ambiguous when passing a text variable that is either str or Iterable[str]

So I suggest that here

def translate_text(
self,
text: Union[str, Iterable[str]],
*,
source_lang: Union[str, Language, None] = None,
target_lang: Union[str, Language],
context: Optional[str] = None,
split_sentences: Union[str, SplitSentences, None] = None,
preserve_formatting: Optional[bool] = None,
formality: Union[str, Formality, None] = None,
glossary: Union[str, GlossaryInfo, None] = None,
tag_handling: Optional[str] = None,
outline_detection: Optional[bool] = None,
non_splitting_tags: Union[str, List[str], None] = None,
splitting_tags: Union[str, List[str], None] = None,
ignore_tags: Union[str, List[str], None] = None,
) -> Union[TextResult, List[TextResult]]:
the typing should be something like:

@overload
def translate_text(
        self,
        text: str,
        *,
        source_lang: Union[str, Language, None] = None,
        target_lang: Union[str, Language],
        context: Optional[str] = None,
        split_sentences: Union[str, SplitSentences, None] = None,
        preserve_formatting: Optional[bool] = None,
        formality: Union[str, Formality, None] = None,
        glossary: Union[str, GlossaryInfo, None] = None,
        tag_handling: Optional[str] = None,
        outline_detection: Optional[bool] = None,
        non_splitting_tags: Union[str, List[str], None] = None,
        splitting_tags: Union[str, List[str], None] = None,
        ignore_tags: Union[str, List[str], None] = None,
    ) -> TextResult:
    ...

@overload
def translate_text(
        self,
        text: Iterable[str],
        *,
        source_lang: Union[str, Language, None] = None,
        target_lang: Union[str, Language],
        context: Optional[str] = None,
        split_sentences: Union[str, SplitSentences, None] = None,
        preserve_formatting: Optional[bool] = None,
        formality: Union[str, Formality, None] = None,
        glossary: Union[str, GlossaryInfo, None] = None,
        tag_handling: Optional[str] = None,
        outline_detection: Optional[bool] = None,
        non_splitting_tags: Union[str, List[str], None] = None,
        splitting_tags: Union[str, List[str], None] = None,
        ignore_tags: Union[str, List[str], None] = None,
    ) -> List[TextResult]:
    ...

def translate_text(
        self,
        text: Union[str, Iterable[str]],
        *,
        source_lang: Union[str, Language, None] = None,
        target_lang: Union[str, Language],
        context: Optional[str] = None,
        split_sentences: Union[str, SplitSentences, None] = None,
        preserve_formatting: Optional[bool] = None,
        formality: Union[str, Formality, None] = None,
        glossary: Union[str, GlossaryInfo, None] = None,
        tag_handling: Optional[str] = None,
        outline_detection: Optional[bool] = None,
        non_splitting_tags: Union[str, List[str], None] = None,
        splitting_tags: Union[str, List[str], None] = None,
        ignore_tags: Union[str, List[str], None] = None,
    ) -> Union[TextResult, List[TextResult]]:

If typed like this mypy understands that when text is a str the return type is TextResult and when it is an Iterable[str] the return type is List[TextResult], avoiding error messages like when passing an Iterable[str] as text:

<file>:102: error: Item "TextResult" of "TextResult | list[TextResult]" has no attribute "__iter__" (not iterable)  [union-attr]

If you prefer, I'd be glad submit a pull request

@JanEbbing
Copy link
Member

This is a great suggestion, if you could up a PR I'll merge it, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants