Upgrade Python syntax with pyupgrade

This commit is contained in:
Hugo van Kemenade 2024-06-29 23:05:25 +03:00
parent 6879a77a6f
commit 49b218bbb1
22 changed files with 42 additions and 35 deletions

View File

@ -11,9 +11,10 @@ import subprocess
import sys import sys
import sysconfig import sysconfig
import tempfile import tempfile
from collections.abc import Sequence
from functools import lru_cache from functools import lru_cache
from io import BytesIO from io import BytesIO
from typing import Any, Callable, Sequence from typing import Any, Callable
import pytest import pytest
from packaging.version import parse as parse_version from packaging.version import parse as parse_version

View File

@ -385,9 +385,10 @@ def test_timeout(test_file: str) -> None:
def test_bounding_box_in_trailer() -> None: def test_bounding_box_in_trailer() -> None:
# Check bounding boxes are parsed in the same way # Check bounding boxes are parsed in the same way
# when specified in the header and the trailer # when specified in the header and the trailer
with Image.open("Tests/images/zero_bb_trailer.eps") as trailer_image, Image.open( with (
FILE1 Image.open("Tests/images/zero_bb_trailer.eps") as trailer_image,
) as header_image: Image.open(FILE1) as header_image,
):
assert trailer_image.size == header_image.size assert trailer_image.size == header_image.size

View File

@ -1,9 +1,9 @@
from __future__ import annotations from __future__ import annotations
import warnings import warnings
from collections.abc import Generator
from io import BytesIO from io import BytesIO
from pathlib import Path from pathlib import Path
from typing import Generator
import pytest import pytest

View File

@ -5,8 +5,9 @@ import os
import os.path import os.path
import tempfile import tempfile
import time import time
from collections.abc import Generator
from pathlib import Path from pathlib import Path
from typing import Any, Generator from typing import Any
import pytest import pytest

View File

@ -2,10 +2,10 @@ from __future__ import annotations
import os import os
import warnings import warnings
from collections.abc import Generator
from io import BytesIO from io import BytesIO
from pathlib import Path from pathlib import Path
from types import ModuleType from types import ModuleType
from typing import Generator
import pytest import pytest

View File

@ -1,7 +1,7 @@
from __future__ import annotations from __future__ import annotations
from collections.abc import Generator
from contextlib import contextmanager from contextlib import contextmanager
from typing import Generator
import pytest import pytest

View File

@ -4,9 +4,9 @@ Tests for resize functionality.
from __future__ import annotations from __future__ import annotations
from collections.abc import Generator
from itertools import permutations from itertools import permutations
from pathlib import Path from pathlib import Path
from typing import Generator
import pytest import pytest

View File

@ -2,7 +2,7 @@ from __future__ import annotations
import contextlib import contextlib
import os.path import os.path
from typing import Sequence from collections.abc import Sequence
import pytest import pytest

View File

@ -1,6 +1,6 @@
from __future__ import annotations from __future__ import annotations
from typing import Generator from collections.abc import Generator
import pytest import pytest

View File

@ -3,7 +3,7 @@ from __future__ import annotations
import array import array
import math import math
import struct import struct
from typing import Sequence from collections.abc import Sequence
import pytest import pytest

View File

