avoid hard dependency on typing_extensions

This commit is contained in:
Nulano 2023-12-27 14:54:48 +01:00
parent cc51dace35
commit 3a4298d16c
5 changed files with 35 additions and 9 deletions

View File

@ -25,6 +25,19 @@ Internal Modules
:undoc-members: :undoc-members:
:show-inheritance: :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 :mod:`~PIL._util` Module
------------------------ ------------------------

View File

@ -37,9 +37,6 @@ classifiers = [
dynamic = [ dynamic = [
"version", "version",
] ]
dependencies = [
'typing-extensions; python_version < "3.10"',
]
[project.optional-dependencies] [project.optional-dependencies]
docs = [ docs = [
"furo", "furo",
@ -68,6 +65,9 @@ tests = [
"pytest-cov", "pytest-cov",
"pytest-timeout", "pytest-timeout",
] ]
typing = [
'typing-extensions; python_version < "3.10"',
]
xmp = [ xmp = [
"defusedxml", "defusedxml",
] ]

16
src/PIL/_typing.py Normal file
View File

@ -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"]

View File

@ -1,14 +1,9 @@
from __future__ import annotations from __future__ import annotations
import os import os
import sys
from pathlib import Path from pathlib import Path
from typing import Any, NoReturn from typing import Any, NoReturn
from ._typing import TypeGuard
if sys.version_info >= (3, 10):
from typing import TypeGuard
else:
from typing_extensions import TypeGuard
def is_path(f: Any) -> TypeGuard[bytes | str | Path]: def is_path(f: Any) -> TypeGuard[bytes | str | Path]:

View File

@ -37,3 +37,5 @@ deps =
numpy numpy
commands = commands =
mypy src {posargs} mypy src {posargs}
extras =
typing