Ignore sys.stdout type

This commit is contained in:
Andrew Murray 2025-01-09 15:04:26 +11:00
parent dae31540f4
commit 5620b9e6ec
3 changed files with 19 additions and 14 deletions

View File

@ -4,7 +4,7 @@ import re
import sys import sys
import warnings import warnings
import zlib import zlib
from io import BytesIO, TextIOWrapper from io import BytesIO
from pathlib import Path from pathlib import Path
from types import ModuleType from types import ModuleType
from typing import Any, cast from typing import Any, cast
@ -811,15 +811,19 @@ class TestFilePng:
@pytest.mark.parametrize("buffer", (True, False)) @pytest.mark.parametrize("buffer", (True, False))
def test_save_stdout(self, buffer: bool, monkeypatch: pytest.MonkeyPatch) -> None: def test_save_stdout(self, buffer: bool, monkeypatch: pytest.MonkeyPatch) -> None:
b = BytesIO() class MyStdOut:
mystdout: TextIOWrapper | BytesIO = TextIOWrapper(b) if buffer else b buffer = BytesIO()
mystdout: MyStdOut | BytesIO = MyStdOut() if buffer else BytesIO()
monkeypatch.setattr(sys, "stdout", mystdout) monkeypatch.setattr(sys, "stdout", mystdout)
with Image.open(TEST_PNG_FILE) as im: with Image.open(TEST_PNG_FILE) as im:
im.save(sys.stdout, "PNG") im.save(sys.stdout, "PNG") # type: ignore[arg-type]
with Image.open(b) as reloaded: if isinstance(mystdout, MyStdOut):
mystdout = mystdout.buffer
with Image.open(mystdout) as reloaded:
assert_image_equal_tofile(reloaded, TEST_PNG_FILE) assert_image_equal_tofile(reloaded, TEST_PNG_FILE)
def test_truncated_end_chunk(self) -> None: def test_truncated_end_chunk(self) -> None:

View File

@ -1,7 +1,7 @@
from __future__ import annotations from __future__ import annotations
import sys import sys
from io import BytesIO, TextIOWrapper from io import BytesIO
from pathlib import Path from pathlib import Path
import pytest import pytest
@ -369,13 +369,17 @@ def test_mimetypes(tmp_path: Path) -> None:
@pytest.mark.parametrize("buffer", (True, False)) @pytest.mark.parametrize("buffer", (True, False))
def test_save_stdout(buffer: bool, monkeypatch: pytest.MonkeyPatch) -> None: def test_save_stdout(buffer: bool, monkeypatch: pytest.MonkeyPatch) -> None:
b = BytesIO() class MyStdOut:
mystdout: TextIOWrapper | BytesIO = TextIOWrapper(b) if buffer else b buffer = BytesIO()
mystdout: MyStdOut | BytesIO = MyStdOut() if buffer else BytesIO()
monkeypatch.setattr(sys, "stdout", mystdout) monkeypatch.setattr(sys, "stdout", mystdout)
with Image.open(TEST_FILE) as im: with Image.open(TEST_FILE) as im:
im.save(sys.stdout, "PPM") im.save(sys.stdout, "PPM") # type: ignore[arg-type]
with Image.open(b) as reloaded: if isinstance(mystdout, MyStdOut):
mystdout = mystdout.buffer
with Image.open(mystdout) as reloaded:
assert_image_equal_tofile(reloaded, TEST_FILE) assert_image_equal_tofile(reloaded, TEST_FILE)

View File

@ -2447,10 +2447,7 @@ class Image:
) )
def save( def save(
self, self, fp: StrOrBytesPath | IO[bytes], format: str | None = None, **params: Any
fp: StrOrBytesPath | IO[bytes] | io.TextIOWrapper,
format: str | None = None,
**params: Any,
) -> None: ) -> None:
""" """
Saves this image under the given filename. If no format is Saves this image under the given filename. If no format is