Skip to content

Commit 2a040ab

Browse files
authored
Merge pull request #292 from bgilbert/doc
doc: add type hints
2 parents 797ea3d + 1a1cb10 commit 2a040ab

File tree

4 files changed

+98
-75
lines changed

4 files changed

+98
-75
lines changed

doc/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
# -- General configuration -----------------------------------------------------
2323

2424
# If your documentation needs a minimal Sphinx version, state it here.
25-
# needs_sphinx = '1.0'
25+
needs_sphinx = '1.6'
2626

2727
# Add any Sphinx extension module names here, as strings. They can be extensions
2828
# coming with Sphinx (named 'sphinx.ext.*') or your custom ones.

doc/index.rst

Lines changed: 89 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -93,111 +93,123 @@ OpenSlide objects
9393

9494
.. module:: openslide
9595

96-
.. class:: OpenSlide(filename)
96+
.. class:: OpenSlide(filename: str | bytes | ~os.PathLike[typing.Any])
9797

9898
An open whole-slide image.
9999

100100
If any operation on the object fails, :exc:`OpenSlideError` is raised.
101101
OpenSlide has latching error semantics: once :exc:`OpenSlideError` is
102102
raised, all future operations on the :class:`OpenSlide`, other than
103-
:meth:`close()`, will also raise :exc:`OpenSlideError`.
103+
:meth:`close`, will also raise :exc:`OpenSlideError`.
104104

105-
:meth:`close()` is called automatically when the object is deleted.
105+
:meth:`close` is called automatically when the object is deleted.
106106
The object may be used as a context manager, in which case it will be
107107
closed upon exiting the context.
108108

109-
:param str filename: the file to open
109+
:param filename: the file to open
110110
:raises OpenSlideUnsupportedFormatError: if the file is not recognized by
111111
OpenSlide
112112
:raises OpenSlideError: if the file is recognized but an error occurred
113113

114-
.. classmethod:: detect_format(filename)
114+
.. classmethod:: detect_format(filename: str | bytes | ~os.PathLike[typing.Any]) -> str | None
115115

116116
Return a string describing the format vendor of the specified file.
117117
This string is also accessible via the :data:`PROPERTY_NAME_VENDOR`
118118
property.
119119

120120
If the file is not recognized, return :obj:`None`.
121121

122-
:param str filename: the file to examine
122+
:param filename: the file to examine
123123

124124
.. attribute:: level_count
125125

126126
The number of levels in the slide. Levels are numbered from ``0``
127127
(highest resolution) to ``level_count - 1`` (lowest resolution).
128128

129+
:type: int
130+
129131
.. attribute:: dimensions
130132

131133
A ``(width, height)`` tuple for level 0 of the slide.
132134

135+
:type: tuple[int, int]
136+
133137
.. attribute:: level_dimensions
134138

135-
A list of ``(width, height)`` tuples, one for each level of the slide.
139+
A tuple of ``(width, height)`` tuples, one for each level of the slide.
136140
``level_dimensions[k]`` are the dimensions of level ``k``.
137141

142+
:type: tuple[tuple[int, int], ...]
143+
138144
.. attribute:: level_downsamples
139145

140-
A list of downsample factors for each level of the slide.
146+
A tuple of downsample factors for each level of the slide.
141147
``level_downsamples[k]`` is the downsample factor of level ``k``.
142148

149+
:type: tuple[float, ...]
150+
143151
.. attribute:: properties
144152

145153
Metadata about the slide, in the form of a
146154
:class:`~collections.abc.Mapping` from OpenSlide property name to
147-
property value. Property values are always strings. OpenSlide
148-
provides some :ref:`standard-properties`, plus
149-
additional properties that vary by slide format.
155+
property value. OpenSlide provides some :ref:`standard-properties`,
156+
plus additional properties that vary by slide format.
157+
158+
:type: ~collections.abc.Mapping[str, str]
150159

151160
.. attribute:: associated_images
152161

153162
Images, such as label or macro images, which are associated with this
154163
slide. This is a :class:`~collections.abc.Mapping` from image
155-
name to RGBA :class:`Image <PIL.Image.Image>`.
164+
name to RGBA :class:`~PIL.Image.Image`.
156165

157166
Unlike in the C interface, these images are not premultiplied.
158167

168+
:type: ~collections.abc.Mapping[str, ~PIL.Image.Image]
169+
159170
.. attribute:: color_profile
160171

