From 255bfe1726ce84bc8bf792b7b1f065574891819b Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Fri, 22 Apr 2022 15:24:12 -0400 Subject: [PATCH 1/2] Add PIL.Image protocol --- circuitpython_typing/pil.py | 42 +++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 circuitpython_typing/pil.py diff --git a/circuitpython_typing/pil.py b/circuitpython_typing/pil.py new file mode 100644 index 0000000..c6e5885 --- /dev/null +++ b/circuitpython_typing/pil.py @@ -0,0 +1,42 @@ +# SPDX-FileCopyrightText: Copyright (c) 2022 Alec Delaney +# +# SPDX-License-Identifier: MIT + +""" +`circuitpython_typing.pil` +================================================================================ + +Type annotation definitions for PIL Images. + +* Author(s): Alec Delaney +""" + +from typing import Tuple + +# Protocol was introduced in Python 3.8. +try: + from typing import Protocol +except ImportError: + from typing_extensions import Protocol + +class PixelAccess(Protocol): + + def __getitem__(self, xy: Tuple[int, int]) -> int: + """Get pixels by x, y coordinate""" + ... + +class Image(Protocol): + + @property + def mode(self) -> str: + """The mode of the image""" + ... + + @property + def size(self) -> Tuple[int, int]: + """The size of the image""" + ... + + def load(self) -> PixelAccess: + """Load the image for quick pixel access""" + ... From 706caded55fe700c3820e0bcba89e733d870a56b Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Fri, 22 Apr 2022 15:41:15 -0400 Subject: [PATCH 2/2] Linted and reformatted --- circuitpython_typing/pil.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/circuitpython_typing/pil.py b/circuitpython_typing/pil.py index c6e5885..4e541d3 100644 --- a/circuitpython_typing/pil.py +++ b/circuitpython_typing/pil.py @@ -19,13 +19,18 @@ except ImportError: from typing_extensions import Protocol + class PixelAccess(Protocol): + """Type annotation for PIL's PixelAccess class""" + # pylint: disable=invalid-name def __getitem__(self, xy: Tuple[int, int]) -> int: """Get pixels by x, y coordinate""" ... + class Image(Protocol): + """Type annotation for PIL's Image class""" @property def mode(self) -> str: