Skip to content

Commit

Permalink
avoid hard dependency on typing_extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
nulano committed Dec 27, 2023
1 parent cc51dac commit 3a4298d
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 9 deletions.
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 supported 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 @@ -37,9 +37,6 @@ classifiers = [
dynamic = [
"version",
]
dependencies = [
'typing-extensions; python_version < "3.10"',
]
[project.optional-dependencies]
docs = [
"furo",
Expand Down Expand Up @@ -68,6 +65,9 @@ tests = [
"pytest-cov",
"pytest-timeout",
]
typing = [
'typing-extensions; python_version < "3.10"',
]
xmp = [
"defusedxml",
]
Expand Down
16 changes: 16 additions & 0 deletions src/PIL/_typing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
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, Type

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


__all__ = ["TypeGuard"]
7 changes: 1 addition & 6 deletions src/PIL/_util.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
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
from ._typing import TypeGuard


def is_path(f: Any) -> TypeGuard[bytes | str | Path]:
Expand Down
2 changes: 2 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,5 @@ deps =
numpy
commands =
mypy src {posargs}
extras =
typing

0 comments on commit 3a4298d

Please sign in to comment.