161172
The embedded :ref:`color profile <color-management>` for this slide,
162-
as a Pillow :class:`~PIL.ImageCms.ImageCmsProfile`, or :obj:`None` if
163-
not available.
173+
or :obj:`None` if not available.
174+
175+
:type: ~PIL.ImageCms.ImageCmsProfile | None
164176

165-
.. method:: read_region(location, level, size)
177+
.. method:: read_region(location: tuple[int, int], level: int, size: tuple[int, int]) -> ~PIL.Image.Image
166178

167-
Return an RGBA :class:`Image <PIL.Image.Image>` containing the
168-
contents of the specified region.
179+
Return an RGBA :class:`~PIL.Image.Image` containing the contents of
180+
the specified region.
169181

170182
Unlike in the C interface, the image data is not premultiplied.
171183

172-
:param tuple location: ``(x, y)`` tuple giving the top left pixel in
173-
the level 0 reference frame
174-
:param int level: the level number
175-
:param tuple size: ``(width, height)`` tuple giving the region size
184+
:param location: ``(x, y)`` tuple giving the top left pixel in the
185+
level 0 reference frame
186+
:param level: the level number
187+
:param size: ``(width, height)`` tuple giving the region size
176188

177-
.. method:: get_best_level_for_downsample(downsample)
189+
.. method:: get_best_level_for_downsample(downsample: float) -> int
178190

179191
Return the best level for displaying the given downsample.
180192

181-
:param float downsample: the desired downsample factor
193+
:param downsample: the desired downsample factor
182194

183-
.. method:: get_thumbnail(size)
195+
.. method:: get_thumbnail(size: tuple[int, int]) -> ~PIL.Image.Image
184196

185-
Return an :class:`Image <PIL.Image.Image>` containing an RGB thumbnail
186-
of the slide.
197+
Return an :class:`~PIL.Image.Image` containing an RGB thumbnail of the
198+
slide.
187199

188-
:param tuple size: the maximum size of the thumbnail as a
189-
``(width, height)`` tuple
200+
:param size: the maximum size of the thumbnail as a ``(width, height)``
201+
tuple
190202

191-
.. method:: set_cache(cache)
203+
.. method:: set_cache(cache: OpenSlideCache) -> None
192204

193205
Use the specified :class:`OpenSlideCache` to store recently decoded
194206
slide tiles. By default, the :class:`OpenSlide` has a private cache
195207
with a default size.
196208

197-
:param OpenSlideCache cache: a cache object
209+
:param cache: a cache object
198210
:raises OpenSlideVersionError: if OpenSlide is older than version 4.0.0
199211

200-
.. method:: close()
212+
.. method:: close() -> None
201213

202214
Close the OpenSlide object.
203215

@@ -254,15 +266,15 @@ reusing it for multiple slide regions::
254266
Caching
255267
-------
256268

257-
.. class:: OpenSlideCache(capacity)
269+
.. class:: OpenSlideCache(capacity: int)
258270

259271
An in-memory tile cache.
260272

261273
Tile caches can be attached to one or more :class:`OpenSlide` objects
262274
with :meth:`OpenSlide.set_cache` to cache recently-decoded tiles. By
263275
default, each :class:`OpenSlide` has its own cache with a default size.
264276

265-
:param int capacity: the cache capacity in bytes
277+
:param capacity: the cache capacity in bytes
266278
:raises OpenSlideVersionError: if OpenSlide is older than version 4.0.0
267279

268280

@@ -335,7 +347,7 @@ Exceptions
335347

336348
Once :exc:`OpenSlideError` has been raised by a particular
337349
:class:`OpenSlide`, all future operations on that :class:`OpenSlide`
338-
(other than :meth:`close() <OpenSlide.close>`) will also raise
350+
(other than :meth:`~OpenSlide.close`) will also raise
339351
:exc:`OpenSlideError`.
340352

341353
.. exception:: OpenSlideUnsupportedFormatError
@@ -354,20 +366,24 @@ Exceptions
354366
Wrapping a Pillow Image
355367
=======================
356368

357-
.. class:: ImageSlide(file)
369+
.. class:: AbstractSlide
358370

359-
A wrapper around an :class:`Image <PIL.Image.Image>` object that
360-
provides an :class:`OpenSlide`-compatible API.
371+
The abstract base class of :class:`OpenSlide` and :class:`ImageSlide`.
361372

362-
:param file: a filename or :class:`Image <PIL.Image.Image>` object
373+
.. class:: ImageSlide(file: str | bytes | ~os.PathLike[typing.Any] | ~PIL.Image.Image)
374+
375+
A wrapper around an :class:`~PIL.Image.Image` object that provides an
376+
:class:`OpenSlide`-compatible API.
377+
378+
:param file: a filename or :class:`~PIL.Image.Image` object
363379
:raises OSError: if the file cannot be opened
364380

365-
.. function:: open_slide(filename)
381+
.. function:: open_slide(filename: str | bytes | ~os.PathLike[typing.Any]) -> OpenSlide | ImageSlide
366382

367383
Return an :class:`OpenSlide` for whole-slide images and an
368384
:class:`ImageSlide` for other types of images.
369385

370-
:param str filename: the file to open
386+
:param filename: the file to open
371387
:raises OpenSlideError: if the file is recognized by OpenSlide but an
372388
error occurred
373389
:raises OSError: if the file is not recognized at all
@@ -385,72 +401,79 @@ Deep Zoom or a similar format.
385401

386402
.. _`Deep Zoom`: https://docs.microsoft.com/en-us/previous-versions/windows/silverlight/dotnet-windows-silverlight/cc645050(v=vs.95)
387403

388-
.. class:: DeepZoomGenerator(osr, tile_size=254, overlap=1, limit_bounds=False)
404+
.. class:: DeepZoomGenerator(osr: AbstractSlide, tile_size: int = 254, overlap: int = 1, limit_bounds: bool = False)
389405

390-
A Deep Zoom generator that wraps an
391-
:class:`OpenSlide <openslide.OpenSlide>` or
392-
:class:`ImageSlide <openslide.ImageSlide>` object.
406+
A Deep Zoom generator that wraps an :class:`~openslide.OpenSlide` object,
407+
:class:`~openslide.ImageSlide` object, or user-provided instance of
408+
:class:`~openslide.AbstractSlide`.
393409

394410
:param osr: the slide object
395-
:param int tile_size: the width and height of a single tile. For best
396-
viewer performance, ``tile_size + 2 * overlap`` should be a power of two.
397-
:param int overlap: the number of extra pixels to add to each interior edge
398-
of a tile
399-
:param bool limit_bounds: ``True`` to render only the non-empty slide
400-
region
411+
:param tile_size: the width and height of a single tile. For best viewer
412+
performance, ``tile_size + 2 * overlap`` should be a power of two.
413+
:param overlap: the number of extra pixels to add to each interior edge of a
414+
tile
415+
:param limit_bounds: :obj:`True` to render only the non-empty slide region
401416

402417
.. attribute:: level_count
403418

404419
The number of Deep Zoom levels in the image.
405420

421+
:type: int
422+
406423
.. attribute:: tile_count
407424

408425
The total number of Deep Zoom tiles in the image.
409426

427+
:type: int
428+
410429
.. attribute:: level_tiles
411430

412-
A list of ``(tiles_x, tiles_y)`` tuples for each Deep Zoom level.
431+
A tuple of ``(tiles_x, tiles_y)`` tuples for each Deep Zoom level.
413432
``level_tiles[k]`` are the tile counts of level ``k``.
414433

434+
:type: tuple[tuple[int, int], ...]
435+
415436
.. attribute:: level_dimensions
416437

417-
A list of ``(pixels_x, pixels_y)`` tuples for each Deep Zoom level.
438+
A tuple of ``(pixels_x, pixels_y)`` tuples for each Deep Zoom level.
418439
``level_dimensions[k]`` are the dimensions of level ``k``.
419440

420-
.. method:: get_dzi(format)
441+
:type: tuple[tuple[int, int], ...]
442+
443+
.. method:: get_dzi(format: str) -> str
421444

422445
Return a string containing the XML metadata for the Deep Zoom ``.dzi``
423446
file.
424447

425-
:param str format: the delivery format of the individual tiles
426-
(``png`` or ``jpeg``)
448+
:param format: the delivery format of the individual tiles (``png`` or
449+
``jpeg``)
427450

428-
.. method:: get_tile(level, address)
451+
.. method:: get_tile(level: int, address: tuple[int, int]) -> ~PIL.Image.Image
429452

430-
Return an RGB :class:`Image <PIL.Image.Image>` for a tile.
453+
Return an RGB :class:`~PIL.Image.Image` for a tile.
431454

432-
:param int level: the Deep Zoom level
433-
:param tuple address: the address of the tile within the level as a
455+
:param level: the Deep Zoom level
456+
:param address: the address of the tile within the level as a
434457
``(column, row)`` tuple
435458

