Skip to content

Commit 76446ee

Browse files
authored
Merge pull request #7524 from cclauss/ruff-rules-C4-PERF102-PIE810-PLR
2 parents 316f397 + eb8405b commit 76446ee

File tree

12 files changed

+23
-27
lines changed

12 files changed

+23
-27
lines changed

Tests/test_image_quantize.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def test_quantize_no_dither():
6767

6868
def test_quantize_no_dither2():
6969
im = Image.new("RGB", (9, 1))
70-
im.putdata(list((p,) * 3 for p in range(0, 36, 4)))
70+
im.putdata([(p,) * 3 for p in range(0, 36, 4)])
7171

7272
palette = Image.new("P", (1, 1))
7373
data = (0, 0, 0, 32, 32, 32)

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ test-extras = "tests"
9696
[tool.ruff]
9797
line-length = 88
9898
select = [
99+
"C4", # flake8-comprehensions
99100
"E", # pycodestyle errors
100101
"EM", # flake8-errmsg
101102
"F", # pyflakes errors

setup.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -440,17 +440,17 @@ def build_extensions(self):
440440

441441
#
442442
# add configured kits
443-
for root_name, lib_name in dict(
444-
JPEG_ROOT="libjpeg",
445-
JPEG2K_ROOT="libopenjp2",
446-
TIFF_ROOT=("libtiff-5", "libtiff-4"),
447-
ZLIB_ROOT="zlib",
448-
FREETYPE_ROOT="freetype2",
449-
HARFBUZZ_ROOT="harfbuzz",
450-
FRIBIDI_ROOT="fribidi",
451-
LCMS_ROOT="lcms2",
452-
IMAGEQUANT_ROOT="libimagequant",
453-
).items():
443+
for root_name, lib_name in {
444+
"JPEG_ROOT": "libjpeg",
445+
"JPEG2K_ROOT": "libopenjp2",
446+
"TIFF_ROOT": ("libtiff-5", "libtiff-4"),
447+
"ZLIB_ROOT": "zlib",
448+
"FREETYPE_ROOT": "freetype2",
449+
"HARFBUZZ_ROOT": "harfbuzz",
450+
"FRIBIDI_ROOT": "fribidi",
451+
"LCMS_ROOT": "lcms2",
452+
"IMAGEQUANT_ROOT": "libimagequant",
453+
}.items():
454454
root = globals()[root_name]
455455

456456
if root is None and root_name in os.environ:

src/PIL/BmpImagePlugin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ def _save(im, fp, filename, bitmap_header=True):
396396
dpi = info.get("dpi", (96, 96))
397397

398398
# 1 meter == 39.3701 inches
399-
ppm = tuple(map(lambda x: int(x * 39.3701 + 0.5), dpi))
399+
ppm = tuple(int(x * 39.3701 + 0.5) for x in dpi)
400400

401401
stride = ((im.size[0] * bits + 7) // 8 + 3) & (~3)
402402
header = 40 # or 64 for OS/2 version 2

src/PIL/CurImagePlugin.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,6 @@ def _open(self):
6464
d, e, o, a = self.tile[0]
6565
self.tile[0] = d, (0, 0) + self.size, o, a
6666

67-
return
68-
6967

7068
#
7169
# --------------------------------------------------------------------

src/PIL/Image.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
from pathlib import Path
4141

4242
try:
43-
import defusedxml.ElementTree as ElementTree
43+
from defusedxml import ElementTree
4444
except ImportError:
4545
ElementTree = None
4646

@@ -1160,7 +1160,7 @@ def quantize(
11601160
if palette.mode != "P":
11611161
msg = "bad mode for palette image"
11621162
raise ValueError(msg)
1163-
if self.mode != "RGB" and self.mode != "L":
1163+
if self.mode not in {"RGB", "L"}:
11641164
msg = "only RGB or L mode images can be quantized to a palette"
11651165
raise ValueError(msg)
11661166
im = self.im.convert("P", dither, palette.im)

src/PIL/ImageDraw.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -921,7 +921,7 @@ def floodfill(image, xy, value, border=None, thresh=0):
921921
if border is None:
922922
fill = _color_diff(p, background) <= thresh
923923
else:
924-
fill = p != value and p != border
924+
fill = p not in (value, border)
925925
if fill:
926926
pixel[s, t] = value
927927
new_edge.add((s, t))

src/PIL/Jpeg2KImagePlugin.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -334,10 +334,7 @@ def _save(im, fp, filename):
334334
if quality_layers is not None and not (
335335
isinstance(quality_layers, (list, tuple))
336336
and all(
337-
[
338-
isinstance(quality_layer, (int, float))
339-
for quality_layer in quality_layers
340-
]
337+
isinstance(quality_layer, (int, float)) for quality_layer in quality_layers
341338
)
342339
):
343340
msg = "quality_layers must be a sequence of numbers"

src/PIL/JpegImagePlugin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ def _open(self):
397397
# self.__offset = self.fp.tell()
398398
break
399399
s = self.fp.read(1)
400-
elif i == 0 or i == 0xFFFF:
400+
elif i in {0, 0xFFFF}:
401401
# padded marker or junk; move on
402402
s = b"\xff"
403403
elif i == 0xFF00: # Skip extraneous data (escaped 0xFF)

src/PIL/SgiImagePlugin.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ def _open(self):
123123

124124

125125
def _save(im, fp, filename):
126-
if im.mode != "RGB" and im.mode != "RGBA" and im.mode != "L":
126+
if im.mode not in {"RGB", "RGBA", "L"}:
127127
msg = "Unsupported SGI image mode"
128128
raise ValueError(msg)
129129

@@ -155,7 +155,7 @@ def _save(im, fp, filename):
155155
# Z Dimension: Number of channels
156156
z = len(im.mode)
157157

158-
if dim == 1 or dim == 2:
158+
if dim in {1, 2}:
159159
z = 1
160160

161161
# assert we've got the right number of bands.

0 commit comments

Comments
 (0)