Skip to content

Commit 7552893

Browse files
authored
Merge pull request #8799 from hugovk/flake8-pie
Call `startswith`/`endswith` once with a tuple, and `range` without `start=0`
2 parents c7ed097 + a2b13cc commit 7552893

13 files changed

+25
-24
lines changed

Tests/test_file_gif.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@ def test_save_dispose(tmp_path: Path) -> None:
601601
Image.new("L", (100, 100), "#111"),
602602
Image.new("L", (100, 100), "#222"),
603603
]
604-
for method in range(0, 4):
604+
for method in range(4):
605605
im_list[0].save(out, save_all=True, append_images=im_list[1:], disposal=method)
606606
with Image.open(out) as img:
607607
for _ in range(2):

Tests/test_file_webp.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ def test_background_from_gif(self, tmp_path: Path) -> None:
231231

232232
with Image.open(out_gif) as reread:
233233
reread_value = reread.convert("RGB").getpixel((1, 1))
234-
difference = sum(abs(original_value[i] - reread_value[i]) for i in range(0, 3))
234+
difference = sum(abs(original_value[i] - reread_value[i]) for i in range(3))
235235
assert difference < 5
236236

237237
def test_duration(self, tmp_path: Path) -> None:

Tests/test_imagedraw.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1044,8 +1044,8 @@ def create_base_image_draw(
10441044
background2: tuple[int, int, int] = GRAY,
10451045
) -> tuple[Image.Image, ImageDraw.ImageDraw]:
10461046
img = Image.new(mode, size, background1)
1047-
for x in range(0, size[0]):
1048-
for y in range(0, size[1]):
1047+
for x in range(size[0]):
1048+
for y in range(size[1]):
10491049
if (x + y) % 2 == 0:
10501050
img.putpixel((x, y), background2)
10511051
return img, ImageDraw.Draw(img)

Tests/test_imagepalette.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ def test_make_linear_lut() -> None:
112112
assert isinstance(lut, list)
113113
assert len(lut) == 256
114114
# Check values
115-
for i in range(0, len(lut)):
115+
for i in range(len(lut)):
116116
assert lut[i] == i
117117

118118

Tests/test_imagesequence.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def test_sanity(tmp_path: Path) -> None:
3232
def test_iterator() -> None:
3333
with Image.open("Tests/images/multipage.tiff") as im:
3434
i = ImageSequence.Iterator(im)
35-
for index in range(0, im.n_frames):
35+
for index in range(im.n_frames):
3636
assert i[index] == next(i)
3737
with pytest.raises(IndexError):
3838
i[index + 1]

Tests/test_pickle.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def helper_pickle_string(protocol: int, test_file: str, mode: str | None) -> Non
6565
("Tests/images/itxt_chunks.png", None),
6666
],
6767
)
68-
@pytest.mark.parametrize("protocol", range(0, pickle.HIGHEST_PROTOCOL + 1))
68+
@pytest.mark.parametrize("protocol", range(pickle.HIGHEST_PROTOCOL + 1))
6969
def test_pickle_image(
7070
tmp_path: Path, test_file: str, test_mode: str | None, protocol: int
7171
) -> None:
@@ -92,7 +92,7 @@ def test_pickle_la_mode_with_palette(tmp_path: Path) -> None:
9292
im = im.convert("PA")
9393

