diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 74764752e80..7e1bedf5883 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -4,11 +4,7 @@ repos: hooks: - id: ruff args: [--exit-non-zero-on-fix] - - - repo: https://github.com/psf/black-pre-commit-mirror - rev: 24.4.2 - hooks: - - id: black + - id: ruff-format - repo: https://github.com/PyCQA/bandit rev: 1.7.9 diff --git a/Makefile b/Makefile index 94f7565d826..fa1caa140b3 100644 --- a/Makefile +++ b/Makefile @@ -114,10 +114,9 @@ lint: .PHONY: lint-fix lint-fix: - python3 -c "import black" > /dev/null 2>&1 || python3 -m pip install black - python3 -m black . python3 -c "import ruff" > /dev/null 2>&1 || python3 -m pip install ruff - python3 -m ruff --fix . + python3 -m ruff check --fix . + python3 -m ruff format . .PHONY: mypy mypy: diff --git a/Tests/check_jpeg_leaks.py b/Tests/check_jpeg_leaks.py index 5f290c6cd37..73cf1e86af9 100644 --- a/Tests/check_jpeg_leaks.py +++ b/Tests/check_jpeg_leaks.py @@ -76,9 +76,8 @@ """ - +# fmt: off standard_l_qtable = ( - # fmt: off 16, 11, 10, 16, 24, 40, 51, 61, 12, 12, 14, 19, 26, 58, 60, 55, 14, 13, 16, 24, 40, 57, 69, 56, @@ -87,11 +86,9 @@ 24, 35, 55, 64, 81, 104, 113, 92, 49, 64, 78, 87, 103, 121, 120, 101, 72, 92, 95, 98, 112, 100, 103, 99, - # fmt: on ) standard_chrominance_qtable = ( - # fmt: off 17, 18, 24, 47, 99, 99, 99, 99, 18, 21, 26, 66, 99, 99, 99, 99, 24, 26, 56, 99, 99, 99, 99, 99, @@ -100,8 +97,8 @@ 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, - # fmt: on ) +# fmt: on @pytest.mark.parametrize( diff --git a/Tests/helper.py b/Tests/helper.py index d6a93a8030b..ddc4f75c035 100644 --- a/Tests/helper.py +++ b/Tests/helper.py @@ -122,9 +122,8 @@ def assert_image_similar( ave_diff = diff / (a.size[0] * a.size[1]) try: assert epsilon >= ave_diff, ( - (msg or "") - + f" average pixel value difference {ave_diff:.4f} > epsilon {epsilon:.4f}" - ) + msg or "" + ) + f" average pixel value difference {ave_diff:.4f} > epsilon {epsilon:.4f}" except Exception as e: try: url = upload(a, b) diff --git a/Tests/test_box_blur.py b/Tests/test_box_blur.py index 1f6ed61277a..ef042c0725e 100644 --- a/Tests/test_box_blur.py +++ b/Tests/test_box_blur.py @@ -72,138 +72,138 @@ def test_color_modes() -> None: def test_radius_0() -> None: + # fmt: off assert_blur( sample, 0, [ - # fmt: off [210, 50, 20, 10, 220, 230, 80], [190, 210, 20, 180, 170, 40, 110], [120, 210, 250, 60, 220, 0, 220], [220, 40, 230, 80, 130, 250, 40], [250, 0, 80, 30, 60, 20, 110], - # fmt: on ], ) + # fmt: on def test_radius_0_02() -> None: + # fmt: off assert_blur( sample, 0.02, [ - # fmt: off [206, 55, 20, 17, 215, 223, 83], [189, 203, 31, 171, 169, 46, 110], [125, 206, 241, 69, 210, 13, 210], [215, 49, 221, 82, 131, 235, 48], [244, 7, 80, 32, 60, 27, 107], - # fmt: on ], delta=2, ) + # fmt: on def test_radius_0_05() -> None: + # fmt: off assert_blur( sample, 0.05, [ - # fmt: off [202, 62, 22, 27, 209, 215, 88], [188, 194, 44, 161, 168, 56, 111], [131, 201, 229, 81, 198, 31, 198], [209, 62, 209, 86, 133, 216, 59], [237, 17, 80, 36, 60, 35, 103], - # fmt: on ], delta=2, ) + # fmt: on def test_radius_0_1() -> None: + # fmt: off assert_blur( sample, 0.1, [ - # fmt: off [196, 72, 24, 40, 200, 203, 93], [187, 183, 62, 148, 166, 68, 111], [139, 193, 213, 96, 182, 54, 182], [201, 78, 193, 91, 133, 191, 73], [227, 31, 80, 42, 61, 47, 99], - # fmt: on ], delta=1, ) + # fmt: on def test_radius_0_5() -> None: + # fmt: off assert_blur( sample, 0.5, [ - # fmt: off [176, 101, 46, 83, 163, 165, 111], [176, 149, 108, 122, 144, 120, 117], [164, 171, 159, 141, 134, 119, 129], [170, 136, 133, 114, 116, 124, 109], [184, 95, 72, 70, 69, 81, 89], - # fmt: on ], delta=1, ) + # fmt: on def test_radius_1() -> None: + # fmt: off assert_blur( sample, 1, [ - # fmt: off [170, 109, 63, 97, 146, 153, 116], [168, 142, 112, 128, 126, 143, 121], [169, 166, 142, 149, 126, 131, 114], [159, 156, 109, 127, 94, 117, 112], [164, 128, 63, 87, 76, 89, 90], - # fmt: on ], delta=1, ) + # fmt: on def test_radius_1_5() -> None: + # fmt: off assert_blur( sample, 1.5, [ - # fmt: off [155, 120, 105, 112, 124, 137, 130], [160, 136, 124, 125, 127, 134, 130], [166, 147, 130, 125, 120, 121, 119], [168, 145, 119, 109, 103, 105, 110], [168, 134, 96, 85, 85, 89, 97], - # fmt: on ], delta=1, ) + # fmt: on def test_radius_bigger_then_half() -> None: + # fmt: off assert_blur( sample, 3, [ - # fmt: off [144, 145, 142, 128, 114, 115, 117], [148, 145, 137, 122, 109, 111, 112], [152, 145, 131, 117, 103, 107, 108], [156, 144, 126, 111, 97, 102, 103], [160, 144, 121, 106, 92, 98, 99], - # fmt: on ], delta=1, ) + # fmt: on def test_radius_bigger_then_width() -> None: @@ -237,36 +237,36 @@ def test_extreme_large_radius() -> None: def test_two_passes() -> None: + # fmt: off assert_blur( sample, 1, [ - # fmt: off [153, 123, 102, 109, 132, 135, 129], [159, 138, 123, 121, 133, 131, 126], [162, 147, 136, 124, 127, 121, 121], [159, 140, 125, 108, 111, 106, 108], [154, 126, 105, 87, 94, 93, 97], - # fmt: on ], passes=2, delta=1, ) + # fmt: on def test_three_passes() -> None: + # fmt: off assert_blur( sample, 1, [ - # fmt: off [146, 131, 116, 118, 126, 131, 130], [151, 138, 125, 123, 126, 128, 127], [154, 143, 129, 123, 120, 120, 119], [152, 139, 122, 113, 108, 108, 108], [148, 132, 112, 102, 97, 99, 100], - # fmt: on ], passes=3, delta=1, ) + # fmt: on diff --git a/Tests/test_file_jpeg.py b/Tests/test_file_jpeg.py index df589e24a1b..ac469a92f13 100644 --- a/Tests/test_file_jpeg.py +++ b/Tests/test_file_jpeg.py @@ -577,9 +577,7 @@ def _n_qtables_helper(n: int, test_file: str) -> None: 24 35 55 64 81 104 113 92 49 64 78 87 103 121 120 101 72 92 95 98 112 100 103 99 - """.split( - None - ) + """.split(None) ] standard_chrominance_qtable = [ @@ -593,9 +591,7 @@ def _n_qtables_helper(n: int, test_file: str) -> None: 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 - """.split( - None - ) + """.split(None) ] # list of qtable lists assert_image_similar( @@ -921,7 +917,7 @@ def test_icc_after_SOF(self) -> None: def test_jpeg_magic_number(self, monkeypatch: pytest.MonkeyPatch) -> None: size = 4097 - buffer = BytesIO(b"\xFF" * size) # Many xFF bytes + buffer = BytesIO(b"\xff" * size) # Many FF bytes max_pos = 0 orig_read = buffer.read diff --git a/Tests/test_file_libtiff.py b/Tests/test_file_libtiff.py index 62f8719af53..0fa669c69f7 100644 --- a/Tests/test_file_libtiff.py +++ b/Tests/test_file_libtiff.py @@ -313,7 +313,7 @@ class Tc(NamedTuple): } def check_tags( - tiffinfo: TiffImagePlugin.ImageFileDirectory_v2 | dict[int, str] + tiffinfo: TiffImagePlugin.ImageFileDirectory_v2 | dict[int, str], ) -> None: im = hopper() @@ -626,7 +626,6 @@ def test_4bit(self, monkeypatch: pytest.MonkeyPatch) -> None: # Act monkeypatch.setattr(TiffImagePlugin, "READ_LIBTIFF", True) with Image.open(test_file) as im: - # Assert assert im.size == (128, 128) assert im.mode == "L" diff --git a/Tests/test_file_pdf.py b/Tests/test_file_pdf.py index 1d5001b1a9b..815686a5249 100644 --- a/Tests/test_file_pdf.py +++ b/Tests/test_file_pdf.py @@ -264,7 +264,7 @@ def test_pdf_append(tmp_path: Path) -> None: # append some info pdf.info.Title = "abc" pdf.info.Author = "def" - pdf.info.Subject = "ghi\uABCD" + pdf.info.Subject = "ghi\uabcd" pdf.info.Keywords = "qw)e\\r(ty" pdf.info.Creator = "hopper()" pdf.start_writing() @@ -292,7 +292,7 @@ def test_pdf_append(tmp_path: Path) -> None: assert pdf.info.Title == "abc" assert pdf.info.Producer == "PdfParser" assert pdf.info.Keywords == "qw)e\\r(ty" - assert pdf.info.Subject == "ghi\uABCD" + assert pdf.info.Subject == "ghi\uabcd" assert b"CreationDate" in pdf.info assert b"ModDate" in pdf.info check_pdf_pages_consistency(pdf) diff --git a/Tests/test_file_ppm.py b/Tests/test_file_ppm.py index 0a61830a40a..38fbd67a4cd 100644 --- a/Tests/test_file_ppm.py +++ b/Tests/test_file_ppm.py @@ -49,7 +49,7 @@ def test_sanity() -> None: (b"P5 3 1 257 \x00\x00\x00\x80\x01\x01", "I", (0, 32640, 65535)), # P6 with maxval < 255 ( - b"P6 3 1 17 \x00\x01\x02\x08\x09\x0A\x0F\x10\x11", + b"P6 3 1 17 \x00\x01\x02\x08\x09\x0a\x0f\x10\x11", "RGB", ( (0, 15, 30), @@ -60,7 +60,7 @@ def test_sanity() -> None: # P6 with maxval > 255 ( b"P6 3 1 257 \x00\x00\x00\x01\x00\x02" - b"\x00\x80\x00\x81\x00\x82\x01\x00\x01\x01\xFF\xFF", + b"\x00\x80\x00\x81\x00\x82\x01\x00\x01\x01\xff\xff", "RGB", ( (0, 1, 2), diff --git a/Tests/test_font_leaks.py b/Tests/test_font_leaks.py index ab8a7f9ecca..6cebc59421b 100644 --- a/Tests/test_font_leaks.py +++ b/Tests/test_font_leaks.py @@ -17,7 +17,10 @@ def _test_font(self, font: ImageFont.FreeTypeFont | ImageFont.ImageFont) -> None draw = ImageDraw.ImageDraw(im) self._test_leak( lambda: draw.text( - (0, 0), "some text " * 1024, font=font, fill="black" # ~10k + (0, 0), + "some text " * 1024, # ~10k + font=font, + fill="black", ) ) diff --git a/Tests/test_image_filter.py b/Tests/test_image_filter.py index 412ab44c3c1..bd526255df7 100644 --- a/Tests/test_image_filter.py +++ b/Tests/test_image_filter.py @@ -149,15 +149,15 @@ def test_kernel_not_enough_coefficients() -> None: def test_consistency_3x3(mode: str) -> None: with Image.open("Tests/images/hopper.bmp") as source: with Image.open("Tests/images/hopper_emboss.bmp") as reference: + # fmt: off kernel = ImageFilter.Kernel( (3, 3), - # fmt: off (-1, -1, 0, -1, 0, 1, 0, 1, 1), - # fmt: on 0.3, ) + # fmt: on assert_image_equal(source.filter(kernel), reference) @@ -165,17 +165,17 @@ def test_consistency_3x3(mode: str) -> None: def test_consistency_5x5(mode: str) -> None: with Image.open("Tests/images/hopper.bmp") as source: with Image.open("Tests/images/hopper_emboss_more.bmp") as reference: + # fmt: off kernel = ImageFilter.Kernel( (5, 5), - # fmt: off (-1, -1, -1, -1, 0, -1, -1, -1, 0, 1, -1, -1, 0, 1, 1, -1, 0, 1, 1, 1, 0, 1, 1, 1, 1), - # fmt: on 0.3, ) + # fmt: on assert_image_equal(source.filter(kernel), reference) diff --git a/Tests/test_imagedraw.py b/Tests/test_imagedraw.py index e397978cbbf..2a3484d486c 100644 --- a/Tests/test_imagedraw.py +++ b/Tests/test_imagedraw.py @@ -812,7 +812,7 @@ def test_rounded_rectangle( tuple[int, int, int, int] | tuple[list[int]] | tuple[tuple[int, int], tuple[int, int]] - ) + ), ) -> None: # Arrange im = Image.new("RGB", (200, 200)) diff --git a/Tests/test_imagefont.py b/Tests/test_imagefont.py index 340cc47420b..05db14ed2b0 100644 --- a/Tests/test_imagefont.py +++ b/Tests/test_imagefont.py @@ -516,7 +516,7 @@ def test_render_empty(font: ImageFont.FreeTypeFont) -> None: def test_unicode_extended(layout_engine: ImageFont.Layout) -> None: # issue #3777 - text = "A\u278A\U0001F12B" + text = "A\u278a\U0001f12b" target = "Tests/images/unicode_extended.png" ttf = ImageFont.truetype( @@ -985,7 +985,7 @@ def test_sbix(layout_engine: ImageFont.Layout) -> None: im = Image.new("RGB", (400, 400), "white") d = ImageDraw.Draw(im) - d.text((50, 50), "\uE901", font=font, embedded_color=True) + d.text((50, 50), "\ue901", font=font, embedded_color=True) assert_image_similar_tofile(im, "Tests/images/chromacheck-sbix.png", 1) except OSError as e: # pragma: no cover @@ -1002,7 +1002,7 @@ def test_sbix_mask(layout_engine: ImageFont.Layout) -> None: im = Image.new("RGB", (400, 400), "white") d = ImageDraw.Draw(im) - d.text((50, 50), "\uE901", (100, 0, 0), font=font) + d.text((50, 50), "\ue901", (100, 0, 0), font=font) assert_image_similar_tofile(im, "Tests/images/chromacheck-sbix_mask.png", 1) except OSError as e: # pragma: no cover diff --git a/Tests/test_imagefontctl.py b/Tests/test_imagefontctl.py index 24c7b871a68..c85eb499cd0 100644 --- a/Tests/test_imagefontctl.py +++ b/Tests/test_imagefontctl.py @@ -229,7 +229,7 @@ def test_getlength( @pytest.mark.parametrize("direction", ("ltr", "ttb")) @pytest.mark.parametrize( "text", - ("i" + ("\u030C" * 15) + "i", "i" + "\u032C" * 15 + "i", "\u035Cii", "i\u0305i"), + ("i" + ("\u030c" * 15) + "i", "i" + "\u032c" * 15 + "i", "\u035cii", "i\u0305i"), ids=("caron-above", "caron-below", "double-breve", "overline"), ) def test_getlength_combine(mode: str, direction: str, text: str) -> None: @@ -272,27 +272,27 @@ def test_anchor_ttb(anchor: str) -> None: combine_tests = ( # extends above (e.g. issue #4553) - ("caron", "a\u030C\u030C\u030C\u030C\u030Cb", None, None, 0.08), - ("caron_la", "a\u030C\u030C\u030C\u030C\u030Cb", "la", None, 0.08), - ("caron_lt", "a\u030C\u030C\u030C\u030C\u030Cb", "lt", None, 0.08), - ("caron_ls", "a\u030C\u030C\u030C\u030C\u030Cb", "ls", None, 0.08), - ("caron_ttb", "ca" + ("\u030C" * 15) + "b", None, "ttb", 0.3), - ("caron_ttb_lt", "ca" + ("\u030C" * 15) + "b", "lt", "ttb", 0.3), + ("caron", "a\u030c\u030c\u030c\u030c\u030cb", None, None, 0.08), + ("caron_la", "a\u030c\u030c\u030c\u030c\u030cb", "la", None, 0.08), + ("caron_lt", "a\u030c\u030c\u030c\u030c\u030cb", "lt", None, 0.08), + ("caron_ls", "a\u030c\u030c\u030c\u030c\u030cb", "ls", None, 0.08), + ("caron_ttb", "ca" + ("\u030c" * 15) + "b", None, "ttb", 0.3), + ("caron_ttb_lt", "ca" + ("\u030c" * 15) + "b", "lt", "ttb", 0.3), # extends below - ("caron_below", "a\u032C\u032C\u032C\u032C\u032Cb", None, None, 0.02), - ("caron_below_ld", "a\u032C\u032C\u032C\u032C\u032Cb", "ld", None, 0.02), - ("caron_below_lb", "a\u032C\u032C\u032C\u032C\u032Cb", "lb", None, 0.02), - ("caron_below_ls", "a\u032C\u032C\u032C\u032C\u032Cb", "ls", None, 0.02), - ("caron_below_ttb", "a" + ("\u032C" * 15) + "b", None, "ttb", 0.03), - ("caron_below_ttb_lb", "a" + ("\u032C" * 15) + "b", "lb", "ttb", 0.03), + ("caron_below", "a\u032c\u032c\u032c\u032c\u032cb", None, None, 0.02), + ("caron_below_ld", "a\u032c\u032c\u032c\u032c\u032cb", "ld", None, 0.02), + ("caron_below_lb", "a\u032c\u032c\u032c\u032c\u032cb", "lb", None, 0.02), + ("caron_below_ls", "a\u032c\u032c\u032c\u032c\u032cb", "ls", None, 0.02), + ("caron_below_ttb", "a" + ("\u032c" * 15) + "b", None, "ttb", 0.03), + ("caron_below_ttb_lb", "a" + ("\u032c" * 15) + "b", "lb", "ttb", 0.03), # extends to the right (e.g. issue #3745) - ("double_breve_below", "a\u035Ci", None, None, 0.02), - ("double_breve_below_ma", "a\u035Ci", "ma", None, 0.02), - ("double_breve_below_ra", "a\u035Ci", "ra", None, 0.02), - ("double_breve_below_ttb", "a\u035Cb", None, "ttb", 0.02), - ("double_breve_below_ttb_rt", "a\u035Cb", "rt", "ttb", 0.02), - ("double_breve_below_ttb_mt", "a\u035Cb", "mt", "ttb", 0.02), - ("double_breve_below_ttb_st", "a\u035Cb", "st", "ttb", 0.02), + ("double_breve_below", "a\u035ci", None, None, 0.02), + ("double_breve_below_ma", "a\u035ci", "ma", None, 0.02), + ("double_breve_below_ra", "a\u035ci", "ra", None, 0.02), + ("double_breve_below_ttb", "a\u035cb", None, "ttb", 0.02), + ("double_breve_below_ttb_rt", "a\u035cb", "rt", "ttb", 0.02), + ("double_breve_below_ttb_mt", "a\u035cb", "mt", "ttb", 0.02), + ("double_breve_below_ttb_st", "a\u035cb", "st", "ttb", 0.02), # extends to the left (fail=0.064) ("overline", "i\u0305", None, None, 0.02), ("overline_la", "i\u0305", "la", None, 0.02), @@ -346,7 +346,7 @@ def test_combine_multiline(anchor: str, align: str) -> None: path = f"Tests/images/test_combine_multiline_{anchor}_{align}.png" f = ImageFont.truetype("Tests/fonts/NotoSans-Regular.ttf", 48) - text = "i\u0305\u035C\ntext" # i with overline and double breve, and a word + text = "i\u0305\u035c\ntext" # i with overline and double breve, and a word im = Image.new("RGB", (400, 400), "white") d = ImageDraw.Draw(im) diff --git a/Tests/test_imagepalette.py b/Tests/test_imagepalette.py index 6cf0079dda7..6d0e6f36ff3 100644 --- a/Tests/test_imagepalette.py +++ b/Tests/test_imagepalette.py @@ -189,7 +189,7 @@ def test_2bit_palette(tmp_path: Path) -> None: rgb = b"\x00" * 2 + b"\x01" * 2 + b"\x02" * 2 img = Image.frombytes("P", (6, 1), rgb) - img.putpalette(b"\xFF\x00\x00\x00\xFF\x00\x00\x00\xFF") # RGB + img.putpalette(b"\xff\x00\x00\x00\xff\x00\x00\x00\xff") # RGB img.save(outfile, format="PNG") assert_image_equal_tofile(img, outfile) diff --git a/Tests/test_imagepath.py b/Tests/test_imagepath.py index cda2584e77b..c5de8a36079 100644 --- a/Tests/test_imagepath.py +++ b/Tests/test_imagepath.py @@ -79,7 +79,7 @@ def test_path_constructors( ), ) def test_invalid_path_constructors( - coords: tuple[str, str] | Sequence[Sequence[int]] + coords: tuple[str, str] | Sequence[Sequence[int]], ) -> None: # Act with pytest.raises(ValueError) as e: diff --git a/Tests/test_pdfparser.py b/Tests/test_pdfparser.py index f6b12cb2047..d85fb1212e3 100644 --- a/Tests/test_pdfparser.py +++ b/Tests/test_pdfparser.py @@ -20,10 +20,10 @@ def test_text_encode_decode() -> None: - assert encode_text("abc") == b"\xFE\xFF\x00a\x00b\x00c" - assert decode_text(b"\xFE\xFF\x00a\x00b\x00c") == "abc" + assert encode_text("abc") == b"\xfe\xff\x00a\x00b\x00c" + assert decode_text(b"\xfe\xff\x00a\x00b\x00c") == "abc" assert decode_text(b"abc") == "abc" - assert decode_text(b"\x1B a \x1C") == "\u02D9 a \u02DD" + assert decode_text(b"\x1b a \x1c") == "\u02d9 a \u02dd" def test_indirect_refs() -> None: @@ -45,8 +45,8 @@ def test_parsing() -> None: assert PdfParser.get_value(b"false%", 0) == (False, 5) assert PdfParser.get_value(b"null<", 0) == (None, 4) assert PdfParser.get_value(b"%cmt\n %cmt\n 123\n", 0) == (123, 15) - assert PdfParser.get_value(b"<901FA3>", 0) == (b"\x90\x1F\xA3", 8) - assert PdfParser.get_value(b"asd < 9 0 1 f A > qwe", 3) == (b"\x90\x1F\xA0", 17) + assert PdfParser.get_value(b"<901FA3>", 0) == (b"\x90\x1f\xa3", 8) + assert PdfParser.get_value(b"asd < 9 0 1 f A > qwe", 3) == (b"\x90\x1f\xa0", 17) assert PdfParser.get_value(b"(asd)", 0) == (b"asd", 5) assert PdfParser.get_value(b"(asd(qwe)zxc)zzz(aaa)", 0) == (b"asd(qwe)zxc", 13) assert PdfParser.get_value(b"(Two \\\nwords.)", 0) == (b"Two words.", 14) @@ -56,9 +56,9 @@ def test_parsing() -> None: assert PdfParser.get_value(b"(One\\(paren).", 0) == (b"One(paren", 12) assert PdfParser.get_value(b"(One\\)paren).", 0) == (b"One)paren", 12) assert PdfParser.get_value(b"(\\0053)", 0) == (b"\x053", 7) - assert PdfParser.get_value(b"(\\053)", 0) == (b"\x2B", 6) - assert PdfParser.get_value(b"(\\53)", 0) == (b"\x2B", 5) - assert PdfParser.get_value(b"(\\53a)", 0) == (b"\x2Ba", 6) + assert PdfParser.get_value(b"(\\053)", 0) == (b"\x2b", 6) + assert PdfParser.get_value(b"(\\53)", 0) == (b"\x2b", 5) + assert PdfParser.get_value(b"(\\53a)", 0) == (b"\x2ba", 6) assert PdfParser.get_value(b"(\\1111)", 0) == (b"\x491", 7) assert PdfParser.get_value(b" 123 (", 0) == (123, 4) assert round(abs(PdfParser.get_value(b" 123.4 %", 0)[0] - 123.4), 7) == 0 @@ -118,7 +118,7 @@ def test_pdf_repr() -> None: assert pdf_repr(None) == b"null" assert pdf_repr(b"a)/b\\(c") == rb"(a\)/b\\\(c)" assert pdf_repr([123, True, {"a": PdfName(b"b")}]) == b"[ 123 true <<\n/a /b\n>> ]" - assert pdf_repr(PdfBinary(b"\x90\x1F\xA0")) == b"<901FA0>" + assert pdf_repr(PdfBinary(b"\x90\x1f\xa0")) == b"<901FA0>" def test_duplicate_xref_entry() -> None: diff --git a/setup.py b/setup.py index b26852b0b07..40ad775e6e0 100644 --- a/setup.py +++ b/setup.py @@ -56,7 +56,6 @@ def get_version(): ) ) - _IMAGING = ("decode", "encode", "map", "display", "outline", "path") _LIB_IMAGING = ( @@ -431,11 +430,11 @@ def get_macos_sdk_path(self): ) except Exception: sdk_path = None - if ( - not sdk_path - or sdk_path == "/Applications/Xcode.app/Contents/Developer" + xcode_sdk_path = ( + "/Applications/Xcode.app/Contents/Developer" "/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk" - ): + ) + if not sdk_path or sdk_path == xcode_sdk_path: commandlinetools_sdk_path = ( "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk" ) diff --git a/src/PIL/BdfFontFile.py b/src/PIL/BdfFontFile.py index bc1416c74c6..b4a3fd17e72 100644 --- a/src/PIL/BdfFontFile.py +++ b/src/PIL/BdfFontFile.py @@ -20,6 +20,7 @@ """ Parse X Bitmap Distribution Format (BDF) """ + from __future__ import annotations from typing import BinaryIO diff --git a/src/PIL/ExifTags.py b/src/PIL/ExifTags.py index 39b4aa55262..a90e8f43083 100644 --- a/src/PIL/ExifTags.py +++ b/src/PIL/ExifTags.py @@ -13,6 +13,7 @@ This module provides constants and clear-text names for various well-known EXIF tags. """ + from __future__ import annotations from enum import IntEnum diff --git a/src/PIL/FontFile.py b/src/PIL/FontFile.py index 1e0c1c166b5..3ec1ae819fc 100644 --- a/src/PIL/FontFile.py +++ b/src/PIL/FontFile.py @@ -50,7 +50,9 @@ def __init__(self) -> None: | None ] = [None] * 256 - def __getitem__(self, ix: int) -> ( + def __getitem__( + self, ix: int + ) -> ( tuple[ tuple[int, int], tuple[int, int, int, int], diff --git a/src/PIL/GdImageFile.py b/src/PIL/GdImageFile.py index 88b87a22cd6..243688bc5c1 100644 --- a/src/PIL/GdImageFile.py +++ b/src/PIL/GdImageFile.py @@ -25,6 +25,7 @@ class is not registered for use with :py:func:`PIL.Image.open()`. To open a implementation is provided for convenience and demonstrational purposes only. """ + from __future__ import annotations from typing import IO diff --git a/src/PIL/GimpGradientFile.py b/src/PIL/GimpGradientFile.py index 220eac57e38..f7d676d3ac8 100644 --- a/src/PIL/GimpGradientFile.py +++ b/src/PIL/GimpGradientFile.py @@ -18,6 +18,7 @@ the corresponding code in GIMP, written by Federico Mena Quintero. See the GIMP distribution for more information.) """ + from __future__ import annotations from math import log, pi, sin, sqrt diff --git a/src/PIL/ImImagePlugin.py b/src/PIL/ImImagePlugin.py index 2fb7ecd52a0..c90e028c860 100644 --- a/src/PIL/ImImagePlugin.py +++ b/src/PIL/ImImagePlugin.py @@ -145,7 +145,7 @@ def _open(self) -> None: if s == b"\r": continue - if not s or s == b"\0" or s == b"\x1A": + if not s or s == b"\0" or s == b"\x1a": break # FIXME: this may read whole file if not a text file @@ -209,7 +209,7 @@ def _open(self) -> None: self._mode = self.info[MODE] # Skip forward to start of image data - while s and s[:1] != b"\x1A": + while s and s[:1] != b"\x1a": s = self.fp.read(1) if not s: msg = "File truncated" diff --git a/src/PIL/Image.py b/src/PIL/Image.py index ebf4f46c435..4c491e3f86f 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -3908,7 +3908,7 @@ def _get_ifd_dict( return self._fixup_dict(dict(info)) def _get_head(self) -> bytes: - version = b"\x2B" if self.bigtiff else b"\x2A" + version = b"\x2b" if self.bigtiff else b"\x2a" if self.endian == "<": head = b"II" + version + b"\x00" + o32le(8) else: diff --git a/src/PIL/ImageDraw2.py b/src/PIL/ImageDraw2.py index 3d68658ed5b..3f7036e83d3 100644 --- a/src/PIL/ImageDraw2.py +++ b/src/PIL/ImageDraw2.py @@ -22,6 +22,7 @@ .. seealso:: :py:mod:`PIL.ImageDraw` """ + from __future__ import annotations from typing import Any, AnyStr, BinaryIO diff --git a/src/PIL/ImtImagePlugin.py b/src/PIL/ImtImagePlugin.py index abb3fb762e7..7ea9f027213 100644 --- a/src/PIL/ImtImagePlugin.py +++ b/src/PIL/ImtImagePlugin.py @@ -55,7 +55,7 @@ def _open(self) -> None: if not s: break - if s == b"\x0C": + if s == b"\x0c": # image data begins self.tile = [ ( diff --git a/src/PIL/JpegImagePlugin.py b/src/PIL/JpegImagePlugin.py index fc897e2b919..5876d19dc50 100644 --- a/src/PIL/JpegImagePlugin.py +++ b/src/PIL/JpegImagePlugin.py @@ -322,7 +322,7 @@ def DQT(self: JpegImageFile, marker: int) -> None: def _accept(prefix: bytes) -> bool: # Magic number was taken from https://en.wikipedia.org/wiki/JPEG - return prefix[:3] == b"\xFF\xD8\xFF" + return prefix[:3] == b"\xff\xd8\xff" ## @@ -339,7 +339,7 @@ def _open(self) -> None: if not _accept(s): msg = "not a JPEG file" raise SyntaxError(msg) - s = b"\xFF" + s = b"\xff" # Create attributes self.bits = self.layers = 0 @@ -405,7 +405,7 @@ def load_read(self, read_bytes: int) -> bytes: # Premature EOF. # Pretend file is finished adding EOI marker self._ended = True - return b"\xFF\xD9" + return b"\xff\xd9" return s @@ -698,7 +698,7 @@ def _save(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None: def validate_qtables( qtables: ( str | tuple[list[int], ...] | list[list[int]] | dict[int, list[int]] | None - ) + ), ) -> list[list[int]] | None: if qtables is None: return qtables @@ -759,7 +759,7 @@ def validate_qtables( for marker in markers: size = o16(2 + ICC_OVERHEAD_LEN + len(marker)) extra += ( - b"\xFF\xE2" + b"\xff\xe2" + size + b"ICC_PROFILE\0" + o8(i) diff --git a/src/PIL/MpoImagePlugin.py b/src/PIL/MpoImagePlugin.py index 5ed9f56a164..3890cf9434f 100644 --- a/src/PIL/MpoImagePlugin.py +++ b/src/PIL/MpoImagePlugin.py @@ -50,7 +50,7 @@ def _save_all(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None: if not offsets: # APP2 marker im_frame.encoderinfo["extra"] = ( - b"\xFF\xE2" + struct.pack(">H", 6 + 82) + b"MPF\0" + b" " * 82 + b"\xff\xe2" + struct.pack(">H", 6 + 82) + b"MPF\0" + b" " * 82 ) exif = im_frame.encoderinfo.get("exif") if isinstance(exif, Image.Exif): @@ -83,7 +83,7 @@ def _save_all(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None: ifd[0xB002] = mpentries fp.seek(mpf_offset) - fp.write(b"II\x2A\x00" + o32le(8) + ifd.tobytes(8)) + fp.write(b"II\x2a\x00" + o32le(8) + ifd.tobytes(8)) fp.seek(0, os.SEEK_END) diff --git a/src/PIL/PcxImagePlugin.py b/src/PIL/PcxImagePlugin.py index dd42003b5a3..a2ae8624d28 100644 --- a/src/PIL/PcxImagePlugin.py +++ b/src/PIL/PcxImagePlugin.py @@ -186,7 +186,7 @@ def _save(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None: + o16(dpi[0]) + o16(dpi[1]) + b"\0" * 24 - + b"\xFF" * 24 + + b"\xff" * 24 + b"\0" + o8(planes) + o16(stride) diff --git a/src/PIL/PdfParser.py b/src/PIL/PdfParser.py index 7cb2d241bc2..bd44917d025 100644 --- a/src/PIL/PdfParser.py +++ b/src/PIL/PdfParser.py @@ -19,14 +19,14 @@ def encode_text(s: str) -> bytes: PDFDocEncoding = { 0x16: "\u0017", - 0x18: "\u02D8", - 0x19: "\u02C7", - 0x1A: "\u02C6", - 0x1B: "\u02D9", - 0x1C: "\u02DD", - 0x1D: "\u02DB", - 0x1E: "\u02DA", - 0x1F: "\u02DC", + 0x18: "\u02d8", + 0x19: "\u02c7", + 0x1A: "\u02c6", + 0x1B: "\u02d9", + 0x1C: "\u02dd", + 0x1D: "\u02db", + 0x1E: "\u02da", + 0x1F: "\u02dc", 0x80: "\u2022", 0x81: "\u2020", 0x82: "\u2021", @@ -36,29 +36,29 @@ def encode_text(s: str) -> bytes: 0x86: "\u0192", 0x87: "\u2044", 0x88: "\u2039", - 0x89: "\u203A", + 0x89: "\u203a", 0x8A: "\u2212", 0x8B: "\u2030", - 0x8C: "\u201E", - 0x8D: "\u201C", - 0x8E: "\u201D", + 0x8C: "\u201e", + 0x8D: "\u201c", + 0x8E: "\u201d", 0x8F: "\u2018", 0x90: "\u2019", - 0x91: "\u201A", + 0x91: "\u201a", 0x92: "\u2122", - 0x93: "\uFB01", - 0x94: "\uFB02", + 0x93: "\ufb01", + 0x94: "\ufb02", 0x95: "\u0141", 0x96: "\u0152", 0x97: "\u0160", 0x98: "\u0178", - 0x99: "\u017D", + 0x99: "\u017d", 0x9A: "\u0131", 0x9B: "\u0142", 0x9C: "\u0153", 0x9D: "\u0161", - 0x9E: "\u017E", - 0xA0: "\u20AC", + 0x9E: "\u017e", + 0xA0: "\u20ac", } @@ -113,13 +113,15 @@ def __str__(self) -> str: class XrefTable: def __init__(self) -> None: - self.existing_entries: dict[int, tuple[int, int]] = ( - {} - ) # object ID => (offset, generation) - self.new_entries: dict[int, tuple[int, int]] = ( - {} - ) # object ID => (offset, generation) - self.deleted_entries = {0: 65536} # object ID => generation + # object ID => (offset, generation) + self.existing_entries: dict[int, tuple[int, int]] = {} + + # object ID => (offset, generation) + self.new_entries: dict[int, tuple[int, int]] = {} + + # object ID => generation + self.deleted_entries = {0: 65536} + self.reading_finished = False def __setitem__(self, key: int, value: tuple[int, int]) -> None: diff --git a/src/PIL/PngImagePlugin.py b/src/PIL/PngImagePlugin.py index 910fa9755cd..e8c9fcaf62f 100644 --- a/src/PIL/PngImagePlugin.py +++ b/src/PIL/PngImagePlugin.py @@ -1416,7 +1416,7 @@ def _save( chunk(fp, b"tRNS", transparency[:alpha_bytes]) else: transparency = max(0, min(255, transparency)) - alpha = b"\xFF" * transparency + b"\0" + alpha = b"\xff" * transparency + b"\0" chunk(fp, b"tRNS", alpha[:alpha_bytes]) elif im.mode in ("1", "L", "I", "I;16"): transparency = max(0, min(65535, transparency)) diff --git a/src/PIL/PpmImagePlugin.py b/src/PIL/PpmImagePlugin.py index 16c9ccbba72..d431c3071cd 100644 --- a/src/PIL/PpmImagePlugin.py +++ b/src/PIL/PpmImagePlugin.py @@ -228,7 +228,7 @@ def _decode_bitonal(self) -> bytearray: msg = b"Invalid token for this mode: %s" % bytes([token]) raise ValueError(msg) data = (data + tokens)[:total_bytes] - invert = bytes.maketrans(b"01", b"\xFF\x00") + invert = bytes.maketrans(b"01", b"\xff\x00") return data.translate(invert) def _decode_blocks(self, maxval: int) -> bytearray: diff --git a/src/PIL/TiffImagePlugin.py b/src/PIL/TiffImagePlugin.py index d9d1bab5ac8..83c8586e362 100644 --- a/src/PIL/TiffImagePlugin.py +++ b/src/PIL/TiffImagePlugin.py @@ -272,12 +272,12 @@ MAX_SAMPLESPERPIXEL = max(len(key_tp[4]) for key_tp in OPEN_INFO) PREFIXES = [ - b"MM\x00\x2A", # Valid TIFF header with big-endian byte order - b"II\x2A\x00", # Valid TIFF header with little-endian byte order - b"MM\x2A\x00", # Invalid TIFF header, assume big-endian - b"II\x00\x2A", # Invalid TIFF header, assume little-endian - b"MM\x00\x2B", # BigTIFF with big-endian byte order - b"II\x2B\x00", # BigTIFF with little-endian byte order + b"MM\x00\x2a", # Valid TIFF header with big-endian byte order + b"II\x2a\x00", # Valid TIFF header with little-endian byte order + b"MM\x2a\x00", # Invalid TIFF header, assume big-endian + b"II\x00\x2a", # Invalid TIFF header, assume little-endian + b"MM\x00\x2b", # BigTIFF with big-endian byte order + b"II\x2b\x00", # BigTIFF with little-endian byte order ] if not getattr(Image.core, "libtiff_support_custom_tags", True): @@ -933,9 +933,9 @@ def tobytes(self, offset: int = 0) -> bytes: is_ifd = typ == TiffTags.LONG and isinstance(value, dict) if is_ifd: if self._endian == "<": - ifh = b"II\x2A\x00\x08\x00\x00\x00" + ifh = b"II\x2a\x00\x08\x00\x00\x00" else: - ifh = b"MM\x00\x2A\x00\x00\x00\x08" + ifh = b"MM\x00\x2a\x00\x00\x00\x08" ifd = ImageFileDirectory_v2(ifh, group=tag) values = self._tags_v2[tag] for ifd_tag, ifd_value in values.items(): diff --git a/src/PIL/WalImageFile.py b/src/PIL/WalImageFile.py index ec5c7490001..1599bc74ffc 100644 --- a/src/PIL/WalImageFile.py +++ b/src/PIL/WalImageFile.py @@ -22,6 +22,7 @@ is not registered for use with :py:func:`PIL.Image.open()`. To open a WAL file, use the :py:func:`PIL.WalImageFile.open()` function instead. """ + from __future__ import annotations from typing import IO diff --git a/src/PIL/_binary.py b/src/PIL/_binary.py index 4594ccce361..d3236c17abb 100644 --- a/src/PIL/_binary.py +++ b/src/PIL/_binary.py @@ -13,6 +13,7 @@ """Binary input/output support routines.""" + from __future__ import annotations from struct import pack, unpack_from diff --git a/src/PIL/_tkinter_finder.py b/src/PIL/_tkinter_finder.py index beddfb0628a..9c0143003a7 100644 --- a/src/PIL/_tkinter_finder.py +++ b/src/PIL/_tkinter_finder.py @@ -1,5 +1,4 @@ -""" Find compiled module linking to Tcl / Tk libraries -""" +"""Find compiled module linking to Tcl / Tk libraries""" from __future__ import annotations