Improve type hints by radarhere · Pull Request #8883 · python-pillow/Pillow

Skip to content

Navigation Menu

Sign in

Appearance settings

Conversation

@radarhere

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.

    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.

    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

Labels

2 participants

@radarhere @hugovk