Skip to content

Commit 754c7ea

Browse files
committed
PERF203 and fixes
1 parent 090ca94 commit 754c7ea

File tree

11 files changed

+29
-38
lines changed

11 files changed

+29
-38
lines changed

Tests/test_bmp_reference.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def test_questionable() -> None:
5656
im.load()
5757
if os.path.basename(f) not in supported:
5858
print(f"Please add {f} to the partially supported bmp specs.")
59-
except Exception: # as msg:
59+
except Exception: # noqa: PERF203
6060
if os.path.basename(f) in supported:
6161
raise
6262

@@ -106,7 +106,7 @@ def get_compare(f: str) -> str:
106106

107107
assert_image_similar(im_converted, compare_converted, 5)
108108

109-
except Exception as msg:
109+
except Exception as msg: # noqa: PERF203
110110
# there are three here that are unsupported:
111111
unsupported = (
112112
os.path.join(base, "g", "rgb32bf.bmp"),

Tests/test_file_libtiff.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -224,10 +224,7 @@ def test_additional_metadata(
224224
with Image.open("Tests/images/hopper_g4.tif") as im:
225225
assert isinstance(im, TiffImagePlugin.TiffImageFile)
226226
for tag in im.tag_v2:
227-
try:
228-
del core_items[tag]
229-
except KeyError:
230-
pass
227+
core_items.pop(tag, None)
231228
del core_items[320] # colormap is special, tested below
232229

233230
# Type codes:

pyproject.toml

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -139,26 +139,22 @@ exclude = "wheels/multibuild"
139139
exclude = [ "wheels/multibuild" ]
140140
fix = true
141141
lint.select = [
142-
"C4", # flake8-comprehensions
143-
"E", # pycodestyle errors
144-
"EM", # flake8-errmsg
145-
"F", # pyflakes errors
146-
"I", # isort
147-
"ISC", # flake8-implicit-str-concat
148-
"LOG", # flake8-logging
149-
"PERF101", # perflint: unnecessary-list-cast
150-
"PERF102", # perflint: incorrect-dict-iterator
151-
"PERF401", # perflint: manual-list-comprehension
152-
"PERF402", # perflint: manual-list-copy
153-
"PERF403", # perflint: manual-dict-comprehension
154-
"PGH", # pygrep-hooks
155-
"PIE", # flake8-pie
156-
"PT", # flake8-pytest-style
157-
"PYI", # flake8-pyi
158-
"RUF100", # unused noqa (yesqa)
159-
"UP", # pyupgrade
160-
"W", # pycodestyle warnings
161-
"YTT", # flake8-2020
142+
"C4", # flake8-comprehensions
143+
"E", # pycodestyle errors
144+
"EM", # flake8-errmsg
145+
"F", # pyflakes errors
146+
"I", # isort
147+
"ISC", # flake8-implicit-str-concat
148+
"LOG", # flake8-logging
149+
"PERF", # perflint
150+
"PGH", # pygrep-hooks
151+
"PIE", # flake8-pie
152+
"PT", # flake8-pytest-style
153+
"PYI", # flake8-pyi
154+
"RUF100", # unused noqa (yesqa)
155+
"UP", # pyupgrade
156+
"W", # pycodestyle warnings
157+
"YTT", # flake8-2020
162158
]
163159
lint.ignore = [
164160
"E203", # Whitespace before ':'

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ def _pkg_config(name: str) -> tuple[list[str], list[str]] | None:
302302
subprocess.check_output(command_cflags).decode("utf8").strip(),
303303
)[::2][1:]
304304
return libs, cflags
305-
except Exception:
305+
except Exception: # noqa: PERF203
306306
pass
307307
return None
308308

src/PIL/GifImagePlugin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ def seek(self, frame: int) -> None:
167167
for f in range(self.__frame + 1, frame + 1):
168168
try:
169169
self._seek(f)
170-
except EOFError as e:
170+
except EOFError as e: # noqa: PERF203
171171
self.seek(last_frame)
172172
msg = "no more images in GIF file"
173173
raise EOFError(msg) from e

src/PIL/Image.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ def init() -> bool:
488488
try:
489489
logger.debug("Importing %s", plugin)
490490
__import__(f"{__spec__.parent}.{plugin}", globals(), locals(), [])
491-
except ImportError as e:
491+
except ImportError as e: # noqa: PERF203
492492
logger.debug("Image: failed to import %s: %s", plugin, e)
493493

494494
if OPEN or SAVE:

src/PIL/ImageFont.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -930,7 +930,7 @@ def load_path(filename: str | bytes) -> ImageFont:
930930
for directory in sys.path:
931931
try:
932932
return load(os.path.join(directory, filename))
933-
except OSError:
933+
except OSError: # noqa: PERF203
934934
pass
935935
msg = f'cannot find font file "{filename}" in sys.path'
936936
if os.path.exists(filename):

src/PIL/ImagePalette.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,13 +198,11 @@ def save(self, fp: str | IO[str]) -> None:
198198
try:
199199
fp.write("# Palette\n")
200200
fp.write(f"# Mode: {self.mode}\n")
201+
palette_len = len(self.palette)
201202
for i in range(256):
202203
fp.write(f"{i}")
203204
for j in range(i * len(self.mode), (i + 1) * len(self.mode)):
204-
try:
205-
fp.write(f" {self.palette[j]}")
206-
except IndexError:
207-
fp.write(" 0")
205+
fp.write(f" {self.palette[j] if j < palette_len else 0}")
208206
fp.write("\n")
209207
finally:
210208
if open_fp:

src/PIL/JpegImagePlugin.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ def APP(self: JpegImageFile, marker: int) -> None:
153153
photoshop[code] = data
154154
offset += size
155155
offset += offset & 1 # align
156-
except struct.error:
156+
except struct.error: # noqa: PERF203
157157
break # insufficient data
158158

159159
elif marker == 0xFFEE and s.startswith(b"Adobe"):
@@ -744,7 +744,7 @@ def validate_qtables(
744744
msg = "Invalid quantization table"
745745
raise TypeError(msg)
746746
table_array = array.array("H", table)
747-
except TypeError as e:
747+
except TypeError as e: # noqa: PERF203
748748
msg = "Invalid quantization table"
749749
raise ValueError(msg) from e
750750
else:

src/PIL/PcfFontFile.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ def _load_encoding(self) -> list[int | None]:
251251
]
252252
if encoding_offset != 0xFFFF:
253253
encoding[i] = encoding_offset
254-
except UnicodeDecodeError:
254+
except UnicodeDecodeError: # noqa: PERF203
255255
# character is not supported in selected encoding
256256
pass
257257

0 commit comments

Comments
 (0)