436-
.. method:: get_tile_coordinates(level, address)
459+
.. method:: get_tile_coordinates(level: int, address: tuple[int, int]) -> tuple[tuple[int, int], int, tuple[int, int]]
437460

438461
Return the :meth:`OpenSlide.read_region()
439462
<openslide.OpenSlide.read_region>` arguments corresponding to the
440463
specified tile.
441464

442-
Most applications should use :meth:`get_tile()` instead.
465+
Most applications should use :meth:`get_tile` instead.
443466

444-
:param int level: the Deep Zoom level
445-
:param tuple address: the address of the tile within the level as a
467+
:param level: the Deep Zoom level
468+
:param address: the address of the tile within the level as a
446469
``(column, row)`` tuple
447470

448-
.. method:: get_tile_dimensions(level, address)
471+
.. method:: get_tile_dimensions(level: int, address: tuple[int, int]) -> tuple[int, int]
449472

450473
Return a ``(pixels_x, pixels_y)`` tuple for the specified tile.
451474

452-
:param int level: the Deep Zoom level
453-
:param tuple address: the address of the tile within the level as a
475+
:param level: the Deep Zoom level
476+
:param address: the address of the tile within the level as a
454477
``(column, row)`` tuple
455478

456479

openslide/__init__.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def level_count(self) -> int:
9898

9999
@property
100100
def level_dimensions(self) -> tuple[tuple[int, int], ...]:
101-
"""A list of (width, height) tuples, one for each level of the image.
101+
"""A tuple of (width, height) tuples, one for each level of the image.
102102
103103
level_dimensions[n] contains the dimensions of level n."""
104104
raise NotImplementedError
@@ -110,7 +110,7 @@ def dimensions(self) -> tuple[int, int]:
110110

111111
@property
112112
def level_downsamples(self) -> tuple[float, ...]:
113-
"""A list of downsampling factors for each level of the image.
113+
"""A tuple of downsampling factors for each level of the image.
114114
115115
level_downsample[n] contains the downsample factor of level n."""
116116
raise NotImplementedError
@@ -217,7 +217,7 @@ def level_count(self) -> int:
217217

218218
@property
219219
def level_dimensions(self) -> tuple[tuple[int, int], ...]:
220-
"""A list of (width, height) tuples, one for each level of the image.
220+
"""A tuple of (width, height) tuples, one for each level of the image.
221221
222222
level_dimensions[n] contains the dimensions of level n."""
223223
return tuple(
@@ -226,7 +226,7 @@ def level_dimensions(self) -> tuple[tuple[int, int], ...]:
226226

227227
@property
228228
def level_downsamples(self) -> tuple[float, ...]:
229-
"""A list of downsampling factors for each level of the image.
229+
"""A tuple of downsampling factors for each level of the image.
230230
231231
level_downsample[n] contains the downsample factor of level n."""
232232
return tuple(
@@ -402,7 +402,7 @@ def level_count(self) -> Literal[1]:
402402

403403
@property
404404
def level_dimensions(self) -> tuple[tuple[int, int]]:
405-
"""A list of (width, height) tuples, one for each level of the image.
405+
"""A tuple of (width, height) tuples, one for each level of the image.
406406
407407
level_dimensions[n] contains the dimensions of level n."""
408408
if self._image is None:
@@ -411,7 +411,7 @@ def level_dimensions(self) -> tuple[tuple[int, int]]:
411411

412412
@property
413413
def level_downsamples(self) -> tuple[float]:
414-
"""A list of downsampling factors for each level of the image.
414+
"""A tuple of downsampling factors for each level of the image.
415415
416416
level_downsample[n] contains the downsample factor of level n."""
417417
return (1.0,)

openslide/deepzoom.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,12 +163,12 @@ def level_count(self) -> int:
163163

164164
@property
165165
def level_tiles(self) -> tuple[tuple[int, int], ...]:
166-
"""A list of (tiles_x, tiles_y) tuples for each Deep Zoom level."""
166+
"""A tuple of (tiles_x, tiles_y) tuples for each Deep Zoom level."""
167167
return self._t_dimensions
168168

169169
@property
170170
def level_dimensions(self) -> tuple[tuple[int, int], ...]:
171-
"""A list of (pixels_x, pixels_y) tuples for each Deep Zoom level."""
171+
"""A tuple of (pixels_x, pixels_y) tuples for each Deep Zoom level."""
172172
return self._z_dimensions
173173

174174
@property

0 commit comments

Comments
 (0)