Skip to content

Commit 6ca8bfb

Browse files
authored
Merge pull request #7897 from hugovk/flake8-pyi
Type hints: Add PYI (flake8-pyi) to Ruff and fix errors
2 parents 58c4c75 + 73bf044 commit 6ca8bfb

11 files changed

Lines changed: 46 additions & 41 deletions

Tests/test_file_libtiff.py

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
import os
77
import re
88
import sys
9-
from collections import namedtuple
109
from pathlib import Path
10+
from typing import Any, NamedTuple
1111

1212
import pytest
1313

@@ -243,36 +243,40 @@ def test_additional_metadata(self, tmp_path: Path) -> None:
243243
TiffImagePlugin.WRITE_LIBTIFF = False
244244

245245
def test_custom_metadata(self, tmp_path: Path) -> None:
246-
tc = namedtuple("tc", "value,type,supported_by_default")
246+
class Tc(NamedTuple):
247+
value: Any
248+
type: int
249+
supported_by_default: bool
250+
247251
custom = {
248252
37000 + k: v
249253
for k, v in enumerate(
250254
[
251-
tc(4, TiffTags.SHORT, True),
252-
tc(123456789, TiffTags.LONG, True),
253-
tc(-4, TiffTags.SIGNED_BYTE, False),
254-
tc(-4, TiffTags.SIGNED_SHORT, False),
255-
tc(-123456789, TiffTags.SIGNED_LONG, False),
256-
tc(TiffImagePlugin.IFDRational(4, 7), TiffTags.RATIONAL, True),
257-
tc(4.25, TiffTags.FLOAT, True),
258-
tc(4.25, TiffTags.DOUBLE, True),
259-
tc("custom tag value", TiffTags.ASCII, True),
260-
tc(b"custom tag value", TiffTags.BYTE, True),
261-
tc((4, 5, 6), TiffTags.SHORT, True),
262-
tc((123456789, 9, 34, 234, 219387, 92432323), TiffTags.LONG, True),
263-
tc((-4, 9, 10), TiffTags.SIGNED_BYTE, False),
264-
tc((-4, 5, 6), TiffTags.SIGNED_SHORT, False),
265-
tc(
255+
Tc(4, TiffTags.SHORT, True),
256+
Tc(123456789, TiffTags.LONG, True),
257+
Tc(-4, TiffTags.SIGNED_BYTE, False),
258+
Tc(-4, TiffTags.SIGNED_SHORT, False),
259+
Tc(-123456789, TiffTags.SIGNED_LONG, False),
260+
Tc(TiffImagePlugin.IFDRational(4, 7), TiffTags.RATIONAL, True),
261+
Tc(4.25, TiffTags.FLOAT, True),
262+
Tc(4.25, TiffTags.DOUBLE, True),
263+
Tc("custom tag value", TiffTags.ASCII, True),
264+
Tc(b"custom tag value", TiffTags.BYTE, True),
265+
Tc((4, 5, 6), TiffTags.SHORT, True),
266+
Tc((123456789, 9, 34, 234, 219387, 92432323), TiffTags.LONG, True),
267+
Tc((-4, 9, 10), TiffTags.SIGNED_BYTE, False),
268+
Tc((-4, 5, 6), TiffTags.SIGNED_SHORT, False),
269+
Tc(
266270
(-123456789, 9, 34, 234, 219387, -92432323),
267271
TiffTags.SIGNED_LONG,
268272
False,
269273
),
270-
tc((4.25, 5.25), TiffTags.FLOAT, True),
271-
tc((4.25, 5.25), TiffTags.DOUBLE, True),
274+
Tc((4.25, 5.25), TiffTags.FLOAT, True),
275+
Tc((4.25, 5.25), TiffTags.DOUBLE, True),
272276
# array of TIFF_BYTE requires bytes instead of tuple for backwards
273277
# compatibility
274-
tc(bytes([4]), TiffTags.BYTE, True),
275-
tc(bytes((4, 9, 10)), TiffTags.BYTE, True),
278+
Tc(bytes([4]), TiffTags.BYTE, True),
279+
Tc(bytes((4, 9, 10)), TiffTags.BYTE, True),
276280
]
277281
)
278282
}

Tests/test_lib_pack.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def assert_pack(
1515
mode: str,
1616
rawmode: str,
1717
data: int | bytes,
18-
*pixels: int | float | tuple[int, ...],
18+
*pixels: float | tuple[int, ...],
1919
) -> None:
2020
"""
2121
data - either raw bytes with data or just number of bytes in rawmode.
@@ -239,7 +239,7 @@ def assert_unpack(
239239
mode: str,
240240
rawmode: str,
241241
data: int | bytes,
242-
*pixels: int | float | tuple[int, ...],
242+
*pixels: float | tuple[int, ...],
243243
) -> None:
244244
"""
245245
data - either raw bytes with data or just number of bytes in rawmode.

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ select = [
106106
"ISC", # flake8-implicit-str-concat
107107
"LOG", # flake8-logging
108108
"PGH", # pygrep-hooks
109+
"PYI", # flake8-pyi
109110
"RUF100", # unused noqa (yesqa)
110111
"UP", # pyupgrade
111112
"W", # pycodestyle warnings
@@ -116,6 +117,7 @@ ignore = [
116117
"E221", # Multiple spaces before operator
117118
"E226", # Missing whitespace around arithmetic operator
118119
"E241", # Multiple spaces after ','
120+
"PYI034", # flake8-pyi: typing.Self added in Python 3.11
119121
]
120122

121123
[tool.ruff.lint.per-file-ignores]

src/PIL/PdfParser.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import re
99
import time
1010
import zlib
11-
from typing import TYPE_CHECKING, Any, List, Union
11+
from typing import TYPE_CHECKING, Any, List, NamedTuple, Union
1212

1313

1414
# see 7.9.2.2 Text String Type on page 86 and D.3 PDFDocEncoding Character Set
@@ -81,9 +81,12 @@ def check_format_condition(condition, error_message):
8181
raise PdfFormatError(error_message)
8282

8383

84-
class IndirectReference(
85-
collections.namedtuple("IndirectReferenceTuple", ["object_id", "generation"])
86-
):
84+
class IndirectReferenceTuple(NamedTuple):
85+
object_id: int
86+
generation: int
87+
88+
89+
class IndirectReference(IndirectReferenceTuple):
8790
def __str__(self):
8891
return f"{self.object_id} {self.generation} R"
8992

src/PIL/TiffTags.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,18 @@
1818
##
1919
from __future__ import annotations
2020

21-
from collections import namedtuple
21+
from typing import NamedTuple
2222

2323

24-
class TagInfo(namedtuple("_TagInfo", "value name type length enum")):
24+
class _TagInfo(NamedTuple):
25+
value: int | None
26+
name: str
27+
type: int | None
28+
length: int | None
29+
enum: dict[str, int]
30+
31+
32+
class TagInfo(_TagInfo):
2533
__slots__: list[str] = []
2634

2735
def __new__(cls, value=None, name="unknown", type=None, length=None, enum=None):

src/PIL/_imaging.pyi

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from __future__ import annotations
2-
31
from typing import Any
42

53
def __getattr__(name: str) -> Any: ...

src/PIL/_imagingcms.pyi

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from __future__ import annotations
2-
31
from typing import Any
42

53
def __getattr__(name: str) -> Any: ...

src/PIL/_imagingft.pyi

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from __future__ import annotations
2-
31
from typing import Any
42

53
def __getattr__(name: str) -> Any: ...

src/PIL/_imagingmath.pyi

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from __future__ import annotations
2-
31
from typing import Any
42

53
def __getattr__(name: str) -> Any: ...

src/PIL/_imagingmorph.pyi

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from __future__ import annotations
2-
31
from typing import Any
42

53
def __getattr__(name: str) -> Any: ...

0 commit comments

Comments
 (0)