@ -32,7 +32,7 @@ import subprocess
import sys import sys
from enum import IntEnum from enum import IntEnum
from functools import cached_property 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 ( from . import (
Image, Image,
@ -504,7 +504,7 @@ def _normalize_mode(im: Image.Image) -> Image.Image:
return im.convert("L") return im.convert("L")
_Palette = Union[bytes, bytearray, List[int], ImagePalette.ImagePalette] _Palette = Union[bytes, bytearray, list[int], ImagePalette.ImagePalette]
def _normalize_palette( def _normalize_palette(

View File

@ -38,7 +38,7 @@ import struct
import sys import sys
import tempfile import tempfile
import warnings import warnings
from collections.abc import Callable, MutableMapping from collections.abc import Callable, MutableMapping, Sequence
from enum import IntEnum from enum import IntEnum
from types import ModuleType from types import ModuleType
from typing import ( from typing import (
@ -47,8 +47,6 @@ from typing import (
Any, Any,
Literal, Literal,
Protocol, Protocol,
Sequence,
Tuple,
cast, cast,
) )
@ -1097,7 +1095,7 @@ class Image:
if trns is not None: if trns is not None:
try: try:
new_im.info["transparency"] = new_im.palette.getcolor( 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, new_im,
) )
except Exception: except Exception:
@ -3067,7 +3065,7 @@ def new(
and isinstance(color, (list, tuple)) and isinstance(color, (list, tuple))
and all(isinstance(i, int) for i in color) 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: if len(color_ints) == 3 or len(color_ints) == 4:
# RGB or RGBA value for a P image # RGB or RGBA value for a P image
from . import ImagePalette from . import ImagePalette

View File

@ -34,8 +34,9 @@ from __future__ import annotations
import math import math
import numbers import numbers
import struct import struct
from collections.abc import Sequence
from types import ModuleType 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 . import Image, ImageColor
from ._deprecate import deprecate from ._deprecate import deprecate
@ -51,7 +52,7 @@ except AttributeError:
if TYPE_CHECKING: if TYPE_CHECKING:
from . import ImageDraw2, ImageFont from . import ImageDraw2, ImageFont
_Ink = Union[float, Tuple[int, ...], str] _Ink = Union[float, tuple[int, ...], str]
""" """
A simple 2D drawing interface for PIL images. 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" msg = "bounding_circle should only contain numeric data"
raise ValueError(msg) 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)): elif len(bounding_circle) == 2 and isinstance(bounding_circle[0], (list, tuple)):
if not all( if not all(
isinstance(i, (int, float)) for i in bounding_circle[0] 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))" msg = "bounding_circle centre should contain 2D coordinates (e.g. (x, y))"
raise ValueError(msg) 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]) polygon_radius = cast(float, bounding_circle[1])
else: else:
msg = ( msg = (

View File

@ -18,8 +18,9 @@ from __future__ import annotations
import abc import abc
import functools import functools
from collections.abc import Sequence
from types import ModuleType from types import ModuleType
from typing import TYPE_CHECKING, Any, Callable, Sequence, cast from typing import TYPE_CHECKING, Any, Callable, cast
if TYPE_CHECKING: if TYPE_CHECKING:
from . import _imaging from . import _imaging

View File

@ -21,7 +21,8 @@ from __future__ import annotations
import functools import functools
import operator import operator
import re import re
from typing import Protocol, Sequence, cast from collections.abc import Sequence
from typing import Protocol, cast
from . import ExifTags, Image, ImagePalette from . import ExifTags, Image, ImagePalette

View File

@ -18,7 +18,8 @@
from __future__ import annotations from __future__ import annotations
import array 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 from . import GimpGradientFile, GimpPaletteFile, ImageColor, PaletteFile

View File

@ -14,7 +14,8 @@
# #
from __future__ import annotations from __future__ import annotations
from typing import Any, Sequence from collections.abc import Sequence
from typing import Any
from . import Image from . import Image

View File

@ -16,8 +16,8 @@
# #
from __future__ import annotations from __future__ import annotations
from collections.abc import Sequence
from io import BytesIO from io import BytesIO
from typing import Sequence
from . import Image, ImageFile from . import Image, ImageFile
from ._binary import i16be as i16 from ._binary import i16be as i16

View File

@ -18,7 +18,7 @@ from __future__ import annotations
import io import io
import os import os
import struct import struct
from typing import IO, Tuple, cast from typing import IO, cast
from . import Image, ImageFile, ImagePalette, _binary from . import Image, ImageFile, ImagePalette, _binary
@ -82,7 +82,7 @@ class BoxReader:
self.remaining_in_box = -1 self.remaining_in_box = -1
# Read the length and type of the next box # 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: if lbox == 1:
lbox = cast(int, self.read_fields(">Q")[0]) lbox = cast(int, self.read_fields(">Q")[0])
hlen = 16 hlen = 16

View File

@ -8,7 +8,7 @@ import os
import re import re
import time import time
import zlib 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 # see 7.9.2.2 Text String Type on page 86 and D.3 PDFDocEncoding Character Set
@ -240,7 +240,7 @@ class PdfName:
return bytes(result) return bytes(result)
class PdfArray(List[Any]): class PdfArray(list[Any]):
def __bytes__(self) -> bytes: def __bytes__(self) -> bytes:
return b"[ " + b" ".join(pdf_repr(x) for x in self) + b" ]" return b"[ " + b" ".join(pdf_repr(x) for x in self) + b" ]"

View File

@ -37,7 +37,7 @@ from __future__ import annotations
import os import os
import struct import struct
import sys import sys
from typing import IO, TYPE_CHECKING, Any, Tuple, cast from typing import IO, TYPE_CHECKING, Any, cast
from . import Image, ImageFile from . import Image, ImageFile
@ -187,7 +187,7 @@ class SpiderImageFile(ImageFile.ImageFile):
def convert2byte(self, depth: int = 255) -> Image.Image: def convert2byte(self, depth: int = 255) -> Image.Image:
extrema = self.getextrema() extrema = self.getextrema()
assert isinstance(extrema[0], float) assert isinstance(extrema[0], float)
minimum, maximum = cast(Tuple[float, float], extrema) minimum, maximum = cast(tuple[float, float], extrema)
m: float = 1 m: float = 1
if maximum != minimum: if maximum != minimum:
m = depth / (maximum - minimum) m = depth / (maximum - minimum)

View File

@ -2,7 +2,8 @@ from __future__ import annotations
import os import os
import sys import sys
from typing import Any, Protocol, Sequence, TypeVar, Union from collections.abc import Sequence
from typing import Any, Protocol, TypeVar, Union
try: try:
import numpy.typing as npt import numpy.typing as npt