Skip to content

Improve type hints #8883

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 5 commits into
base: main
Choose a base branch
from
Open

Conversation

radarhere
Copy link
Member

This is part of #8362 - I'm hoping to break down that PR into easier-to-review chunks.

  1. A tile's args might be a tuple, string or None.

    Pillow/src/PIL/ImageFile.py

    Lines 101 to 105 in c8d98d5

    class _Tile(NamedTuple):
    codec_name: str
    extents: tuple[int, int, int, int] | None
    offset: int = 0
    args: tuple[Any, ...] | str | None = None

    Assert that args is a tuple before accessing a value by index.

  2. _getmp may return a dict or None.

    def _getmp(self) -> dict[int, Any] | None:

    Assert that it is not None before using a key.

  3. Pixel access might return a tuple or a float.

    class PixelAccess:
    def __getitem__(self, xy: tuple[int, int]) -> float | tuple[int, ...]: ...

    Assert that it is a tuple before checking one of the channel values.

  4. load() might return None.

    def load(self) -> core.PixelAccess | None:

    Assert that it isn't None before getting pixel data for a particular co-ordinate.

  5. getcolors() might return None.

    Pillow/src/PIL/Image.py

    Lines 1411 to 1413 in c8d98d5

    def getcolors(
    self, maxcolors: int = 256
    ) -> list[tuple[int, tuple[int, ...]]] | list[tuple[int, float]] | None:

    Assert that it isn't None before checking how much a colour is used.

@radarhere radarhere changed the title Improved type hints Improve type hints Apr 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant