Skip to content

Commit

Permalink
add type hints for _util
Browse files Browse the repository at this point in the history
  • Loading branch information
nulano committed Dec 27, 2023
1 parent d4fd049 commit 1856eed
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ classifiers = [
dynamic = [
"version",
]
dependencies = [
'typing-extensions; python_version < "3.10"',
]
[project.optional-dependencies]
docs = [
"furo",
Expand Down
19 changes: 15 additions & 4 deletions src/PIL/_util.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,32 @@
from __future__ import annotations

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

if sys.version_info >= (3, 10):
from typing import TypeGuard
else:
from typing_extensions 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:
return DeferredError(ex)

0 comments on commit 1856eed

Please sign in to comment.