From 126bc44e9eb21a08f4655bee821969ca82053552 Mon Sep 17 00:00:00 2001 From: Aarni Koskela Date: Mon, 6 Nov 2023 20:41:21 +0200 Subject: [PATCH 1/2] Fix up most noqas Update Tests/bench_cffi_access.py Co-authored-by: Alexander Karpinsky --- Tests/bench_cffi_access.py | 2 +- src/PIL/EpsImagePlugin.py | 13 ++++++------- src/PIL/IcnsImagePlugin.py | 4 ++-- src/PIL/Image.py | 3 ++- src/PIL/PdfParser.py | 4 ++-- 5 files changed, 13 insertions(+), 13 deletions(-) diff --git a/Tests/bench_cffi_access.py b/Tests/bench_cffi_access.py index d94b1985b..36ce63296 100644 --- a/Tests/bench_cffi_access.py +++ b/Tests/bench_cffi_access.py @@ -45,7 +45,7 @@ def test_direct(): assert caccess[(0, 0)] == access[(0, 0)] - print("Size: %sx%s" % im.size) # noqa: UP031 + print(f"Size: {im.width}x{im.height}") timer(iterate_get, "PyAccess - get", im.size, access) timer(iterate_set, "PyAccess - set", im.size, access) timer(iterate_get, "C-api - get", im.size, caccess) diff --git a/src/PIL/EpsImagePlugin.py b/src/PIL/EpsImagePlugin.py index 9f2914501..c05208c80 100644 --- a/src/PIL/EpsImagePlugin.py +++ b/src/PIL/EpsImagePlugin.py @@ -77,12 +77,11 @@ def Ghostscript(tile, size, fp, scale=1, transparency=False): # Hack to support hi-res rendering scale = int(scale) or 1 - size = (size[0] * scale, size[1] * scale) + width = size[0] * scale + height = size[1] * scale # resolution is dependent on bbox and size - res = ( - 72.0 * size[0] / (bbox[2] - bbox[0]), - 72.0 * size[1] / (bbox[3] - bbox[1]), - ) + res_x = 72.0 * width / (bbox[2] - bbox[0]) + res_y = 72.0 * height / (bbox[3] - bbox[1]) out_fd, outfile = tempfile.mkstemp() os.close(out_fd) @@ -119,8 +118,8 @@ def Ghostscript(tile, size, fp, scale=1, transparency=False): command = [ gs_binary, "-q", # quiet mode - "-g%dx%d" % size, # set output geometry (pixels) - "-r%fx%f" % res, # set input DPI (dots per inch) # noqa: UP031 + f"-g{width:d}x{height:d}", # set output geometry (pixels) + f"-r{res_x:f}x{res_y:f}", # set input DPI (dots per inch) "-dBATCH", # exit after processing "-dNOPAUSE", # don't pause between pages "-dSAFER", # safe mode diff --git a/src/PIL/IcnsImagePlugin.py b/src/PIL/IcnsImagePlugin.py index 5226c986d..61f56b216 100644 --- a/src/PIL/IcnsImagePlugin.py +++ b/src/PIL/IcnsImagePlugin.py @@ -391,8 +391,8 @@ if __name__ == "__main__": with open(sys.argv[1], "rb") as fp: imf = IcnsImageFile(fp) for size in imf.info["sizes"]: - imf.size = size - imf.save("out-%s-%s-%s.png" % size) # noqa: UP031 + w, h, x = imf.size = size + imf.save(f"out-{w}-{h}-{x}.png") with Image.open(sys.argv[1]) as im: im.save("out.png") if sys.platform == "windows": diff --git a/src/PIL/Image.py b/src/PIL/Image.py index 930ca060b..55bd7ea07 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -3100,7 +3100,8 @@ def fromarray(obj, mode=None): try: mode, rawmode = _fromarray_typemap[typekey] except KeyError as e: - msg = "Cannot handle this data type: %s, %s" % typekey # noqa: UP031 + typ_shape, typ_type = typekey + msg = f"Cannot handle this data type: {typ_shape}, {typ_type}" raise TypeError(msg) from e else: rawmode = mode diff --git a/src/PIL/PdfParser.py b/src/PIL/PdfParser.py index 07df577ae..8bdb65cce 100644 --- a/src/PIL/PdfParser.py +++ b/src/PIL/PdfParser.py @@ -82,7 +82,7 @@ class IndirectReference( collections.namedtuple("IndirectReferenceTuple", ["object_id", "generation"]) ): def __str__(self): - return "%s %s R" % self # noqa: UP031 + return f"{self.object_id} {self.generation} R" def __bytes__(self): return self.__str__().encode("us-ascii") @@ -103,7 +103,7 @@ class IndirectReference( class IndirectObjectDef(IndirectReference): def __str__(self): - return "%s %s obj" % self # noqa: UP031 + return f"{self.object_id} {self.generation} obj" class XrefTable: From f47633a24aa5729050d377e8146c83092808d762 Mon Sep 17 00:00:00 2001 From: Aarni Koskela Date: Mon, 13 Nov 2023 09:28:01 +0200 Subject: [PATCH 2/2] Apply suggestions from code review Co-authored-by: Andrew Murray <3112309+radarhere@users.noreply.github.com> --- src/PIL/IcnsImagePlugin.py | 4 ++-- src/PIL/Image.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/PIL/IcnsImagePlugin.py b/src/PIL/IcnsImagePlugin.py index 61f56b216..b415a3219 100644 --- a/src/PIL/IcnsImagePlugin.py +++ b/src/PIL/IcnsImagePlugin.py @@ -391,8 +391,8 @@ if __name__ == "__main__": with open(sys.argv[1], "rb") as fp: imf = IcnsImageFile(fp) for size in imf.info["sizes"]: - w, h, x = imf.size = size - imf.save(f"out-{w}-{h}-{x}.png") + width, height, scale = imf.size = size + imf.save(f"out-{width}-{height}-{scale}.png") with Image.open(sys.argv[1]) as im: im.save("out.png") if sys.platform == "windows": diff --git a/src/PIL/Image.py b/src/PIL/Image.py index 55bd7ea07..546d90203 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -3100,8 +3100,8 @@ def fromarray(obj, mode=None): try: mode, rawmode = _fromarray_typemap[typekey] except KeyError as e: - typ_shape, typ_type = typekey - msg = f"Cannot handle this data type: {typ_shape}, {typ_type}" + typekey_shape, typestr = typekey + msg = f"Cannot handle this data type: {typekey_shape}, {typestr}" raise TypeError(msg) from e else: rawmode = mode