Skip to content

Commit 11954d4

Browse files
authored
Merge pull request #356 from bgilbert/3.10
Remove conditionals for Python 3.10 features
2 parents 86369e5 + 9d8ec5c commit 11954d4

5 files changed

Lines changed: 53 additions & 78 deletions

File tree

examples/deepzoom/deepzoom_multiserver.py

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,12 @@
2828
import os
2929
from pathlib import Path, PurePath
3030
from threading import Lock
31-
from typing import TYPE_CHECKING, Any, Literal
31+
from typing import Any, Literal, TypeAlias
3232
import zlib
3333

3434
from flask import Flask, Response, abort, make_response, render_template, url_for
3535
from PIL import Image, ImageCms
3636

37-
if TYPE_CHECKING:
38-
# Python 3.10+
39-
from typing import TypeAlias
40-
4137
if os.name == 'nt':
4238
_dll_path = os.getenv('OPENSLIDE_PATH')
4339
if _dll_path is not None:
@@ -68,17 +64,16 @@
6864
)
6965
SRGB_PROFILE = ImageCms.getOpenProfile(BytesIO(SRGB_PROFILE_BYTES))
7066

71-
if TYPE_CHECKING:
72-
ColorMode: TypeAlias = Literal[
73-
'default',
74-
'absolute-colorimetric',
75-
'perceptual',
76-
'relative-colorimetric',
77-
'saturation',
78-
'embed',
79-
'ignore',
80-
]
81-
Transform: TypeAlias = Callable[[Image.Image], None]
67+
ColorMode: TypeAlias = Literal[
68+
'default',
69+
'absolute-colorimetric',
70+
'perceptual',
71+
'relative-colorimetric',
72+
'saturation',
73+
'embed',
74+
'ignore',
75+
]
76+
Transform: TypeAlias = Callable[[Image.Image], None]
8277

8378

8479
class DeepZoomMultiServer(Flask):

examples/deepzoom/deepzoom_server.py

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,13 @@
2727
import os
2828
from pathlib import Path
2929
import re
30-
from typing import TYPE_CHECKING, Any, Literal
30+
from typing import Any, Literal, TypeAlias
3131
from unicodedata import normalize
3232
import zlib
3333

3434
from flask import Flask, Response, abort, make_response, render_template, url_for
3535
from PIL import Image, ImageCms
3636

37-
if TYPE_CHECKING:
38-
# Python 3.10+
39-
from typing import TypeAlias
40-
4137
if os.name == 'nt':
4238
_dll_path = os.getenv('OPENSLIDE_PATH')
4339
if _dll_path is not None:
@@ -70,17 +66,16 @@
7066
)
7167
SRGB_PROFILE = ImageCms.getOpenProfile(BytesIO(SRGB_PROFILE_BYTES))
7268

73-
if TYPE_CHECKING:
74-
ColorMode: TypeAlias = Literal[
75-
'default',
76-
'absolute-colorimetric',
77-
'perceptual',
78-
'relative-colorimetric',
79-
'saturation',
80-
'embed',
81-
'ignore',
82-
]
83-
Transform: TypeAlias = Callable[[Image.Image], None]
69+
ColorMode: TypeAlias = Literal[
70+
'default',
71+
'absolute-colorimetric',
72+
'perceptual',
73+
'relative-colorimetric',
74+
'saturation',
75+
'embed',
76+
'ignore',
77+
]
78+
Transform: TypeAlias = Callable[[Image.Image], None]
8479

8580

8681
class DeepZoomServer(Flask):

examples/deepzoom/deepzoom_tile.py

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,12 @@
3434
import re
3535
import shutil
3636
import sys
37-
from typing import TYPE_CHECKING, Literal
37+
from typing import TYPE_CHECKING, Literal, TypeAlias
3838
from unicodedata import normalize
3939
import zlib
4040

4141
from PIL import Image, ImageCms
4242

43-
if TYPE_CHECKING:
44-
# Python 3.10+
45-
from typing import TypeAlias
46-
4743
if os.name == 'nt':
4844
_dll_path = os.getenv('OPENSLIDE_PATH')
4945
if _dll_path is not None:
@@ -76,20 +72,21 @@
7672
)
7773
SRGB_PROFILE = ImageCms.getOpenProfile(BytesIO(SRGB_PROFILE_BYTES))
7874

75+
ColorMode: TypeAlias = Literal[
76+
'default',
77+
'absolute-colorimetric',
78+
'perceptual',
79+
'relative-colorimetric',
80+
'saturation',
81+
'embed',
82+
'ignore',
83+
]
84+
Transform: TypeAlias = Callable[[Image.Image], None]
7985
if TYPE_CHECKING:
80-
ColorMode: TypeAlias = Literal[
81-
'default',
82-
'absolute-colorimetric',
83-
'perceptual',
84-
'relative-colorimetric',
85-
'saturation',
86-
'embed',
87-
'ignore',
88-
]
86+
# Python 3.12+
8987
TileQueue: TypeAlias = multiprocessing.queues.JoinableQueue[
9088
tuple[str | None, int, tuple[int, int], Path] | None
9189
]
92-
Transform: TypeAlias = Callable[[Image.Image], None]
9390

9491

9592
class TileWorker(Process):

openslide/deepzoom.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,13 @@
2626

2727
from io import BytesIO
2828
import math
29-
from typing import TYPE_CHECKING
29+
from typing import TypeGuard
3030
from xml.etree.ElementTree import Element, ElementTree, SubElement
3131

3232
from PIL import Image
3333

3434
import openslide
3535

36-
if TYPE_CHECKING:
37-
# Python 3.10+
38-
from typing import TypeGuard
39-
4036

4137
class DeepZoomGenerator:
4238
"""Generates Deep Zoom tiles and metadata."""

openslide/lowlevel.py

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,13 @@
5050
from itertools import count
5151
import os
5252
import platform
53-
from typing import TYPE_CHECKING, Any, Protocol, TypeVar, cast
53+
from typing import TYPE_CHECKING, Any, ParamSpec, Protocol, TypeAlias, TypeVar, cast
5454

5555
from PIL import Image
5656

5757
from . import _convert
5858

5959
if TYPE_CHECKING:
60-
# Python 3.10+
61-
from typing import ParamSpec, TypeAlias
62-
6360
from _convert import _Buffer
6461

6562

@@ -199,9 +196,7 @@ def from_param(cls, obj: _OpenSlideCache) -> _OpenSlideCache:
199196
return obj
200197

201198

202-
if TYPE_CHECKING:
203-
# Python 3.10+
204-
Filename: TypeAlias = str | bytes | os.PathLike[Any]
199+
Filename: TypeAlias = str | bytes | os.PathLike[Any]
205200

206201

207202
class _filename_p:
@@ -314,24 +309,25 @@ def __call__(self, *_args: Any) -> Any:
314309
raise OpenSlideVersionError(self._minimum_version)
315310

316311

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)
321314

322-
class _Func(Protocol[_P, _T]):
323-
available: bool
324315

325-
def __call__(self, *args: _P.args) -> _T: ... # type: ignore[valid-type]
316+
class _Func(Protocol[_P, _T]):
317+
available: bool
326318

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]
331320

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+
)
335331

336332

337333
# resolve and return an OpenSlide function with the specified properties
@@ -361,11 +357,7 @@ def _wraps_funcs(
361357
wrapped: list[_Func[..., Any]],
362358
) -> Callable[[Callable[_P, _T]], _Func[_P, _T]]:
363359
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)
369361
f.available = True
370362
for w in wrapped:
371363
f.available = f.available and w.available

0 commit comments

Comments
 (0)