Skip to content

Commit

Permalink
Upgrade Python syntax with pyupgrade --py36-plus
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk committed Oct 15, 2021
1 parent f99d47f commit 43fac1c
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 27 deletions.
4 changes: 2 additions & 2 deletions Tests/test_file_gif.py
Original file line number Diff line number Diff line change
Expand Up @@ -818,7 +818,7 @@ def test_palette_save_P(tmp_path):
# Forcing a non-straight grayscale palette.

im = hopper("P")
palette = bytes([255 - i // 3 for i in range(768)])
palette = bytes(255 - i // 3 for i in range(768))

out = str(tmp_path / "temp.gif")
im.save(out, palette=palette)
Expand Down Expand Up @@ -885,7 +885,7 @@ def test_getdata():
im.putpalette(ImagePalette.ImagePalette("RGB"))
im.info = {"background": 0}

passed_palette = bytes([255 - i // 3 for i in range(768)])
passed_palette = bytes(255 - i // 3 for i in range(768))

GifImagePlugin._FORCE_OPTIMIZE = True
try:
Expand Down
12 changes: 6 additions & 6 deletions Tests/test_file_jpeg.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,26 +85,26 @@ def test_cmyk(self):
f = "Tests/images/pil_sample_cmyk.jpg"
with Image.open(f) as im:
# the source image has red pixels in the upper left corner.
c, m, y, k = [x / 255.0 for x in im.getpixel((0, 0))]
c, m, y, k = (x / 255.0 for x in im.getpixel((0, 0)))
assert c == 0.0
assert m > 0.8
assert y > 0.8
assert k == 0.0
# the opposite corner is black
c, m, y, k = [
c, m, y, k = (
x / 255.0 for x in im.getpixel((im.size[0] - 1, im.size[1] - 1))
]
)
assert k > 0.9
# roundtrip, and check again
im = self.roundtrip(im)
c, m, y, k = [x / 255.0 for x in im.getpixel((0, 0))]
c, m, y, k = (x / 255.0 for x in im.getpixel((0, 0)))
assert c == 0.0
assert m > 0.8
assert y > 0.8
assert k == 0.0
c, m, y, k = [
c, m, y, k = (
x / 255.0 for x in im.getpixel((im.size[0] - 1, im.size[1] - 1))
]
)
assert k > 0.9

@pytest.mark.parametrize(
Expand Down
2 changes: 1 addition & 1 deletion Tests/test_file_webp.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ def test_background_from_gif(self, tmp_path):
with Image.open(out_gif) as reread:
reread_value = reread.convert("RGB").getpixel((1, 1))
difference = sum(
[abs(original_value[i] - reread_value[i]) for i in range(0, 3)]
abs(original_value[i] - reread_value[i]) for i in range(0, 3)
)
assert difference < 5

Expand Down
8 changes: 4 additions & 4 deletions Tests/test_imagefont.py
Original file line number Diff line number Diff line change
Expand Up @@ -909,7 +909,7 @@ def test_cbdt(self):
d.text((10, 10), "\U0001f469", font=font, embedded_color=True)

assert_image_similar_tofile(im, "Tests/images/cbdt_notocoloremoji.png", 6.2)
except IOError as e: # pragma: no cover
except OSError as e: # pragma: no cover
assert str(e) in ("unimplemented feature", "unknown file format")
pytest.skip("freetype compiled without libpng or CBDT support")

Expand All @@ -930,7 +930,7 @@ def test_cbdt_mask(self):
assert_image_similar_tofile(
im, "Tests/images/cbdt_notocoloremoji_mask.png", 6.2
)
except IOError as e: # pragma: no cover
except OSError as e: # pragma: no cover
assert str(e) in ("unimplemented feature", "unknown file format")
pytest.skip("freetype compiled without libpng or CBDT support")

Expand All @@ -949,7 +949,7 @@ def test_sbix(self):
d.text((50, 50), "\uE901", font=font, embedded_color=True)

assert_image_similar_tofile(im, "Tests/images/chromacheck-sbix.png", 1)
except IOError as e: # pragma: no cover
except OSError as e: # pragma: no cover
assert str(e) in ("unimplemented feature", "unknown file format")
pytest.skip("freetype compiled without libpng or SBIX support")

Expand All @@ -968,7 +968,7 @@ def test_sbix_mask(self):
d.text((50, 50), "\uE901", (100, 0, 0), font=font)

assert_image_similar_tofile(im, "Tests/images/chromacheck-sbix_mask.png", 1)
except IOError as e: # pragma: no cover
except OSError as e: # pragma: no cover
assert str(e) in ("unimplemented feature", "unknown file format")
pytest.skip("freetype compiled without libpng or SBIX support")

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

def pixel(im):
if hasattr(im, "im"):
return "{} {}".format(im.mode, repr(im.getpixel((0, 0))))
return f"{im.mode} {repr(im.getpixel((0, 0)))}"
else:
if isinstance(im, int):
return int(im) # hack to deal with booleans
Expand Down
2 changes: 1 addition & 1 deletion src/PIL/GifImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ def load_prepare(self):
if not self.im and "transparency" in self.info:
self.im = Image.core.fill(self.mode, self.size, self.info["transparency"])

super(GifImageFile, self).load_prepare()
super().load_prepare()

def tell(self):
return self.__frame
Expand Down
8 changes: 4 additions & 4 deletions src/PIL/Image.py
Original file line number Diff line number Diff line change
Expand Up @@ -943,12 +943,12 @@ def convert_transparency(m, v):
transparency = convert_transparency(matrix, transparency)
elif len(mode) == 3:
transparency = tuple(
[

convert_transparency(
matrix[i * 4 : i * 4 + 4], transparency
)
for i in range(0, len(transparency))
]

)
new.info["transparency"] = transparency
return new
Expand Down Expand Up @@ -1951,7 +1951,7 @@ def resize(self, size, resample=None, box=None, reducing_gap=None):
message = f"Unknown resampling filter ({resample})."

filters = [
"{} ({})".format(filter[1], filter[0])
f"{filter[1]} ({filter[0]})"
for filter in (
(NEAREST, "Image.NEAREST"),
(LANCZOS, "Image.LANCZOS"),
Expand Down Expand Up @@ -2553,7 +2553,7 @@ def __transformer(self, box, image, method, data, resample=NEAREST, fill=1):
message = f"Unknown resampling filter ({resample})."

filters = [
"{} ({})".format(filter[1], filter[0])
f"{filter[1]} ({filter[0]})"
for filter in (
(NEAREST, "Image.NEAREST"),
(BILINEAR, "Image.BILINEAR"),
Expand Down
6 changes: 3 additions & 3 deletions src/PIL/ImageDraw.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,13 +174,13 @@ def coord_at_angle(coord, angle):
angle -= 90
distance = width / 2 - 1
return tuple(
[

p + (math.floor(p_d) if p_d > 0 else math.ceil(p_d))
for p, p_d in (
(x, distance * math.cos(math.radians(angle))),
(y, distance * math.sin(math.radians(angle))),
)
]

)

flipped = (
Expand Down Expand Up @@ -979,6 +979,6 @@ def _color_diff(color1, color2):
Uses 1-norm distance to calculate difference between two values.
"""
if isinstance(color2, tuple):
return sum([abs(color1[i] - color2[i]) for i in range(0, len(color2))])
return sum(abs(color1[i] - color2[i]) for i in range(0, len(color2)))
else:
return abs(color1 - color2)
4 changes: 2 additions & 2 deletions src/PIL/PdfParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ def write_header(self):
self.f.write(b"%PDF-1.4\n")

def write_comment(self, s):
self.f.write(f"% {s}\n".encode("utf-8"))
self.f.write(f"% {s}\n".encode())

def write_catalog(self):
self.del_root()
Expand Down Expand Up @@ -862,7 +862,7 @@ def get_value(cls, data, offset, expect_indirect=None, max_nesting=-1):
if m:
# filter out whitespace
hex_string = bytearray(
[b for b in m.group(1) if b in b"0123456789abcdefABCDEF"]
b for b in m.group(1) if b in b"0123456789abcdefABCDEF"
)
if len(hex_string) % 2 == 1:
# append a 0 if the length is not even - yes, at the end
Expand Down
6 changes: 3 additions & 3 deletions src/PIL/TiffImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,7 @@ def _register_basic(idx_fmt_name):
_load_dispatch[idx] = ( # noqa: F821
size,
lambda self, data, legacy_api=True: (
self._unpack("{}{}".format(len(data) // size, fmt), data)
self._unpack(f"{len(data) // size}{fmt}", data)
),
)
_write_dispatch[idx] = lambda self, *values: ( # noqa: F821
Expand Down Expand Up @@ -718,7 +718,7 @@ def write_string(self, value):

@_register_loader(5, 8)
def load_rational(self, data, legacy_api=True):
vals = self._unpack("{}L".format(len(data) // 4), data)
vals = self._unpack(f"{len(data) // 4}L", data)

def combine(a, b):
return (a, b) if legacy_api else IFDRational(a, b)
Expand All @@ -741,7 +741,7 @@ def write_undefined(self, value):

@_register_loader(10, 8)
def load_signed_rational(self, data, legacy_api=True):
vals = self._unpack("{}l".format(len(data) // 4), data)
vals = self._unpack(f"{len(data) // 4}l", data)

def combine(a, b):
return (a, b) if legacy_api else IFDRational(a, b)
Expand Down

0 comments on commit 43fac1c

Please sign in to comment.