Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed string formatting to f-strings #8043

Merged
merged 3 commits into from
May 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/PIL/DdsImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,16 +271,16 @@
module = sys.modules[__name__]
for item in DDSD:
assert item.name is not None
setattr(module, "DDSD_" + item.name, item.value)
setattr(module, f"DDSD_{item.name}", item.value)

Check warning on line 274 in src/PIL/DdsImagePlugin.py

View check run for this annotation

Codecov / codecov/patch

src/PIL/DdsImagePlugin.py#L274

Added line #L274 was not covered by tests
for item1 in DDSCAPS:
assert item1.name is not None
setattr(module, "DDSCAPS_" + item1.name, item1.value)
setattr(module, f"DDSCAPS_{item1.name}", item1.value)

Check warning on line 277 in src/PIL/DdsImagePlugin.py

View check run for this annotation

Codecov / codecov/patch

src/PIL/DdsImagePlugin.py#L277

Added line #L277 was not covered by tests
for item2 in DDSCAPS2:
assert item2.name is not None
setattr(module, "DDSCAPS2_" + item2.name, item2.value)
setattr(module, f"DDSCAPS2_{item2.name}", item2.value)

Check warning on line 280 in src/PIL/DdsImagePlugin.py

View check run for this annotation

Codecov / codecov/patch

src/PIL/DdsImagePlugin.py#L280

Added line #L280 was not covered by tests
for item3 in DDPF:
assert item3.name is not None
setattr(module, "DDPF_" + item3.name, item3.value)
setattr(module, f"DDPF_{item3.name}", item3.value)

Check warning on line 283 in src/PIL/DdsImagePlugin.py

View check run for this annotation

Codecov / codecov/patch

src/PIL/DdsImagePlugin.py#L283

Added line #L283 was not covered by tests

DDS_FOURCC = DDPF.FOURCC
DDS_RGB = DDPF.RGB
Expand Down
2 changes: 1 addition & 1 deletion src/PIL/ImImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@
n += 1

else:
msg = "Syntax error in IM header: " + s.decode("ascii", "replace")
msg = f"Syntax error in IM header: {s.decode('ascii', 'replace')}"

Check warning on line 199 in src/PIL/ImImagePlugin.py

View check run for this annotation

Codecov / codecov/patch

src/PIL/ImImagePlugin.py#L199

Added line #L199 was not covered by tests
raise SyntaxError(msg)

if not n:
Expand Down
14 changes: 7 additions & 7 deletions src/PIL/Image.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@

try:
# get decoder
decoder = getattr(core, decoder_name + "_decoder")
decoder = getattr(core, f"{decoder_name}_decoder")

Check warning on line 408 in src/PIL/Image.py

View check run for this annotation

Codecov / codecov/patch

src/PIL/Image.py#L408

Added line #L408 was not covered by tests
except AttributeError as e:
msg = f"decoder {decoder_name} not available"
raise OSError(msg) from e
Expand All @@ -428,7 +428,7 @@

try:
# get encoder
encoder = getattr(core, encoder_name + "_encoder")
encoder = getattr(core, f"{encoder_name}_encoder")

Check warning on line 431 in src/PIL/Image.py

View check run for this annotation

Codecov / codecov/patch

src/PIL/Image.py#L431

Added line #L431 was not covered by tests
except AttributeError as e:
msg = f"encoder {encoder_name} not available"
raise OSError(msg) from e
Expand Down Expand Up @@ -603,7 +603,7 @@
) -> str:
suffix = ""
if format:
suffix = "." + format
suffix = f".{format}"

Check warning on line 606 in src/PIL/Image.py

View check run for this annotation

Codecov / codecov/patch

src/PIL/Image.py#L606

Added line #L606 was not covered by tests

