Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add type hints to _util #7642

Merged
merged 6 commits into from
Jan 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion Tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def test_deferred_error():
# Arrange

# Act
thing = _util.DeferredError(ValueError("Some error text"))
thing = _util.DeferredError.new(ValueError("Some error text"))

# Assert
with pytest.raises(ValueError):
Expand Down
13 changes: 13 additions & 0 deletions docs/reference/internal_modules.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,19 @@ Internal Modules
:undoc-members:
:show-inheritance:

:mod:`~PIL._typing` Module
--------------------------

.. module:: PIL._typing

Provides a convenient way to import type hints that are not available
on some Python versions.

.. py:data:: TypeGuard
:value: typing.TypeGuard

See :py:obj:`typing.TypeGuard`.

:mod:`~PIL._util` Module
------------------------

Expand Down
6 changes: 3 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ tests = [
"pytest-cov",
"pytest-timeout",
]
typing = [
'typing-extensions; python_version < "3.10"',
]
xmp = [
"defusedxml",
]
Expand Down Expand Up @@ -141,9 +144,6 @@ exclude = [
'^src/PIL/DdsImagePlugin.py$',
'^src/PIL/FpxImagePlugin.py$',
'^src/PIL/Image.py$',
'^src/PIL/ImageCms.py$',
'^src/PIL/ImageFile.py$',
'^src/PIL/ImageFont.py$',
'^src/PIL/ImageMath.py$',
'^src/PIL/ImageMorph.py$',
'^src/PIL/ImageQt.py$',
Expand Down
2 changes: 1 addition & 1 deletion src/PIL/Image.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
raise ImportError(msg)

except ImportError as v:
core = DeferredError(ImportError("The _imaging C module is not installed."))
core = DeferredError.new(ImportError("The _imaging C module is not installed."))

Check warning on line 95 in src/PIL/Image.py

View check run for this annotation

Codecov / codecov/patch

src/PIL/Image.py#L95

Added line #L95 was not covered by tests
# Explanations for ways that we know we might have an import error
if str(v).startswith("Module use of python"):
# The _imaging C module is present, but not compiled for
Expand Down
2 changes: 1 addition & 1 deletion src/PIL/ImageCms.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
# anything in core.
from ._util import DeferredError

_imagingcms = DeferredError(ex)
_imagingcms = DeferredError.new(ex)

Check warning on line 31 in src/PIL/ImageCms.py

View check run for this annotation

Codecov / codecov/patch

src/PIL/ImageCms.py#L31

Added line #L31 was not covered by tests

DESCRIPTION = """
pyCMS
Expand Down
4 changes: 2 additions & 2 deletions src/PIL/ImageFile.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import itertools
import struct
import sys
from typing import NamedTuple
from typing import Any, NamedTuple

from . import Image
from ._deprecate import deprecate
Expand Down Expand Up @@ -94,7 +94,7 @@ class _Tile(NamedTuple):
encoder_name: str
extents: tuple[int, int, int, int]
offset: int
args: tuple | str | None
args: tuple[Any, ...] | str | None


#
Expand Down
6 changes: 3 additions & 3 deletions src/PIL/ImageFont.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
from enum import IntEnum
from io import BytesIO
from pathlib import Path
from typing import IO
from typing import BinaryIO

from . import Image
from ._util import is_directory, is_path
Expand All @@ -53,7 +53,7 @@
except ImportError as ex:
from ._util import DeferredError

core = DeferredError(ex)
core = DeferredError.new(ex)

Check warning on line 56 in src/PIL/ImageFont.py

View check run for this annotation

Codecov / codecov/patch

src/PIL/ImageFont.py#L56

Added line #L56 was not covered by tests


def _string_length_check(text):
Expand Down Expand Up @@ -191,7 +191,7 @@

def __init__(
self,
font: bytes | str | Path | IO | None = None,
font: bytes | str | Path | BinaryIO | None = None,
size: float = 10,
index: int = 0,
encoding: str = "",
Expand Down
2 changes: 1 addition & 1 deletion src/PIL/PyAccess.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
# anything in core.
from ._util import DeferredError

FFI = ffi = DeferredError(ex)
FFI = ffi = DeferredError.new(ex)

Check warning on line 46 in src/PIL/PyAccess.py

View check run for this annotation

Codecov / codecov/patch

src/PIL/PyAccess.py#L46

Added line #L46 was not covered by tests

logger = logging.getLogger(__name__)

Expand Down
5 changes: 5 additions & 0 deletions src/PIL/_imagingcms.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from __future__ import annotations

from typing import Any

def __getattr__(name: str) -> Any: ...
5 changes: 5 additions & 0 deletions src/PIL/_imagingft.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from __future__ import annotations

from typing import Any

def __getattr__(name: str) -> Any: ...
18 changes: 18 additions & 0 deletions src/PIL/_typing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from __future__ import annotations

import sys

if sys.version_info >= (3, 10):
from typing import TypeGuard
else:
try:
from typing_extensions import TypeGuard
except ImportError:
from typing import Any

class TypeGuard: # type: ignore[no-redef]
def __class_getitem__(cls, item: Any) -> type[bool]:
return bool

Check warning on line 15 in src/PIL/_typing.py

View check run for this annotation

Codecov / codecov/patch

src/PIL/_typing.py#L15

Added line #L15 was not covered by tests


__all__ = ["TypeGuard"]
19 changes: 15 additions & 4 deletions src/PIL/_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,31 @@

import os
from pathlib import Path
from typing import Any, NoReturn

from ._typing import TypeGuard

def is_path(f):

def is_path(f: Any) -> TypeGuard[bytes | str | Path]:
return isinstance(f, (bytes, str, Path))


def is_directory(f):
def is_directory(f: Any) -> TypeGuard[bytes | str | Path]:
"""Checks if an object is a string, and that it points to a directory."""
return is_path(f) and os.path.isdir(f)


class DeferredError:
def __init__(self, ex):
def __init__(self, ex: BaseException):
self.ex = ex

def __getattr__(self, elt):
def __getattr__(self, elt: str) -> NoReturn:
raise self.ex

@staticmethod
def new(ex: BaseException) -> Any:
"""
Creates an object that raises the wrapped exception ``ex`` when used,
and casts it to :py:obj:`~typing.Any` type.
"""
return DeferredError(ex)
2 changes: 2 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,7 @@ skip_install = true
deps =
mypy==1.7.1
numpy
extras =
typing
commands =
mypy src {posargs}