Skip to content

Commit 3a4298d

Browse files
committed
avoid hard dependency on typing_extensions
1 parent cc51dac commit 3a4298d

File tree

5 files changed

+35
-9
lines changed

5 files changed

+35
-9
lines changed

docs/reference/internal_modules.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,19 @@ Internal Modules
2525
:undoc-members:
2626
:show-inheritance:
2727

28+
:mod:`~PIL._typing` Module
29+
--------------------------
30+
31+
.. module:: PIL._typing
32+
33+
Provides a convenient way to import type hints that are not available
34+
on some supported Python versions.
35+
36+
.. py:data:: TypeGuard
37+
:value: typing.TypeGuard
38+
39+
See :py:obj:`typing.TypeGuard`.
40+
2841
:mod:`~PIL._util` Module
2942
------------------------
3043

pyproject.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,6 @@ classifiers = [
3737
dynamic = [
3838
"version",
3939
]
40-
dependencies = [
41-
'typing-extensions; python_version < "3.10"',
42-
]
4340
[project.optional-dependencies]
4441
docs = [
4542
"furo",
@@ -68,6 +65,9 @@ tests = [
6865
"pytest-cov",
6966
"pytest-timeout",
7067
]
68+
typing = [
69+
'typing-extensions; python_version < "3.10"',
70+
]
7171
xmp = [
7272
"defusedxml",
7373
]

src/PIL/_typing.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import sys
2+
3+
if sys.version_info >= (3, 10):
4+
from typing import TypeGuard
5+
else:
6+
try:
7+
from typing_extensions import TypeGuard
8+
except ImportError:
9+
from typing import Any, Type
10+
11+
class TypeGuard: # type: ignore[no-redef]
12+
def __class_getitem__(cls, item: Any) -> Type[bool]:
13+
return bool
14+
15+
16+
__all__ = ["TypeGuard"]

src/PIL/_util.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
11
from __future__ import annotations
22

33
import os
4-
import sys
54
from pathlib import Path
65
from typing import Any, NoReturn
7-
8-
if sys.version_info >= (3, 10):
9-
from typing import TypeGuard
10-
else:
11-
from typing_extensions import TypeGuard
6+
from ._typing import TypeGuard
127

138

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

tox.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,5 @@ deps =
3737
numpy
3838
commands =
3939
mypy src {posargs}
40+
extras =
41+
typing

0 commit comments

Comments
 (0)