From 3a4298d16c660d5dec46cfa372b6aceb79c1e056 Mon Sep 17 00:00:00 2001 From: Nulano Date: Wed, 27 Dec 2023 14:54:48 +0100 Subject: [PATCH] avoid hard dependency on typing_extensions --- docs/reference/internal_modules.rst | 13 +++++++++++++ pyproject.toml | 6 +++--- src/PIL/_typing.py | 16 ++++++++++++++++ src/PIL/_util.py | 7 +------ tox.ini | 2 ++ 5 files changed, 35 insertions(+), 9 deletions(-) create mode 100644 src/PIL/_typing.py diff --git a/docs/reference/internal_modules.rst b/docs/reference/internal_modules.rst index 363a67d9b..57dd3f35b 100644 --- a/docs/reference/internal_modules.rst +++ b/docs/reference/internal_modules.rst @@ -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 ------------------------ diff --git a/pyproject.toml b/pyproject.toml index de707a283..abb32f14d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -37,9 +37,6 @@ classifiers = [ dynamic = [ "version", ] -dependencies = [ - 'typing-extensions; python_version < "3.10"', -] [project.optional-dependencies] docs = [ "furo", @@ -68,6 +65,9 @@ tests = [ "pytest-cov", "pytest-timeout", ] +typing = [ + 'typing-extensions; python_version < "3.10"', +] xmp = [ "defusedxml", ] diff --git a/src/PIL/_typing.py b/src/PIL/_typing.py new file mode 100644 index 000000000..583be41b7 --- /dev/null +++ b/src/PIL/_typing.py @@ -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"] diff --git a/src/PIL/_util.py b/src/PIL/_util.py index d11cbfa58..37cd979f6 100644 --- a/src/PIL/_util.py +++ b/src/PIL/_util.py @@ -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]: diff --git a/tox.ini b/tox.ini index 034d89372..bfcfaf5d9 100644 --- a/tox.ini +++ b/tox.ini @@ -37,3 +37,5 @@ deps = numpy commands = mypy src {posargs} +extras = + typing