Skip to content
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

Check test type hints #8208

Merged
merged 1 commit into from
Jul 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Tests/test_file_gif.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ def test_palette_434(tmp_path: Path) -> None:

def roundtrip(im: Image.Image, **kwargs: bool) -> Image.Image:
out = str(tmp_path / "temp.gif")
im.copy().save(out, **kwargs)
im.copy().save(out, "GIF", **kwargs)
reloaded = Image.open(out)

return reloaded
Expand Down
21 changes: 16 additions & 5 deletions Tests/test_file_libtiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,22 @@ def test_g4_tiff_bytesio(self, tmp_path: Path) -> None:
def test_g4_non_disk_file_object(self, tmp_path: Path) -> None:
"""Testing loading from non-disk non-BytesIO file object"""
test_file = "Tests/images/hopper_g4_500.tif"
s = io.BytesIO()
with open(test_file, "rb") as f:
s.write(f.read())
s.seek(0)
r = io.BufferedReader(s)
data = f.read()

class NonBytesIO(io.RawIOBase):
def read(self, size: int = -1) -> bytes:
nonlocal data
if size == -1:
size = len(data)
result = data[:size]
data = data[size:]
return result

def readable(self) -> bool:
return True

r = io.BufferedReader(NonBytesIO())
with Image.open(r) as im:
assert im.size == (500, 500)
self._assert_noerr(tmp_path, im)
Expand Down Expand Up @@ -1139,7 +1150,7 @@ def test_save_single_strip(self, argument: bool, tmp_path: Path) -> None:
arguments: dict[str, str | int] = {"compression": "tiff_adobe_deflate"}
if argument:
arguments["strip_size"] = 2**18
im.save(out, **arguments)
im.save(out, "TIFF", **arguments)

with Image.open(out) as im:
assert isinstance(im, TiffImagePlugin.TiffImageFile)
Expand Down
2 changes: 1 addition & 1 deletion Tests/test_file_pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def test_dpi(params: dict[str, int | tuple[int, int]], tmp_path: Path) -> None:
im = hopper()

outfile = str(tmp_path / "temp.pdf")
im.save(outfile, **params)
im.save(outfile, "PDF", **params)

with open(outfile, "rb") as fp:
contents = fp.read()
Expand Down
22 changes: 13 additions & 9 deletions Tests/test_file_tiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ def test_closed_file(self) -> None:

def test_seek_after_close(self) -> None:
im = Image.open("Tests/images/multipage.tiff")
assert isinstance(im, TiffImagePlugin.TiffImageFile)
im.close()

with pytest.raises(ValueError):
Expand Down Expand Up @@ -424,13 +425,13 @@ def test_load_string(self) -> None:
def test_load_float(self) -> None:
ifd = TiffImagePlugin.ImageFileDirectory_v2()
data = b"abcdabcd"
ret = ifd.load_float(data, False)
ret = getattr(ifd, "load_float")(data, False)
assert ret == (1.6777999408082104e22, 1.6777999408082104e22)

def test_load_double(self) -> None:
ifd = TiffImagePlugin.ImageFileDirectory_v2()
data = b"abcdefghabcdefgh"
ret = ifd.load_double(data, False)
ret = getattr(ifd, "load_double")(data, False)
assert ret == (8.540883223036124e194, 8.540883223036124e194)

def test_ifd_tag_type(self) -> None:
Expand Down Expand Up @@ -599,7 +600,7 @@ def test_gray_semibyte_per_pixel(self) -> None:
def test_with_underscores(self, tmp_path: Path) -> None:
kwargs = {"resolution_unit": "inch", "x_resolution": 72, "y_resolution": 36}
filename = str(tmp_path / "temp.tif")
hopper("RGB").save(filename, **kwargs)
hopper("RGB").save(filename, "TIFF", **kwargs)
with Image.open(filename) as im:
# legacy interface
assert im.tag[X_RESOLUTION][0][0] == 72
Expand All @@ -624,14 +625,17 @@ def test_roundtrip_tiff_uint16(self, tmp_path: Path) -> None:
def test_iptc(self, tmp_path: Path) -> None:
# Do not preserve IPTC_NAA_CHUNK by default if type is LONG
outfile = str(tmp_path / "temp.tif")
im = hopper()
ifd = TiffImagePlugin.ImageFileDirectory_v2()
ifd[33723] = 1
ifd.tagtype[33723] = 4
im.tag_v2 = ifd
im.save(outfile)
with Image.open("Tests/images/hopper.tif") as im:
im.load()
assert isinstance(im, TiffImagePlugin.TiffImageFile)
ifd = TiffImagePlugin.ImageFileDirectory_v2()
ifd[33723] = 1
ifd.tagtype[33723] = 4
im.tag_v2 = ifd
im.save(outfile)