if not file:
f, filename = tempfile.mkstemp(suffix)
Expand Down Expand Up @@ -2180,7 +2180,7 @@
(Resampling.HAMMING, "Image.Resampling.HAMMING"),
)
]
msg += " Use " + ", ".join(filters[:-1]) + " or " + filters[-1]
msg += f" Use {', '.join(filters[:-1])} or {filters[-1]}"

Check warning on line 2183 in src/PIL/Image.py

View check run for this annotation

Codecov / codecov/patch

src/PIL/Image.py#L2183

Added line #L2183 was not covered by tests
raise ValueError(msg)

if reducing_gap is not None and reducing_gap < 1.0:
Expand Down Expand Up @@ -2825,7 +2825,7 @@
(Resampling.BICUBIC, "Image.Resampling.BICUBIC"),
)
]
msg += " Use " + ", ".join(filters[:-1]) + " or " + filters[-1]
msg += f" Use {', '.join(filters[:-1])} or {filters[-1]}"

Check warning on line 2828 in src/PIL/Image.py

View check run for this annotation

Codecov / codecov/patch

src/PIL/Image.py#L2828

Added line #L2828 was not covered by tests
raise ValueError(msg)

image.load()
Expand Down Expand Up @@ -3223,8 +3223,8 @@
((1, 1, 3), "|u1"): ("RGB", "RGB"),
((1, 1, 4), "|u1"): ("RGBA", "RGBA"),
# shortcuts:
((1, 1), _ENDIAN + "i4"): ("I", "I"),
((1, 1), _ENDIAN + "f4"): ("F", "F"),
((1, 1), f"{_ENDIAN}i4"): ("I", "I"),
((1, 1), f"{_ENDIAN}f4"): ("F", "F"),
}


Expand Down
4 changes: 2 additions & 2 deletions src/PIL/ImageMath.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
out = Image.new(mode or im_1.mode, im_1.size, None)
im_1.load()
try:
op = getattr(_imagingmath, op + "_" + im_1.mode)
op = getattr(_imagingmath, f"{op}_{im_1.mode}")

Check warning on line 64 in src/PIL/ImageMath.py

View check run for this annotation

Codecov / codecov/patch

src/PIL/ImageMath.py#L64

Added line #L64 was not covered by tests
except AttributeError as e:
msg = f"bad operand type for '{op}'"
raise TypeError(msg) from e
Expand Down Expand Up @@ -89,7 +89,7 @@
im_1.load()
im_2.load()
try:
op = getattr(_imagingmath, op + "_" + im_1.mode)
op = getattr(_imagingmath, f"{op}_{im_1.mode}")

Check warning on line 92 in src/PIL/ImageMath.py

View check run for this annotation

Codecov / codecov/patch

src/PIL/ImageMath.py#L92

Added line #L92 was not covered by tests
except AttributeError as e:
msg = f"bad operand type for '{op}'"
raise TypeError(msg) from e
Expand Down
8 changes: 4 additions & 4 deletions src/PIL/ImageMode.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ def getmode(mode: str) -> ModeDescriptor:
# Bits need to be extended to bytes
"1": ("L", "L", ("1",), "|b1"),
"L": ("L", "L", ("L",), "|u1"),
"I": ("L", "I", ("I",), endian + "i4"),
"F": ("L", "F", ("F",), endian + "f4"),
"I": ("L", "I", ("I",), f"{endian}i4"),
"F": ("L", "F", ("F",), f"{endian}f4"),
"P": ("P", "L", ("P",), "|u1"),
"RGB": ("RGB", "L", ("R", "G", "B"), "|u1"),
"RGBX": ("RGB", "L", ("R", "G", "B", "X"), "|u1"),
Expand Down Expand Up @@ -78,8 +78,8 @@ def getmode(mode: str) -> ModeDescriptor:
"I;16LS": "<i2",
"I;16B": ">u2",
"I;16BS": ">i2",
"I;16N": endian + "u2",
"I;16NS": endian + "i2",
"I;16N": f"{endian}u2",
"I;16NS": f"{endian}i2",
"I;32": "<u4",
"I;32B": ">u4",
"I;32L": "<u4",
Expand Down
2 changes: 1 addition & 1 deletion src/PIL/ImageMorph.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
],
}
if op_name not in known_patterns:
msg = "Unknown pattern " + op_name + "!"
msg = f"Unknown pattern {op_name}!"

