Added type hints

This commit is contained in:
Andrew Murray 2024-12-28 07:37:57 +11:00
parent 1187777c5b
commit 741da90906
6 changed files with 34 additions and 20 deletions

View File

@ -514,21 +514,31 @@ class TestImage:
im = hopper()
im.save(temp_file, convert_mode=True)
def test_convert_mode(self) -> None:
for mode, modes in [["P", []], ["P", ["P"]]]: # no modes, same mode
im = Image.new(mode, (100, 100))
assert im._convert_mode(modes) is None
@pytest.mark.parametrize(
"mode, modes",
(
("P", ["RGB"]),
("P", ["L"]), # converting to a non-preferred mode
("LA", ["P"]),
("I", ["L"]),
("RGB", ["L"]),
("RGB", ["CMYK"]),
),
)
def test_convert_mode(self, mode: str, modes: list[str]) -> None:
im = Image.new(mode, (100, 100))
assert im._convert_mode(modes) is not None
for mode, modes in [
["P", ["RGB"]],
["P", ["L"]], # converting to a non-preferred mode
["LA", ["P"]],
["I", ["L"]],
["RGB", ["L"]],
["RGB", ["CMYK"]],
]:
im = Image.new(mode, (100, 100))
assert im._convert_mode(modes) is not None
@pytest.mark.parametrize(
"mode, modes",
(
("P", []), # no mode
("P", ["P"]), # same mode
),
)
def test_convert_mode_noop(self, mode: str, modes: list[str]) -> None:
im = Image.new(mode, (100, 100))
assert im._convert_mode(modes) is None
def test_effect_mandelbrot(self) -> None:
# Arrange

View File

@ -1181,7 +1181,7 @@ def getdata(
return fp.data
def _supported_modes():
def _supported_modes() -> list[str]:
return ["RGB", "RGBA", "P", "I", "F", "LA", "L", "1"]

View File

@ -2618,9 +2618,11 @@ class Image:
if open_fp:
fp.close()
def _convert_mode(self, modes, params={}):
def _convert_mode(
self, modes: list[str], params: dict[str, Any] = {}
) -> Image | None:
if not modes or self.mode in modes:
return
return None
if self.mode == "P":
preferred_modes = []
if "A" in self.im.getpalettemode():
@ -2674,6 +2676,8 @@ class Image:
elif new_mode:
return self.convert(new_mode)
return None
def seek(self, frame: int) -> None:
"""
Seeks to the given frame in this sequence file. If you seek

View File

@ -891,7 +891,7 @@ def jpeg_factory(
return im
def _supported_modes():
def _supported_modes() -> list[str]:
return ["RGB", "CMYK", "YCbCr", "RGBX", "L", "1"]

View File

@ -1532,7 +1532,7 @@ def getchunks(im: Image.Image, **params: Any) -> list[tuple[bytes, bytes, bytes]
return chunks
def _supported_modes():
def _supported_modes() -> list[str]:
return ["RGB", "RGBA", "P", "I", "LA", "L", "1"]

View File

@ -314,7 +314,7 @@ def _save(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None:
fp.write(data)
def _supported_modes():
def _supported_modes() -> list[str]:
return [
"RGB",
"RGBA",