Skip to content

Commit

Permalink
Upgrade Python syntax with pyupgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk committed Jul 1, 2024
1 parent 725dade commit bf0cbd0
Show file tree
Hide file tree
Showing 22 changed files with 42 additions and 35 deletions.
3 changes: 2 additions & 1 deletion Tests/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions Tests/test_file_eps.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,9 +425,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


Expand Down
2 changes: 1 addition & 1 deletion Tests/test_file_gif.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down
3 changes: 2 additions & 1 deletion Tests/test_file_pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

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

Expand Down
2 changes: 1 addition & 1 deletion Tests/test_image_resample.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

from collections.abc import Generator
from contextlib import contextmanager
from typing import Generator

import pytest

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

Expand Down
2 changes: 1 addition & 1 deletion Tests/test_imagedraw.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import contextlib
import os.path
from typing import Sequence
from collections.abc import Sequence

import pytest

Expand Down
2 changes: 1 addition & 1 deletion Tests/test_imageops_usm.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

from typing import Generator
from collections.abc import Generator

import pytest

Expand Down
2 changes: 1 addition & 1 deletion Tests/test_imagepath.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import array
import math
import struct
from typing import Sequence
from collections.abc import Sequence

import pytest

Expand Down
4 changes: 2 additions & 2 deletions src/PIL/GifImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -505,7 +505,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(
Expand Down
8 changes: 3 additions & 5 deletions src/PIL/Image.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -47,8 +47,6 @@
Any,
Literal,
Protocol,
Sequence,
Tuple,
cast,
)

Expand Down Expand Up @@ -1115,7 +1113,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:
Expand Down Expand Up @@ -3092,7 +3090,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
Expand Down
9 changes: 5 additions & 4 deletions src/PIL/ImageDraw.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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]
Expand All @@ -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 = (
Expand Down
3 changes: 2 additions & 1 deletion src/PIL/ImageFilter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/PIL/ImageOps.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

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

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

Expand Down
2 changes: 1 addition & 1 deletion src/PIL/IptcImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/PIL/Jpeg2KImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/PIL/PdfParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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" ]"

Expand Down
4 changes: 2 additions & 2 deletions src/PIL/SpiderImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion src/PIL/_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit bf0cbd0

Please sign in to comment.