Merge pull request #7897 from hugovk/flake8-pyi

Type hints: Add PYI (flake8-pyi) to Ruff and fix errors
This commit is contained in:
Andrew Murray 2024-03-27 19:58:53 +11:00 committed by GitHub
commit 6ca8bfb253
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 46 additions and 41 deletions

View File

@ -6,8 +6,8 @@ import itertools
import os import os
import re import re
import sys import sys
from collections import namedtuple
from pathlib import Path from pathlib import Path
from typing import Any, NamedTuple
import pytest import pytest
@ -243,36 +243,40 @@ class TestFileLibTiff(LibTiffTestCase):
TiffImagePlugin.WRITE_LIBTIFF = False TiffImagePlugin.WRITE_LIBTIFF = False
def test_custom_metadata(self, tmp_path: Path) -> None: 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 = { custom = {
37000 + k: v 37000 + k: v
for k, v in enumerate( for k, v in enumerate(
[ [
tc(4, TiffTags.SHORT, True), Tc(4, TiffTags.SHORT, True),
tc(123456789, TiffTags.LONG, True), Tc(123456789, TiffTags.LONG, True),
tc(-4, TiffTags.SIGNED_BYTE, False), Tc(-4, TiffTags.SIGNED_BYTE, False),
tc(-4, TiffTags.SIGNED_SHORT, False), Tc(-4, TiffTags.SIGNED_SHORT, False),
tc(-123456789, TiffTags.SIGNED_LONG, False), Tc(-123456789, TiffTags.SIGNED_LONG, False),
tc(TiffImagePlugin.IFDRational(4, 7), TiffTags.RATIONAL, True), Tc(TiffImagePlugin.IFDRational(4, 7), TiffTags.RATIONAL, True),
tc(4.25, TiffTags.FLOAT, True), Tc(4.25, TiffTags.FLOAT, True),
tc(4.25, TiffTags.DOUBLE, True), Tc(4.25, TiffTags.DOUBLE, True),
tc("custom tag value", TiffTags.ASCII, True), Tc("custom tag value", TiffTags.ASCII, True),
tc(b"custom tag value", TiffTags.BYTE, True), Tc(b"custom tag value", TiffTags.BYTE, True),
tc((4, 5, 6), TiffTags.SHORT, True), Tc((4, 5, 6), TiffTags.SHORT, True),
tc((123456789, 9, 34, 234, 219387, 92432323), TiffTags.LONG, True), Tc((123456789, 9, 34, 234, 219387, 92432323), TiffTags.LONG, True),
tc((-4, 9, 10), TiffTags.SIGNED_BYTE, False), Tc((-4, 9, 10), TiffTags.SIGNED_BYTE, False),
tc((-4, 5, 6), TiffTags.SIGNED_SHORT, False), Tc((-4, 5, 6), TiffTags.SIGNED_SHORT, False),
tc( Tc(
(-123456789, 9, 34, 234, 219387, -92432323), (-123456789, 9, 34, 234, 219387, -92432323),
TiffTags.SIGNED_LONG, TiffTags.SIGNED_LONG,
False, False,
), ),
tc((4.25, 5.25), TiffTags.FLOAT, True), Tc((4.25, 5.25), TiffTags.FLOAT, True),
tc((4.25, 5.25), TiffTags.DOUBLE, True), Tc((4.25, 5.25), TiffTags.DOUBLE, True),
# array of TIFF_BYTE requires bytes instead of tuple for backwards # array of TIFF_BYTE requires bytes instead of tuple for backwards
# compatibility # compatibility
tc(bytes([4]), TiffTags.BYTE, True), Tc(bytes([4]), TiffTags.BYTE, True),
tc(bytes((4, 9, 10)), TiffTags.BYTE, True), Tc(bytes((4, 9, 10)), TiffTags.BYTE, True),
] ]
) )
} }

View File

@ -15,7 +15,7 @@ class TestLibPack:
mode: str, mode: str,
rawmode: str, rawmode: str,
data: int | bytes, data: int | bytes,
*pixels: int | float | tuple[int, ...], *pixels: float | tuple[int, ...],
) -> None: ) -> None:
""" """
data - either raw bytes with data or just number of bytes in rawmode. data - either raw bytes with data or just number of bytes in rawmode.
@ -239,7 +239,7 @@ class TestLibUnpack:
mode: str, mode: str,
rawmode: str, rawmode: str,
data: int | bytes, data: int | bytes,
*pixels: int | float | tuple[int, ...], *pixels: float | tuple[int, ...],
) -> None: ) -> None:
""" """
data - either raw bytes with data or just number of bytes in rawmode. data - either raw bytes with data or just number of bytes in rawmode.

View File

@ -106,6 +106,7 @@ select = [
"ISC", # flake8-implicit-str-concat "ISC", # flake8-implicit-str-concat
"LOG", # flake8-logging "LOG", # flake8-logging
"PGH", # pygrep-hooks "PGH", # pygrep-hooks
"PYI", # flake8-pyi
"RUF100", # unused noqa (yesqa) "RUF100", # unused noqa (yesqa)
"UP", # pyupgrade "UP", # pyupgrade
"W", # pycodestyle warnings "W", # pycodestyle warnings
@ -116,6 +117,7 @@ ignore = [
"E221", # Multiple spaces before operator "E221", # Multiple spaces before operator
"E226", # Missing whitespace around arithmetic operator "E226", # Missing whitespace around arithmetic operator
"E241", # Multiple spaces after ',' "E241", # Multiple spaces after ','
"PYI034", # flake8-pyi: typing.Self added in Python 3.11
] ]
[tool.ruff.lint.per-file-ignores] [tool.ruff.lint.per-file-ignores]

View File

@ -8,7 +8,7 @@ import os
import re import re
import time import time
import zlib 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 # 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):
raise PdfFormatError(error_message) raise PdfFormatError(error_message)
class IndirectReference( class IndirectReferenceTuple(NamedTuple):
collections.namedtuple("IndirectReferenceTuple", ["object_id", "generation"]) object_id: int
): generation: int
class IndirectReference(IndirectReferenceTuple):
def __str__(self): def __str__(self):
return f"{self.object_id} {self.generation} R" return f"{self.object_id} {self.generation} R"

View File

@ -18,10 +18,18 @@
## ##
from __future__ import annotations 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 | None
name: str
type: int | None
length: int | None
enum: dict[str, int]
class TagInfo(_TagInfo):
__slots__: list[str] = [] __slots__: list[str] = []
def __new__(cls, value=None, name="unknown", type=None, length=None, enum=None): def __new__(cls, value=None, name="unknown", type=None, length=None, enum=None):

View File

@ -1,5 +1,3 @@
from __future__ import annotations
from typing import Any from typing import Any
def __getattr__(name: str) -> Any: ... def __getattr__(name: str) -> Any: ...

View File

@ -1,5 +1,3 @@
from __future__ import annotations
from typing import Any from typing import Any
def __getattr__(name: str) -> Any: ... def __getattr__(name: str) -> Any: ...

View File

@ -1,5 +1,3 @@
from __future__ import annotations
from typing import Any from typing import Any
def __getattr__(name: str) -> Any: ... def __getattr__(name: str) -> Any: ...

View File

@ -1,5 +1,3 @@
from __future__ import annotations
from typing import Any from typing import Any
def __getattr__(name: str) -> Any: ... def __getattr__(name: str) -> Any: ...

View File

@ -1,5 +1,3 @@
from __future__ import annotations
from typing import Any from typing import Any
def __getattr__(name: str) -> Any: ... def __getattr__(name: str) -> Any: ...

View File

@ -1,5 +1,3 @@
from __future__ import annotations
from typing import Any from typing import Any
def __getattr__(name: str) -> Any: ... def __getattr__(name: str) -> Any: ...