From 69baeccf2ee7850ccfb9b2b05ab584b87ad50fe1 Mon Sep 17 00:00:00 2001 From: Mark Mayo Date: Thu, 13 Oct 2022 15:20:11 +1300 Subject: [PATCH 1/3] some pylint warnings Fixed some pylint issues --- Tests/helper.py | 13 ++++++------- Tests/test_file_jpeg.py | 2 +- Tests/test_file_libtiff.py | 2 +- Tests/test_file_png.py | 2 +- Tests/test_file_ppm.py | 8 ++++---- Tests/test_file_tiff.py | 2 +- Tests/test_image.py | 7 +++---- Tests/test_image_access.py | 5 ++--- Tests/test_imagemath.py | 7 +++---- setup.py | 2 +- 10 files changed, 23 insertions(+), 27 deletions(-) diff --git a/Tests/helper.py b/Tests/helper.py index 13c6955e41d..0d1d03ac8c1 100644 --- a/Tests/helper.py +++ b/Tests/helper.py @@ -208,12 +208,11 @@ def _get_mem_usage(self): # ru_maxrss # This is the maximum resident set size utilized (in bytes). return mem / 1024 # Kb - else: - # linux - # man 2 getrusage - # ru_maxrss (since Linux 2.6.32) - # This is the maximum resident set size used (in kilobytes). - return mem # Kb + # linux + # man 2 getrusage + # ru_maxrss (since Linux 2.6.32) + # This is the maximum resident set size used (in kilobytes). + return mem # Kb def _test_leak(self, core): start_mem = self._get_mem_usage() @@ -285,7 +284,7 @@ def magick_command(): if imagemagick and shutil.which(imagemagick[0]): return imagemagick - elif graphicsmagick and shutil.which(graphicsmagick[0]): + if graphicsmagick and shutil.which(graphicsmagick[0]): return graphicsmagick diff --git a/Tests/test_file_jpeg.py b/Tests/test_file_jpeg.py index adbb72aa576..fa96e425b8c 100644 --- a/Tests/test_file_jpeg.py +++ b/Tests/test_file_jpeg.py @@ -30,7 +30,7 @@ ) try: - import defusedxml.ElementTree as ElementTree + from defusedxml import ElementTree except ImportError: ElementTree = None diff --git a/Tests/test_file_libtiff.py b/Tests/test_file_libtiff.py index d9066c58912..58d3aac0683 100644 --- a/Tests/test_file_libtiff.py +++ b/Tests/test_file_libtiff.py @@ -934,7 +934,7 @@ def test_block_tile_tags(self, compression, tmp_path): im.save(out, exif=tags, compression=compression) with Image.open(out) as reloaded: - for tag in tags.keys(): + for tag in tags: assert tag not in reloaded.getexif() def test_old_style_jpeg(self): diff --git a/Tests/test_file_png.py b/Tests/test_file_png.py index 1af0223eb7d..37235fe6f02 100644 --- a/Tests/test_file_png.py +++ b/Tests/test_file_png.py @@ -20,7 +20,7 @@ ) try: - import defusedxml.ElementTree as ElementTree + from defusedxml import ElementTree except ImportError: ElementTree = None diff --git a/Tests/test_file_ppm.py b/Tests/test_file_ppm.py index 5c6376caf66..72711de777c 100644 --- a/Tests/test_file_ppm.py +++ b/Tests/test_file_ppm.py @@ -240,7 +240,7 @@ def test_header_token_too_long(tmp_path): def test_truncated_file(tmp_path): # Test EOF in header path = str(tmp_path / "temp.pgm") - with open(path, "w") as f: + with open(path, "w", encoding='utf-8') as f: f.write("P6") with pytest.raises(ValueError) as e: @@ -259,7 +259,7 @@ def test_truncated_file(tmp_path): @pytest.mark.parametrize("maxval", (0, 65536)) def test_invalid_maxval(maxval, tmp_path): path = str(tmp_path / "temp.ppm") - with open(path, "w") as f: + with open(path, "w", encoding='utf-8') as f: f.write("P6\n3 1 " + str(maxval)) with pytest.raises(ValueError) as e: @@ -283,12 +283,12 @@ def test_neg_ppm(): def test_mimetypes(tmp_path): path = str(tmp_path / "temp.pgm") - with open(path, "w") as f: + with open(path, "w", encoding='utf-8') as f: f.write("P4\n128 128\n255") with Image.open(path) as im: assert im.get_format_mimetype() == "image/x-portable-bitmap" - with open(path, "w") as f: + with open(path, "w", encoding='utf-8') as f: f.write("PyCMYK\n128 128\n255") with Image.open(path) as im: assert im.get_format_mimetype() == "image/x-portable-anymap" diff --git a/Tests/test_file_tiff.py b/Tests/test_file_tiff.py index 1a5ba594f3f..a32e6a00515 100644 --- a/Tests/test_file_tiff.py +++ b/Tests/test_file_tiff.py @@ -18,7 +18,7 @@ ) try: - import defusedxml.ElementTree as ElementTree + from defusedxml import ElementTree except ImportError: ElementTree = None diff --git a/Tests/test_image.py b/Tests/test_image.py index ab945e946f0..d847d06174f 100644 --- a/Tests/test_image.py +++ b/Tests/test_image.py @@ -129,7 +129,6 @@ def test_width_height(self): im.size = (3, 4) def test_invalid_image(self): - import io im = io.BytesIO(b"") with pytest.raises(UnidentifiedImageError): @@ -699,15 +698,15 @@ def act(fp): def test_empty_exif(self): with Image.open("Tests/images/exif.png") as im: exif = im.getexif() - assert dict(exif) != {} + assert dict(exif) # Test that exif data is cleared after another load exif.load(None) - assert dict(exif) == {} + assert not dict(exif) # Test loading just the EXIF header exif.load(b"Exif\x00\x00") - assert dict(exif) == {} + assert not dict(exif) @mark_if_feature_version( pytest.mark.valgrind_known_error, "libjpeg_turbo", "2.0", reason="Known Failing" diff --git a/Tests/test_image_access.py b/Tests/test_image_access.py index a000cb64c70..5a6d1c75492 100644 --- a/Tests/test_image_access.py +++ b/Tests/test_image_access.py @@ -131,8 +131,7 @@ def color(mode): bands = Image.getmodebands(mode) if bands == 1: return 1 - else: - return tuple(range(1, bands + 1)) + return tuple(range(1, bands + 1)) def check(self, mode, c=None): if not c: @@ -415,7 +414,7 @@ class TestEmbeddable: def test_embeddable(self): import ctypes - with open("embed_pil.c", "w") as fh: + with open("embed_pil.c", "w", encoding='utf-8') as fh: fh.write( """ #include "Python.h" diff --git a/Tests/test_imagemath.py b/Tests/test_imagemath.py index 39d91eadea6..a5414a050f9 100644 --- a/Tests/test_imagemath.py +++ b/Tests/test_imagemath.py @@ -6,10 +6,9 @@ def pixel(im): if hasattr(im, "im"): return f"{im.mode} {repr(im.getpixel((0, 0)))}" - else: - if isinstance(im, int): - return int(im) # hack to deal with booleans - print(im) + if isinstance(im, int): + return int(im) # hack to deal with booleans + print(im) A = Image.new("L", (1, 1), 1) diff --git a/setup.py b/setup.py index aa3168aa5be..14c40475227 100755 --- a/setup.py +++ b/setup.py @@ -23,7 +23,7 @@ def get_version(): version_file = "src/PIL/_version.py" - with open(version_file) as f: + with open(version_file, encoding="utf-8") as f: exec(compile(f.read(), version_file, "exec")) return locals()["__version__"] From c93413835d46d0090b4c0e519d5bc0beef7ae90c Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 13 Oct 2022 02:22:49 +0000 Subject: [PATCH 2/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- Tests/test_file_ppm.py | 8 ++++---- Tests/test_image_access.py | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Tests/test_file_ppm.py b/Tests/test_file_ppm.py index 72711de777c..471da9a797b 100644 --- a/Tests/test_file_ppm.py +++ b/Tests/test_file_ppm.py @@ -240,7 +240,7 @@ def test_header_token_too_long(tmp_path): def test_truncated_file(tmp_path): # Test EOF in header path = str(tmp_path / "temp.pgm") - with open(path, "w", encoding='utf-8') as f: + with open(path, "w", encoding="utf-8") as f: f.write("P6") with pytest.raises(ValueError) as e: @@ -259,7 +259,7 @@ def test_truncated_file(tmp_path): @pytest.mark.parametrize("maxval", (0, 65536)) def test_invalid_maxval(maxval, tmp_path): path = str(tmp_path / "temp.ppm") - with open(path, "w", encoding='utf-8') as f: + with open(path, "w", encoding="utf-8") as f: f.write("P6\n3 1 " + str(maxval)) with pytest.raises(ValueError) as e: @@ -283,12 +283,12 @@ def test_neg_ppm(): def test_mimetypes(tmp_path): path = str(tmp_path / "temp.pgm") - with open(path, "w", encoding='utf-8') as f: + with open(path, "w", encoding="utf-8") as f: f.write("P4\n128 128\n255") with Image.open(path) as im: assert im.get_format_mimetype() == "image/x-portable-bitmap" - with open(path, "w", encoding='utf-8') as f: + with open(path, "w", encoding="utf-8") as f: f.write("PyCMYK\n128 128\n255") with Image.open(path) as im: assert im.get_format_mimetype() == "image/x-portable-anymap" diff --git a/Tests/test_image_access.py b/Tests/test_image_access.py index 5a6d1c75492..955740b952a 100644 --- a/Tests/test_image_access.py +++ b/Tests/test_image_access.py @@ -414,7 +414,7 @@ class TestEmbeddable: def test_embeddable(self): import ctypes - with open("embed_pil.c", "w", encoding='utf-8') as fh: + with open("embed_pil.c", "w", encoding="utf-8") as fh: fh.write( """ #include "Python.h" From ddede39932c65a2e618f6e71306f41036a1b1713 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sun, 23 Oct 2022 14:24:30 +1100 Subject: [PATCH 3/3] Removed blank line --- Tests/test_image.py | 1 - 1 file changed, 1 deletion(-) diff --git a/Tests/test_image.py b/Tests/test_image.py index d847d06174f..e579034904d 100644 --- a/Tests/test_image.py +++ b/Tests/test_image.py @@ -129,7 +129,6 @@ def test_width_height(self): im.size = (3, 4) def test_invalid_image(self): - im = io.BytesIO(b"") with pytest.raises(UnidentifiedImageError): with Image.open(im):