9494
# Act / Assert
95-
for protocol in range(0, pickle.HIGHEST_PROTOCOL + 1):
95+
for protocol in range(pickle.HIGHEST_PROTOCOL + 1):
9696
im._mode = "LA"
9797
with open(filename, "wb") as f:
9898
pickle.dump(im, f, protocol)
@@ -133,7 +133,7 @@ def helper_assert_pickled_font_images(
133133

134134

135135
@skip_unless_feature("freetype2")
136-
@pytest.mark.parametrize("protocol", list(range(0, pickle.HIGHEST_PROTOCOL + 1)))
136+
@pytest.mark.parametrize("protocol", list(range(pickle.HIGHEST_PROTOCOL + 1)))
137137
def test_pickle_font_string(protocol: int) -> None:
138138
# Arrange
139139
font = ImageFont.truetype(FONT_PATH, FONT_SIZE)
@@ -147,7 +147,7 @@ def test_pickle_font_string(protocol: int) -> None:
147147

148148

149149
@skip_unless_feature("freetype2")
150-
@pytest.mark.parametrize("protocol", list(range(0, pickle.HIGHEST_PROTOCOL + 1)))
150+
@pytest.mark.parametrize("protocol", list(range(pickle.HIGHEST_PROTOCOL + 1)))
151151
def test_pickle_font_file(tmp_path: Path, protocol: int) -> None:
152152
# Arrange
153153
font = ImageFont.truetype(FONT_PATH, FONT_SIZE)

pyproject.toml

+2
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ lint.select = [
121121
"ISC", # flake8-implicit-str-concat
122122
"LOG", # flake8-logging
123123
"PGH", # pygrep-hooks
124+
"PIE", # flake8-pie
124125
"PT", # flake8-pytest-style
125126
"PYI", # flake8-pyi
126127
"RUF100", # unused noqa (yesqa)
@@ -133,6 +134,7 @@ lint.ignore = [
133134
"E221", # Multiple spaces before operator
134135
"E226", # Missing whitespace around arithmetic operator
135136
"E241", # Multiple spaces after ','
137+
"PIE790", # flake8-pie: unnecessary-placeholder
136138
"PT001", # pytest-fixture-incorrect-parentheses-style
137139
"PT007", # pytest-parametrize-values-wrong-type
138140
"PT011", # pytest-raises-too-broad

src/PIL/IcnsImagePlugin.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,7 @@ def read_png_or_jpeg2000(
123123
Image._decompression_bomb_check(im.size)
124124
return {"RGBA": im}
125125
elif (
126-
sig.startswith(b"\xff\x4f\xff\x51")
127-
or sig.startswith(b"\x0d\x0a\x87\x0a")
126+
sig.startswith((b"\xff\x4f\xff\x51", b"\x0d\x0a\x87\x0a"))
128127
or sig == b"\x00\x00\x00\x0cjP \x0d\x0a\x87\x0a"
129128
):
130129
if not enable_jpeg2k:

src/PIL/Image.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1001,7 +1001,7 @@ def convert_transparency(
10011001
elif len(mode) == 3:
10021002
transparency = tuple(
10031003
convert_transparency(matrix[i * 4 : i * 4 + 4], transparency)
1004-
for i in range(0, len(transparency))
1004+
for i in range(len(transparency))
10051005
)
10061006
new_im.info["transparency"] = transparency
10071007
return new_im
@@ -4003,7 +4003,7 @@ def get_ifd(self, tag: int) -> dict[int, Any]:
40034003
ifd_data = tag_data[ifd_offset:]
40044004

40054005
makernote = {}
4006-
for i in range(0, struct.unpack("<H", ifd_data[:2])[0]):
4006+
for i in range(struct.unpack("<H", ifd_data[:2])[0]):
40074007
ifd_tag, typ, count, data = struct.unpack(
40084008
"<HHL4s", ifd_data[i * 12 + 2 : (i + 1) * 12 + 2]
40094009
)
@@ -4038,7 +4038,7 @@ def get_ifd(self, tag: int) -> dict[int, Any]:
40384038
self._ifds[tag] = dict(self._fixup_dict(makernote))
40394039
elif self.get(0x010F) == "Nintendo":
40404040
makernote = {}
4041-
for i in range(0, struct.unpack(">H", tag_data[:2])[0]):
4041+
for i in range(struct.unpack(">H", tag_data[:2])[0]):
40424042
ifd_tag, typ, count, data = struct.unpack(
40434043
">HHL4s", tag_data[i * 12 + 2 : (i + 1) * 12 + 2]
40444044
)

src/PIL/ImageDraw.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1204,7 +1204,7 @@ def _get_angles(n_sides: int, rotation: float) -> list[float]:
12041204
degrees = 360 / n_sides
12051205
# Start with the bottom left polygon vertex
12061206
current_angle = (270 - 0.5 * degrees) + rotation
1207-
for _ in range(0, n_sides):
1207+
for _ in range(n_sides):
12081208
angles.append(current_angle)
12091209
current_angle += degrees
12101210
if current_angle > 360:
@@ -1227,4 +1227,4 @@ def _color_diff(
12271227
first = color1 if isinstance(color1, tuple) else (color1,)
12281228
second = color2 if isinstance(color2, tuple) else (color2,)
12291229

1230-
return sum(abs(first[i] - second[i]) for i in range(0, len(second)))
1230+
return sum(abs(first[i] - second[i]) for i in range(len(second)))

src/PIL/ImageOps.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -213,14 +213,14 @@ def colorize(
213213
blue = []
214214

215215
# Create the low-end values
216-
for i in range(0, blackpoint):
216+
for i in range(blackpoint):
217217
red.append(rgb_black[0])
218218
green.append(rgb_black[1])
219219
blue.append(rgb_black[2])
220220

221221
# Create the mapping (2-color)
222222
if rgb_mid is None:
223-
range_map = range(0, whitepoint - blackpoint)
223+
range_map = range(whitepoint - blackpoint)
224224

225225
for i in range_map:
226226
red.append(
@@ -235,8 +235,8 @@ def colorize(
235235

236236
# Create the mapping (3-color)
237237
else:
238-
range_map1 = range(0, midpoint - blackpoint)
239-
range_map2 = range(0, whitepoint - midpoint)
238+
range_map1 = range(midpoint - blackpoint)
239+
range_map2 = range(whitepoint - midpoint)
240240

241241
for i in range_map1:
242242
red.append(
@@ -256,7 +256,7 @@ def colorize(
256256
blue.append(rgb_mid[2] + i * (rgb_white[2] - rgb_mid[2]) // len(range_map2))
257257

258258
# Create the high-end values
259-
for i in range(0, 256 - whitepoint):
259+
for i in range(256 - whitepoint):
260260
red.append(rgb_white[0])
261261
green.append(rgb_white[1])
262262
blue.append(rgb_white[2])

src/PIL/JpegImagePlugin.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,7 @@ def _getmp(self: JpegImageFile) -> dict[int, Any] | None:
569569
mpentries = []
570570
try:
571571
rawmpentries = mp[0xB002]
572-
for entrynum in range(0, quant):
572+
for entrynum in range(quant):
573573
unpackedentry = struct.unpack_from(
574574
f"{endianness}LLLHH", rawmpentries, entrynum * 16
575575
)

src/PIL/TiffImagePlugin.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1584,7 +1584,7 @@ def _setup(self) -> None:
15841584
# byte order.
15851585
elif rawmode == "I;16":
15861586
rawmode = "I;16N"
1587-
elif rawmode.endswith(";16B") or rawmode.endswith(";16L"):
1587+
elif rawmode.endswith((";16B", ";16L")):
15881588
rawmode = rawmode[:-1] + "N"
15891589

15901590
# Offset in the tile tuple is 0, we go from 0,0 to

0 commit comments

Comments
 (0)