From b4ee9673729df7a996fc04fe9d3487f99bf32ef5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Robert?= Date: Mon, 1 Jul 2024 19:16:13 +0200 Subject: [PATCH 1/2] BUG: fix an incompatibility with numpy 1.20 --- src/PIL/_typing.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/PIL/_typing.py b/src/PIL/_typing.py index 09ece18fa..435c67f04 100644 --- a/src/PIL/_typing.py +++ b/src/PIL/_typing.py @@ -7,8 +7,8 @@ from typing import Any, Protocol, Sequence, TypeVar, Union try: import numpy.typing as npt - NumpyArray = npt.NDArray[Any] -except ImportError: + NumpyArray = npt.NDArray[Any] # requires numpy>=1.21 +except (ImportError, AttributeError): pass if sys.version_info >= (3, 10): From 8b8fc18998bb416631acc81f54e95e8c8fc50f61 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Tue, 2 Jul 2024 19:05:59 +1000 Subject: [PATCH 2/2] Do not import numpy.typing unless TYPE_CHECKING --- src/PIL/_typing.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/PIL/_typing.py b/src/PIL/_typing.py index 435c67f04..db1e80e2f 100644 --- a/src/PIL/_typing.py +++ b/src/PIL/_typing.py @@ -2,14 +2,15 @@ from __future__ import annotations import os import sys -from typing import Any, Protocol, Sequence, TypeVar, Union +from typing import TYPE_CHECKING, Any, Protocol, Sequence, TypeVar, Union -try: - import numpy.typing as npt +if TYPE_CHECKING: + try: + import numpy.typing as npt - NumpyArray = npt.NDArray[Any] # requires numpy>=1.21 -except (ImportError, AttributeError): - pass + NumpyArray = npt.NDArray[Any] # requires numpy>=1.21 + except (ImportError, AttributeError): + pass if sys.version_info >= (3, 10): from typing import TypeGuard