Skip to content

Improve type annotations #1136

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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open

Conversation

alanhdu
Copy link

@alanhdu alanhdu commented Jul 8, 2025

This improves the type annotations bundled with imageio. These are meant to fix some of the type errors I was hitting in our internal codebase after upgrading imageio and is not a systematic audit.

Each commit fixes a different issue and can be merged separately if necessary.

alanhdu added 6 commits July 8, 2025 10:43
`Sequence`s are covariant, so this allows you to pass in a
`list[np.ndarray]` without issues. From briefly looking at the
implementation, we do *not* modify the original array, so this is ok.

See https://mypy.readthedocs.io/en/stable/common_issues.html#invariance-vs-covariance
for more information.
These functions take `str`s, *not* `Format` objects. I'm not
sure what the intention of the `Format` objects were in the first place.
Without this, if you do `with imageio.get_writer(...) as writer`,
`writer` will have the type `Format._BaseReaderWriter` since
`__enter__` does not preserve *which* subclass we are.
typing.IO[bytes] is more general (for instance, `io.BytesIO` is an
`IO[bytes]` but not a `BinaryIO`), and we don't need any of the
functionality in `BinaryIO`.

Ideally, we would actually define a custom protocol here (since we
probably don't need the entire file-like API), but that would be a
lot of work.
The `@overload` still needs a *final* declaration -- otherwise,
some type-checkers will silently ignore the last overload.
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 this pull request may close these issues.

1 participant