Skip to content

Commit 6ed952b

Browse files
committed
Fix: PYI024 Use typing.NamedTuple instead of collections.namedtuple
1 parent 5d37d02 commit 6ed952b

3 files changed

Lines changed: 42 additions & 27 deletions

File tree

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
}

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
26+
name: str
27+
type: int
28+
length: int
29+
enum: dict[int, str]
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):

0 commit comments

Comments
 (0)