From 49b218bbb1d7b13959465b82c6305b963c407ce8 Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> Date: Sat, 29 Jun 2024 23:05:25 +0300 Subject: [PATCH] Upgrade Python syntax with pyupgrade --- Tests/helper.py | 3 ++- Tests/test_file_eps.py | 7 ++++--- Tests/test_file_gif.py | 2 +- Tests/test_file_pdf.py | 3 ++- Tests/test_file_tiff.py | 2 +- Tests/test_image_resample.py | 2 +- Tests/test_image_resize.py | 2 +- Tests/test_imagedraw.py | 2 +- Tests/test_imageops_usm.py | 2 +- Tests/test_imagepath.py | 2 +- src/PIL/GifImagePlugin.py | 4 ++-- src/PIL/Image.py | 8 +++----- src/PIL/ImageDraw.py | 9 +++++---- src/PIL/ImageFilter.py | 3 ++- src/PIL/ImageOps.py | 3 ++- src/PIL/ImagePalette.py | 3 ++- src/PIL/ImageTransform.py | 3 ++- src/PIL/IptcImagePlugin.py | 2 +- src/PIL/Jpeg2KImagePlugin.py | 4 ++-- src/PIL/PdfParser.py | 4 ++-- src/PIL/SpiderImagePlugin.py | 4 ++-- src/PIL/_typing.py | 3 ++- 22 files changed, 42 insertions(+), 35 deletions(-) diff --git a/Tests/helper.py b/Tests/helper.py index 605a214d400..7abc495b6ea 100644 --- a/Tests/helper.py +++ b/Tests/helper.py @@ -11,9 +11,10 @@ import sys import sysconfig import tempfile +from collections.abc import Sequence from functools import lru_cache from io import BytesIO -from typing import Any, Callable, Sequence +from typing import Any, Callable import pytest from packaging.version import parse as parse_version diff --git a/Tests/test_file_eps.py b/Tests/test_file_eps.py index 3181fc20444..b54238132c2 100644 --- a/Tests/test_file_eps.py +++ b/Tests/test_file_eps.py @@ -385,9 +385,10 @@ def test_timeout(test_file: str) -> None: def test_bounding_box_in_trailer() -> None: # Check bounding boxes are parsed in the same way # when specified in the header and the trailer - with Image.open("Tests/images/zero_bb_trailer.eps") as trailer_image, Image.open( - FILE1 - ) as header_image: + with ( + Image.open("Tests/images/zero_bb_trailer.eps") as trailer_image, + Image.open(FILE1) as header_image, + ): assert trailer_image.size == header_image.size diff --git a/Tests/test_file_gif.py b/Tests/test_file_gif.py index e19c88a47c4..79a2ec0ab37 100644 --- a/Tests/test_file_gif.py +++ b/Tests/test_file_gif.py @@ -1,9 +1,9 @@ from __future__ import annotations import warnings +from collections.abc import Generator from io import BytesIO from pathlib import Path -from typing import Generator import pytest diff --git a/Tests/test_file_pdf.py b/Tests/test_file_pdf.py index d39a86565ce..02f07a565d7 100644 --- a/Tests/test_file_pdf.py +++ b/Tests/test_file_pdf.py @@ -5,8 +5,9 @@ import os.path import tempfile import time +from collections.abc import Generator from pathlib import Path -from typing import Any, Generator +from typing import Any import pytest diff --git a/Tests/test_file_tiff.py b/Tests/test_file_tiff.py index c06d110a75e..93a9f9e6a61 100644 --- a/Tests/test_file_tiff.py +++ b/Tests/test_file_tiff.py @@ -2,10 +2,10 @@ import os import warnings +from collections.abc import Generator from io import BytesIO from pathlib import Path from types import ModuleType -from typing import Generator import pytest diff --git a/Tests/test_image_resample.py b/Tests/test_image_resample.py index d6055b577e3..3abe09d4973 100644 --- a/Tests/test_image_resample.py +++ b/Tests/test_image_resample.py @@ -1,7 +1,7 @@ from __future__ import annotations +from collections.abc import Generator from contextlib import contextmanager -from typing import Generator import pytest diff --git a/Tests/test_image_resize.py b/Tests/test_image_resize.py index 64098f80fb1..3ef05a25f67 100644 --- a/Tests/test_image_resize.py +++ b/Tests/test_image_resize.py @@ -4,9 +4,9 @@ from __future__ import annotations +from collections.abc import Generator from itertools import permutations from pathlib import Path -from typing import Generator import pytest diff --git a/Tests/test_imagedraw.py b/Tests/test_imagedraw.py index 1466ab1bac8..1ff3fea2a64 100644 --- a/Tests/test_imagedraw.py +++ b/Tests/test_imagedraw.py @@ -2,7 +2,7 @@ import contextlib import os.path -from typing import Sequence +from collections.abc import Sequence import pytest diff --git a/Tests/test_imageops_usm.py b/Tests/test_imageops_usm.py index c8e2c0467de..920012d8639 100644 --- a/Tests/test_imageops_usm.py +++ b/Tests/test_imageops_usm.py @@ -1,6 +1,6 @@ from __future__ import annotations -from typing import Generator +from collections.abc import Generator import pytest diff --git a/Tests/test_imagepath.py b/Tests/test_imagepath.py index 9487560af88..cda2584e77b 100644 --- a/Tests/test_imagepath.py +++ b/Tests/test_imagepath.py @@ -3,7 +3,7 @@ import array import math import struct -from typing import Sequence +from collections.abc import Sequence import pytest diff --git a/src/PIL/GifImagePlugin.py b/src/PIL/GifImagePlugin.py index 8018e25dcee..bf74f9356fa 100644 --- a/src/PIL/GifImagePlugin.py +++ b/src/PIL/GifImagePlugin.py @@ -32,7 +32,7 @@ import sys from enum import IntEnum from functools import cached_property -from typing import IO, TYPE_CHECKING, Any, List, Literal, NamedTuple, Union +from typing import IO, TYPE_CHECKING, Any, Literal, NamedTuple, Union from . import ( Image, @@ -504,7 +504,7 @@ def _normalize_mode(im: Image.Image) -> Image.Image: return im.convert("L") -_Palette = Union[bytes, bytearray, List[int], ImagePalette.ImagePalette] +_Palette = Union[bytes, bytearray, list[int], ImagePalette.ImagePalette] def _normalize_palette( diff --git a/src/PIL/Image.py b/src/PIL/Image.py index dbdb3b132ce..37266343437 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -38,7 +38,7 @@ import sys import tempfile import warnings -from collections.abc import Callable, MutableMapping +from collections.abc import Callable, MutableMapping, Sequence from enum import IntEnum from types import ModuleType from typing import ( @@ -47,8 +47,6 @@ Any, Literal, Protocol, - Sequence, - Tuple, cast, ) @@ -1097,7 +1095,7 @@ def convert_transparency( if trns is not None: try: new_im.info["transparency"] = new_im.palette.getcolor( - cast(Tuple[int, ...], trns), # trns was converted to RGB + cast(tuple[int, ...], trns), # trns was converted to RGB new_im, ) except Exception: @@ -3067,7 +3065,7 @@ def new( and isinstance(color, (list, tuple)) and all(isinstance(i, int) for i in color) ): - color_ints: tuple[int, ...] = cast(Tuple[int, ...], tuple(color)) + color_ints: tuple[int, ...] = cast(tuple[int, ...], tuple(color)) if len(color_ints) == 3 or len(color_ints) == 4: # RGB or RGBA value for a P image from . import ImagePalette diff --git a/src/PIL/ImageDraw.py b/src/PIL/ImageDraw.py index 244d3d5f022..2b3620e712c 100644 --- a/src/PIL/ImageDraw.py +++ b/src/PIL/ImageDraw.py @@ -34,8 +34,9 @@ import math import numbers import struct +from collections.abc import Sequence from types import ModuleType -from typing import TYPE_CHECKING, AnyStr, Callable, List, Sequence, Tuple, Union, cast +from typing import TYPE_CHECKING, AnyStr, Callable, Union, cast from . import Image, ImageColor from ._deprecate import deprecate @@ -51,7 +52,7 @@ if TYPE_CHECKING: from . import ImageDraw2, ImageFont -_Ink = Union[float, Tuple[int, ...], str] +_Ink = Union[float, tuple[int, ...], str] """ A simple 2D drawing interface for PIL images. @@ -1124,7 +1125,7 @@ def _compute_regular_polygon_vertices( msg = "bounding_circle should only contain numeric data" raise ValueError(msg) - *centroid, polygon_radius = cast(List[float], list(bounding_circle)) + *centroid, polygon_radius = cast(list[float], list(bounding_circle)) elif len(bounding_circle) == 2 and isinstance(bounding_circle[0], (list, tuple)): if not all( isinstance(i, (int, float)) for i in bounding_circle[0] @@ -1136,7 +1137,7 @@ def _compute_regular_polygon_vertices( msg = "bounding_circle centre should contain 2D coordinates (e.g. (x, y))" raise ValueError(msg) - centroid = cast(List[float], list(bounding_circle[0])) + centroid = cast(list[float], list(bounding_circle[0])) polygon_radius = cast(float, bounding_circle[1]) else: msg = ( diff --git a/src/PIL/ImageFilter.py b/src/PIL/ImageFilter.py index e18b4a4466a..8b0974b2c37 100644 --- a/src/PIL/ImageFilter.py +++ b/src/PIL/ImageFilter.py @@ -18,8 +18,9 @@ import abc import functools +from collections.abc import Sequence from types import ModuleType -from typing import TYPE_CHECKING, Any, Callable, Sequence, cast +from typing import TYPE_CHECKING, Any, Callable, cast if TYPE_CHECKING: from . import _imaging diff --git a/src/PIL/ImageOps.py b/src/PIL/ImageOps.py index 55335872d3e..50e3ebfba8b 100644 --- a/src/PIL/ImageOps.py +++ b/src/PIL/ImageOps.py @@ -21,7 +21,8 @@ import functools import operator import re -from typing import Protocol, Sequence, cast +from collections.abc import Sequence +from typing import Protocol, cast from . import ExifTags, Image, ImagePalette diff --git a/src/PIL/ImagePalette.py b/src/PIL/ImagePalette.py index ed38285dc28..8ccecbd07ca 100644 --- a/src/PIL/ImagePalette.py +++ b/src/PIL/ImagePalette.py @@ -18,7 +18,8 @@ from __future__ import annotations import array -from typing import IO, TYPE_CHECKING, Sequence +from collections.abc import Sequence +from typing import IO, TYPE_CHECKING from . import GimpGradientFile, GimpPaletteFile, ImageColor, PaletteFile diff --git a/src/PIL/ImageTransform.py b/src/PIL/ImageTransform.py index ffd7916745c..a3d8f441a9f 100644 --- a/src/PIL/ImageTransform.py +++ b/src/PIL/ImageTransform.py @@ -14,7 +14,8 @@ # from __future__ import annotations -from typing import Any, Sequence +from collections.abc import Sequence +from typing import Any from . import Image diff --git a/src/PIL/IptcImagePlugin.py b/src/PIL/IptcImagePlugin.py index 73df83bfb31..f9d5b75f0f9 100644 --- a/src/PIL/IptcImagePlugin.py +++ b/src/PIL/IptcImagePlugin.py @@ -16,8 +16,8 @@ # from __future__ import annotations +from collections.abc import Sequence from io import BytesIO -from typing import Sequence from . import Image, ImageFile from ._binary import i16be as i16 diff --git a/src/PIL/Jpeg2KImagePlugin.py b/src/PIL/Jpeg2KImagePlugin.py index e50cd7799a3..c8a57567aa6 100644 --- a/src/PIL/Jpeg2KImagePlugin.py +++ b/src/PIL/Jpeg2KImagePlugin.py @@ -18,7 +18,7 @@ import io import os import struct -from typing import IO, Tuple, cast +from typing import IO, cast from . import Image, ImageFile, ImagePalette, _binary @@ -82,7 +82,7 @@ def next_box_type(self) -> bytes: self.remaining_in_box = -1 # Read the length and type of the next box - lbox, tbox = cast(Tuple[int, bytes], self.read_fields(">I4s")) + lbox, tbox = cast(tuple[int, bytes], self.read_fields(">I4s")) if lbox == 1: lbox = cast(int, self.read_fields(">Q")[0]) hlen = 16 diff --git a/src/PIL/PdfParser.py b/src/PIL/PdfParser.py index 9e22313475e..622dc7de9e7 100644 --- a/src/PIL/PdfParser.py +++ b/src/PIL/PdfParser.py @@ -8,7 +8,7 @@ import re import time import zlib -from typing import TYPE_CHECKING, Any, List, NamedTuple, Union +from typing import TYPE_CHECKING, Any, NamedTuple, Union # see 7.9.2.2 Text String Type on page 86 and D.3 PDFDocEncoding Character Set @@ -240,7 +240,7 @@ def __bytes__(self) -> bytes: return bytes(result) -class PdfArray(List[Any]): +class PdfArray(list[Any]): def __bytes__(self) -> bytes: return b"[ " + b" ".join(pdf_repr(x) for x in self) + b" ]" diff --git a/src/PIL/SpiderImagePlugin.py b/src/PIL/SpiderImagePlugin.py index f5a09c3ef61..a07101e5461 100644 --- a/src/PIL/SpiderImagePlugin.py +++ b/src/PIL/SpiderImagePlugin.py @@ -37,7 +37,7 @@ import os import struct import sys -from typing import IO, TYPE_CHECKING, Any, Tuple, cast +from typing import IO, TYPE_CHECKING, Any, cast from . import Image, ImageFile @@ -187,7 +187,7 @@ def seek(self, frame: int) -> None: def convert2byte(self, depth: int = 255) -> Image.Image: extrema = self.getextrema() assert isinstance(extrema[0], float) - minimum, maximum = cast(Tuple[float, float], extrema) + minimum, maximum = cast(tuple[float, float], extrema) m: float = 1 if maximum != minimum: m = depth / (maximum - minimum) diff --git a/src/PIL/_typing.py b/src/PIL/_typing.py index 09ece18fa60..1db116f6de6 100644 --- a/src/PIL/_typing.py +++ b/src/PIL/_typing.py @@ -2,7 +2,8 @@ import os import sys -from typing import Any, Protocol, Sequence, TypeVar, Union +from collections.abc import Sequence +from typing import Any, Protocol, TypeVar, Union try: import numpy.typing as npt