Check warning on line 87 in src/PIL/ImageMorph.py

View check run for this annotation

Codecov / codecov/patch

src/PIL/ImageMorph.py#L87

Added line #L87 was not covered by tests
raise Exception(msg)

self.patterns = known_patterns[op_name]
Expand Down
2 changes: 1 addition & 1 deletion src/PIL/ImageWin.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@
)

def __dispatcher(self, action, *args):
return getattr(self, "ui_handle_" + action)(*args)
return getattr(self, f"ui_handle_{action}")(*args)

Check warning on line 199 in src/PIL/ImageWin.py

View check run for this annotation

Codecov / codecov/patch

src/PIL/ImageWin.py#L199

Added line #L199 was not covered by tests

def ui_handle_clear(self, dc, x0, y0, x1, y1):
pass
Expand Down
2 changes: 1 addition & 1 deletion src/PIL/PalmImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@

# we ignore the palette here
im.mode = "P"
rawmode = "P;" + str(bpp)
rawmode = f"P;{bpp}"

Check warning on line 145 in src/PIL/PalmImagePlugin.py

View check run for this annotation

Codecov / codecov/patch

src/PIL/PalmImagePlugin.py#L145

Added line #L145 was not covered by tests
version = 1

elif im.mode == "1":
Expand Down
8 changes: 3 additions & 5 deletions src/PIL/PdfParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,7 @@
elif key in self.deleted_entries:
generation = self.deleted_entries[key]
else:
msg = (
"object ID " + str(key) + " cannot be deleted because it doesn't exist"
)
msg = f"object ID {key} cannot be deleted because it doesn't exist"

Check warning on line 147 in src/PIL/PdfParser.py

View check run for this annotation

Codecov / codecov/patch

src/PIL/PdfParser.py#L147

Added line #L147 was not covered by tests
raise IndexError(msg)

def __contains__(self, key):
Expand Down Expand Up @@ -225,7 +223,7 @@
return hash(self.name)

def __repr__(self):
return f"PdfName({repr(self.name)})"
return f"{self.__class__.__name__}({repr(self.name)})"

Check warning on line 226 in src/PIL/PdfParser.py

View check run for this annotation

Codecov / codecov/patch

src/PIL/PdfParser.py#L226

Added line #L226 was not covered by tests

@classmethod
def from_pdf_stream(cls, data):
Expand Down Expand Up @@ -884,7 +882,7 @@
if m:
return cls.get_literal_string(data, m.end())
# return None, offset # fallback (only for debugging)
msg = "unrecognized object: " + repr(data[offset : offset + 32])
msg = f"unrecognized object: {repr(data[offset : offset + 32])}"

Check warning on line 885 in src/PIL/PdfParser.py

View check run for this annotation

Codecov / codecov/patch

src/PIL/PdfParser.py#L885

Added line #L885 was not covered by tests
raise PdfFormatError(msg)