with Image.open(outfile) as im:
assert isinstance(im, TiffImagePlugin.TiffImageFile)
assert 33723 not in im.tag_v2

def test_rowsperstrip(self, tmp_path: Path) -> None:
Expand Down
2 changes: 1 addition & 1 deletion Tests/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,7 @@ def _make_new(
else:
assert new_image.palette is None

_make_new(im, im_p, ImagePalette.ImagePalette(list(range(256)) * 3))
_make_new(im, im_p, ImagePalette.ImagePalette("RGB"))
_make_new(im_p, im, None)
_make_new(im, blank_p, ImagePalette.ImagePalette())
_make_new(im, blank_pa, ImagePalette.ImagePalette())
Expand Down
7 changes: 4 additions & 3 deletions Tests/test_imagegrab.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ def test_grab_invalid_xdisplay(self) -> None:
def test_grabclipboard(self) -> None:
if sys.platform == "darwin":
subprocess.call(["screencapture", "-cx"])

ImageGrab.grabclipboard()
elif sys.platform == "win32":
p = subprocess.Popen(["powershell", "-command", "-"], stdin=subprocess.PIPE)
p.stdin.write(
Expand All @@ -69,6 +71,8 @@ def test_grabclipboard(self) -> None:
[Windows.Forms.Clipboard]::SetImage($bmp)"""
)
p.communicate()

ImageGrab.grabclipboard()
else:
if not shutil.which("wl-paste") and not shutil.which("xclip"):
with pytest.raises(
Expand All @@ -77,9 +81,6 @@ def test_grabclipboard(self) -> None:
r" ImageGrab.grabclipboard\(\) on Linux",
):
ImageGrab.grabclipboard()
return

ImageGrab.grabclipboard()

@pytest.mark.skipif(sys.platform != "win32", reason="Windows only")
def test_grabclipboard_file(self) -> None:
Expand Down
8 changes: 8 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,11 @@ follow_imports = "silent"
warn_redundant_casts = true
warn_unreachable = true
warn_unused_ignores = true
exclude = [
'^Tests/oss-fuzz/fuzz_font.py$',
'^Tests/oss-fuzz/fuzz_pillow.py$',
'^Tests/test_qt_image_qapplication.py$',
'^Tests/test_font_pcf_charsets.py$',
'^Tests/test_font_pcf.py$',
'^Tests/test_file_tar.py$',
]
4 changes: 2 additions & 2 deletions src/PIL/Image.py
Original file line number Diff line number Diff line change
Expand Up @@ -1886,7 +1886,7 @@ def alpha_composite(

def point(
self,
lut: Sequence[float] | Callable[[int], float] | ImagePointHandler,
lut: Sequence[float] | NumpyArray | Callable[[int], float] | ImagePointHandler,
mode: str | None = None,
) -> Image:
"""
Expand Down Expand Up @@ -1996,7 +1996,7 @@ def putalpha(self, alpha: Image | int) -> None:

def putdata(
self,
data: Sequence[float] | Sequence[Sequence[int]],
data: Sequence[float] | Sequence[Sequence[int]] | NumpyArray,
scale: float = 1.0,
offset: float = 0.0,
) -> None:
Expand Down
4 changes: 3 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,11 @@ deps =
ipython
numpy
packaging
pytest
types-defusedxml
types-olefile
types-setuptools
extras =
typing
commands =
mypy src {posargs}
mypy src Tests {posargs}
Loading