Skip to content

gettext.GNUTranslations.__init__’s argument has an incorrect type #7628

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

Closed
Jayman2000 opened this issue Apr 15, 2022 · 2 comments · Fixed by #7636
Closed

gettext.GNUTranslations.__init__’s argument has an incorrect type #7628

Jayman2000 opened this issue Apr 15, 2022 · 2 comments · Fixed by #7636

Comments

@Jayman2000
Copy link

Jayman2000 commented Apr 15, 2022

mypy says that this program is wrong:

from gettext import GNUTranslations

with open("foo.mo", mode='rb') as mo_file:
    GNUTranslations(mo_file)

mypy’s output:

main.py:4: error: Argument 1 to "GNUTranslations" has incompatible type "BufferedReader"; expected "Optional[IO[str]]"
Found 1 error in 1 file (checked 1 source file)

I believe that fp (gettext.GNUTranslations.__init__’s argument) should have the type Optional[IO[bytes]], not Optional[IO[str]].

@Jayman2000 Jayman2000 changed the title `gettext. gettext.GNUTranslations.__init__’s argument has an incorrect type Apr 15, 2022
@TeamSpen210
Copy link
Contributor

The problem is that NullTranslations (its base class) is hinted as taking an IO[str] | None, but GNUTranslations needs a binary file. NullTranslations doesn't actually do anything with the file, just store it for subclasses to use so the best solution would probably be to make NullTranslations take IO | None and then constrain in GNUTranslations.

@srittau
Copy link
Collaborator

srittau commented Apr 15, 2022

I'd say we just type NullTranslations according to how we type GNUTranslations (as long as it doesn't contradict the actual implementation in the former) as the latter is the only "real" translations class we have. We also should replace IO with a tight protocol.

srittau added a commit to srittau/typeshed that referenced this issue Apr 16, 2022
* Use a protocol instead of `IO` for `fp` argument.
* Annotate a few missing return types.
* Mark some class constants as final.

Fixes: python#7628
JelleZijlstra pushed a commit that referenced this issue Apr 16, 2022
* Use a protocol instead of `IO` for `fp` argument.
* Annotate a few missing return types.
* Mark some class constants as final.

Fixes #7628
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

Successfully merging a pull request may close this issue.

3 participants