Skip to content

Commit

Permalink
Added type hints
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed Dec 27, 2024
1 parent 1187777 commit 741da90
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 21 deletions.
40 changes: 25 additions & 15 deletions Tests/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -514,21 +514,31 @@ def test_no_supported_modes_method(self, tmp_path: Path) -> None:
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

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", ["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

@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
Expand Down
2 changes: 1 addition & 1 deletion src/PIL/GifImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -1181,7 +1181,7 @@ def write(self, data: Buffer) -> int:
return fp.data


def _supported_modes():
def _supported_modes() -> list[str]:
return ["RGB", "RGBA", "P", "I", "F", "LA", "L", "1"]


Expand Down
8 changes: 6 additions & 2 deletions src/PIL/Image.py
Original file line number Diff line number Diff line change
Expand Up @@ -2618,9 +2618,11 @@ def save(
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():
Expand Down Expand Up @@ -2674,6 +2676,8 @@ def _convert_mode(self, modes, params={}):
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
Expand Down
2 changes: 1 addition & 1 deletion src/PIL/JpegImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -891,7 +891,7 @@ def jpeg_factory(
return im


def _supported_modes():
def _supported_modes() -> list[str]:
return ["RGB", "CMYK", "YCbCr", "RGBX", "L", "1"]


Expand Down
2 changes: 1 addition & 1 deletion src/PIL/PngImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -1532,7 +1532,7 @@ def append(fp: IO[bytes], cid: bytes, *data: bytes) -> None:
return chunks


def _supported_modes():
def _supported_modes() -> list[str]:
return ["RGB", "RGBA", "P", "I", "LA", "L", "1"]


Expand Down
2 changes: 1 addition & 1 deletion src/PIL/WebPImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 741da90

Please sign in to comment.