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 warnings
import zlib
from io import BytesIO, TextIOWrapper
from io import BytesIO
from pathlib import Path
from types import ModuleType
from typing import Any, cast
@ -811,15 +811,19 @@ class TestFilePng:
@pytest.mark.parametrize("buffer", (True, False))
def test_save_stdout(self, buffer: bool, monkeypatch: pytest.MonkeyPatch) -> None:
b = BytesIO()
mystdout: TextIOWrapper | BytesIO = TextIOWrapper(b) if buffer else b
class MyStdOut:
buffer = BytesIO()
mystdout: MyStdOut | BytesIO = MyStdOut() if buffer else BytesIO()
monkeypatch.setattr(sys, "stdout", mystdout)
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)
def test_truncated_end_chunk(self) -> None:

View File

@ -1,7 +1,7 @@
from __future__ import annotations
import sys
from io import BytesIO, TextIOWrapper
from io import BytesIO
from pathlib import Path
import pytest
@ -369,13 +369,17 @@ def test_mimetypes(tmp_path: Path) -> None:
@pytest.mark.parametrize("buffer", (True, False))
def test_save_stdout(buffer: bool, monkeypatch: pytest.MonkeyPatch) -> None:
b = BytesIO()
mystdout: TextIOWrapper | BytesIO = TextIOWrapper(b) if buffer else b
class MyStdOut:
buffer = BytesIO()
mystdout: MyStdOut | BytesIO = MyStdOut() if buffer else BytesIO()
monkeypatch.setattr(sys, "stdout", mystdout)
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)

View File

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