|
50 | 50 | from itertools import count |
51 | 51 | import os |
52 | 52 | import platform |
53 | | -from typing import TYPE_CHECKING, Any, Protocol, TypeVar, cast |
| 53 | +from typing import TYPE_CHECKING, Any, ParamSpec, Protocol, TypeAlias, TypeVar, cast |
54 | 54 |
|
55 | 55 | from PIL import Image |
56 | 56 |
|
57 | 57 | from . import _convert |
58 | 58 |
|
59 | 59 | if TYPE_CHECKING: |
60 | | - # Python 3.10+ |
61 | | - from typing import ParamSpec, TypeAlias |
62 | | - |
63 | 60 | from _convert import _Buffer |
64 | 61 |
|
65 | 62 |
|
@@ -199,9 +196,7 @@ def from_param(cls, obj: _OpenSlideCache) -> _OpenSlideCache: |
199 | 196 | return obj |
200 | 197 |
|
201 | 198 |
|
202 | | -if TYPE_CHECKING: |
203 | | - # Python 3.10+ |
204 | | - Filename: TypeAlias = str | bytes | os.PathLike[Any] |
| 199 | +Filename: TypeAlias = str | bytes | os.PathLike[Any] |
205 | 200 |
|
206 | 201 |
|
207 | 202 | class _filename_p: |
@@ -314,24 +309,25 @@ def __call__(self, *_args: Any) -> Any: |
314 | 309 | raise OpenSlideVersionError(self._minimum_version) |
315 | 310 |
|
316 | 311 |
|
317 | | -# gate runtime code that requires ParamSpec, Python 3.10+ |
318 | | -if TYPE_CHECKING: |
319 | | - _P = ParamSpec('_P') |
320 | | - _T = TypeVar('_T', covariant=True) |
| 312 | +_P = ParamSpec('_P') |
| 313 | +_T = TypeVar('_T', covariant=True) |
321 | 314 |
|
322 | | - class _Func(Protocol[_P, _T]): |
323 | | - available: bool |
324 | 315 |
|
325 | | - def __call__(self, *args: _P.args) -> _T: ... # type: ignore[valid-type] |
| 316 | +class _Func(Protocol[_P, _T]): |
| 317 | + available: bool |
326 | 318 |
|
327 | | - class _CTypesFunc(_Func[_P, _T]): |
328 | | - restype: type | None |
329 | | - argtypes: list[type] |
330 | | - errcheck: _ErrCheck |
| 319 | + def __call__(self, *args: _P.args) -> _T: ... # type: ignore[valid-type] |
331 | 320 |
|
332 | | - _ErrCheck: TypeAlias = ( |
333 | | - Callable[[Any, _CTypesFunc[..., Any], tuple[Any, ...]], Any] | None |
334 | | - ) |
| 321 | + |
| 322 | +class _CTypesFunc(_Func[_P, _T]): |
| 323 | + restype: type | None |
| 324 | + argtypes: list[type] |
| 325 | + errcheck: _ErrCheck |
| 326 | + |
| 327 | + |
| 328 | +_ErrCheck: TypeAlias = ( |
| 329 | + Callable[[Any, _CTypesFunc[..., Any], tuple[Any, ...]], Any] | None |
| 330 | +) |
335 | 331 |
|
336 | 332 |
|
337 | 333 | # resolve and return an OpenSlide function with the specified properties |
@@ -361,11 +357,7 @@ def _wraps_funcs( |
361 | 357 | wrapped: list[_Func[..., Any]], |
362 | 358 | ) -> Callable[[Callable[_P, _T]], _Func[_P, _T]]: |
363 | 359 | def decorator(fn: Callable[_P, _T]) -> _Func[_P, _T]: |
364 | | - if TYPE_CHECKING: |
365 | | - # requires ParamSpec, Python 3.10+ |
366 | | - f = cast(_Func[_P, _T], fn) |
367 | | - else: |
368 | | - f = fn |
| 360 | + f = cast('_Func[_P, _T]', fn) |
369 | 361 | f.available = True |
370 | 362 | for w in wrapped: |
371 | 363 | f.available = f.available and w.available |
|
0 commit comments