re_lit_str_token = re.compile(
Expand Down
2 changes: 1 addition & 1 deletion src/PIL/PngImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@
"""Call the appropriate chunk handler"""

logger.debug("STREAM %r %s %s", cid, pos, length)
return getattr(self, "chunk_" + cid.decode("ascii"))(pos, length)
return getattr(self, f"chunk_{cid.decode('ascii')}")(pos, length)

Check warning on line 192 in src/PIL/PngImagePlugin.py

View check run for this annotation

Codecov / codecov/patch

src/PIL/PngImagePlugin.py#L192

Added line #L192 was not covered by tests

def crc(self, cid, data):
"""Read and verify checksum"""
Expand Down
10 changes: 5 additions & 5 deletions src/PIL/SpiderImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@
im = im.convert2byte()
except Exception:
if not isSpiderImage(img):
print(img + " is not a Spider image file")
print(f"{img} is not a Spider image file")

Check warning on line 221 in src/PIL/SpiderImagePlugin.py

View check run for this annotation

Codecov / codecov/patch

src/PIL/SpiderImagePlugin.py#L221

Added line #L221 was not covered by tests
continue
im.info["filename"] = img
imglist.append(im)
Expand Down Expand Up @@ -299,10 +299,10 @@
sys.exit()

with Image.open(filename) as im:
print("image: " + str(im))
print("format: " + str(im.format))
print("size: " + str(im.size))
print("mode: " + str(im.mode))
print(f"image: {im}")
print(f"format: {im.format}")
print(f"size: {im.size}")
print(f"mode: {im.mode}")
print("max, min: ", end=" ")
print(im.getextrema())

Expand Down
12 changes: 6 additions & 6 deletions src/PIL/TiffImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@

idx, fmt, name = idx_fmt_name
TYPES[idx] = name
size = struct.calcsize("=" + fmt)
size = struct.calcsize(f"={fmt}")

Check warning on line 472 in src/PIL/TiffImagePlugin.py

View check run for this annotation

Codecov / codecov/patch

src/PIL/TiffImagePlugin.py#L472

Added line #L472 was not covered by tests
_load_dispatch[idx] = ( # noqa: F821
size,
lambda self, data, legacy_api=True: (
Expand Down Expand Up @@ -987,8 +987,8 @@
ImageFileDirectory_v2._write_dispatch = _write_dispatch
for idx, name in TYPES.items():
name = name.replace(" ", "_")
setattr(ImageFileDirectory_v2, "load_" + name, _load_dispatch[idx][1])
setattr(ImageFileDirectory_v2, "write_" + name, _write_dispatch[idx])
setattr(ImageFileDirectory_v2, f"load_{name}", _load_dispatch[idx][1])
setattr(ImageFileDirectory_v2, f"write_{name}", _write_dispatch[idx])

Check warning on line 991 in src/PIL/TiffImagePlugin.py

View check run for this annotation

Codecov / codecov/patch

src/PIL/TiffImagePlugin.py#L990-L991

Added lines #L990 - L991 were not covered by tests
del _load_dispatch, _write_dispatch, idx, name


Expand Down Expand Up @@ -2025,9 +2025,9 @@

def setEndian(self, endian):
self.endian = endian
self.longFmt = self.endian + "L"
self.shortFmt = self.endian + "H"
self.tagFormat = self.endian + "HHL"
self.longFmt = f"{self.endian}L"
self.shortFmt = f"{self.endian}H"
self.tagFormat = f"{self.endian}HHL"

Check warning on line 2030 in src/PIL/TiffImagePlugin.py

View check run for this annotation

Codecov / codecov/patch

src/PIL/TiffImagePlugin.py#L2028-L2030

Added lines #L2028 - L2030 were not covered by tests

def skipIFDs(self):
while True:
Expand Down
4 changes: 2 additions & 2 deletions src/PIL/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@

codec, lib = codecs[feature]

return codec + "_encoder" in dir(Image.core)
return f"{codec}_encoder" in dir(Image.core)

Check warning on line 92 in src/PIL/features.py

View check run for this annotation

Codecov / codecov/patch

src/PIL/features.py#L92

Added line #L92 was not covered by tests


def version_codec(feature):
Expand All @@ -105,7 +105,7 @@

codec, lib = codecs[feature]

version = getattr(Image.core, lib + "_version")
version = getattr(Image.core, f"{lib}_version")

Check warning on line 108 in src/PIL/features.py

View check run for this annotation

Codecov / codecov/patch

src/PIL/features.py#L108

Added line #L108 was not covered by tests

if feature == "libtiff":
return version.split("\n")[0].split("Version ")[1]
Expand Down
Loading