Skip to content

Commit

Permalink
Fix: PYI024 Use typing.NamedTuple instead of collections.namedtuple
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk committed Mar 23, 2024
1 parent 5d37d02 commit 6ed952b
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 27 deletions.
46 changes: 25 additions & 21 deletions Tests/test_file_libtiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import os
import re
import sys
from collections import namedtuple
from pathlib import Path
from typing import Any, NamedTuple

import pytest

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

def test_custom_metadata(self, tmp_path: Path) -> None:
tc = namedtuple("tc", "value,type,supported_by_default")
class Tc(NamedTuple):
value: Any
type: int
supported_by_default: bool

custom = {
37000 + k: v
for k, v in enumerate(
[
tc(4, TiffTags.SHORT, True),
tc(123456789, TiffTags.LONG, True),
tc(-4, TiffTags.SIGNED_BYTE, False),
tc(-4, TiffTags.SIGNED_SHORT, False),
tc(-123456789, TiffTags.SIGNED_LONG, False),
tc(TiffImagePlugin.IFDRational(4, 7), TiffTags.RATIONAL, True),
tc(4.25, TiffTags.FLOAT, True),
tc(4.25, TiffTags.DOUBLE, True),
tc("custom tag value", TiffTags.ASCII, True),
tc(b"custom tag value", TiffTags.BYTE, True),
tc((4, 5, 6), TiffTags.SHORT, True),
tc((123456789, 9, 34, 234, 219387, 92432323), TiffTags.LONG, True),
tc((-4, 9, 10), TiffTags.SIGNED_BYTE, False),
tc((-4, 5, 6), TiffTags.SIGNED_SHORT, False),
tc(
Tc(4, TiffTags.SHORT, True),
Tc(123456789, TiffTags.LONG, True),
Tc(-4, TiffTags.SIGNED_BYTE, False),
Tc(-4, TiffTags.SIGNED_SHORT, False),
Tc(-123456789, TiffTags.SIGNED_LONG, False),
Tc(TiffImagePlugin.IFDRational(4, 7), TiffTags.RATIONAL, True),
Tc(4.25, TiffTags.FLOAT, True),
Tc(4.25, TiffTags.DOUBLE, True),
Tc("custom tag value", TiffTags.ASCII, True),
Tc(b"custom tag value", TiffTags.BYTE, True),
Tc((4, 5, 6), TiffTags.SHORT, True),
Tc((123456789, 9, 34, 234, 219387, 92432323), TiffTags.LONG, True),
Tc((-4, 9, 10), TiffTags.SIGNED_BYTE, False),
Tc((-4, 5, 6), TiffTags.SIGNED_SHORT, False),
Tc(
(-123456789, 9, 34, 234, 219387, -92432323),
TiffTags.SIGNED_LONG,
False,
),
tc((4.25, 5.25), TiffTags.FLOAT, True),
tc((4.25, 5.25), TiffTags.DOUBLE, True),
Tc((4.25, 5.25), TiffTags.FLOAT, True),
Tc((4.25, 5.25), TiffTags.DOUBLE, True),
# array of TIFF_BYTE requires bytes instead of tuple for backwards
# compatibility
tc(bytes([4]), TiffTags.BYTE, True),
tc(bytes((4, 9, 10)), TiffTags.BYTE, True),
Tc(bytes([4]), TiffTags.BYTE, True),
Tc(bytes((4, 9, 10)), TiffTags.BYTE, True),
]
)
}
Expand Down
11 changes: 7 additions & 4 deletions src/PIL/PdfParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import re
import time
import zlib
from typing import TYPE_CHECKING, Any, List, Union
from typing import TYPE_CHECKING, Any, List, NamedTuple, Union


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


class IndirectReference(
collections.namedtuple("IndirectReferenceTuple", ["object_id", "generation"])
):
class IndirectReferenceTuple(NamedTuple):
object_id: int
generation: int


class IndirectReference(IndirectReferenceTuple):
def __str__(self):
return f"{self.object_id} {self.generation} R"

Expand Down
12 changes: 10 additions & 2 deletions src/PIL/TiffTags.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,18 @@
##
from __future__ import annotations

from collections import namedtuple
from typing import NamedTuple


class TagInfo(namedtuple("_TagInfo", "value name type length enum")):
class _TagInfo(NamedTuple):
value: int
name: str
type: int
length: int
enum: dict[int, str]


class TagInfo(_TagInfo):
__slots__: list[str] = []

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

0 comments on commit 6ed952b

Please sign in to comment.