testlib: fix Pillow type annotations

Pillow upstream added a `py.typed` file in their last release, which is
causing us problems because:

  - we `from PIL import Image`, which kinda looks like the class `Image`
    in the `PIL` module, but is actually a submodule.  In particular,
    `PIL.Image.open()` is not a static method, but a function call, and
    it doesn't return `PIL.Image`, but rather `PIL.Image.Image`.  Fix a
    couple of annotations.

  - our hacks for setting `Image = None` if the import is missing are no
    longer working (since the import now has a better type than `Any`).
    Let's make the import unconditional.  `dnf install python3-pillow`.

  - several methods are unannotated in the upstream code, leading to
    warnings about "untyped call from typed code".  We use an #ignore
    for those; cf. https://github.com/python-pillow/Pillow/issues/8029
This commit is contained in:
Allison Karlitskaya 2024-04-30 07:28:26 +02:00
parent 930d957325
commit dcefbb2581
2 changed files with 7 additions and 11 deletions

View file

@ -25,7 +25,6 @@ module = [
"machine.*",
"task.*",
"testvm",
"PIL",
# run with bots checked out but its dependencies missing
"libvirt",

View file

@ -42,11 +42,7 @@ import cdp
from lcov import write_lcov
from lib.constants import OSTREE_IMAGES
from machine import testvm
try:
from PIL import Image
except ImportError:
Image = None
from PIL import Image
_T = TypeVar('_T')
_FT = TypeVar("_FT", bound=Callable[..., Any])
@ -1203,12 +1199,13 @@ class Browser:
def ignorable_change(a: tuple[int, int, int], b: tuple[int, int, int]) -> bool:
return abs(a[0] - b[0]) <= 2 and abs(a[1] - b[1]) <= 2 and abs(a[2] - b[2]) <= 2
def img_eq(ref: Image, now: Image, delta: Image) -> bool:
def img_eq(ref: Image.Image, now: Image.Image, delta: Image.Image) -> bool:
# This is slow but exactly what we want.
# ImageMath might be able to speed this up.
data_ref = ref.load()
data_now = now.load()
data_delta = delta.load()
# no-untyped-call: see https://github.com/python-pillow/Pillow/issues/8029
data_ref = ref.load() # type: ignore[no-untyped-call]
data_now = now.load() # type: ignore[no-untyped-call]
data_delta = delta.load() # type: ignore[no-untyped-call]
result = True
count = 0
width, height = delta.size
@ -1233,7 +1230,7 @@ class Browser:
# Preserve alpha channel so that the 'now'
# image can be used as the new reference image
# without further changes
img_now.putalpha(img_ref.getchannel("A"))
img_now.putalpha(img_ref.getchannel("A")) # type: ignore[no-untyped-call]
img_now.save(filename)
attach(filename, move=True)
ref_filename_for_attach = base + "-reference.png"