Skip to content

Commit 46ede3b

Browse files
committed
Add type stubs and inline type annotations
- Add rawpy/_rawpy.pyi (614-line stub file for Cython module) - Add rawpy/py.typed (PEP 561 marker) - Add type annotations to all methods/properties in _rawpy.pyx - Convert properties from legacy Cython syntax to @Property decorator - Add TYPE_CHECKING imports to __init__.py, type-annotate imread() - Add type annotations to enhance.py, rename median -> median_func - Add test_stubtest.py (mypy stubtest verification) - Add test_mypy.py (mypy validation) - Add test_type_imports.py (verify all types importable) - Add stubtest_allowlist.txt
1 parent f502852 commit 46ede3b

File tree

10 files changed

+1160
-227
lines changed

10 files changed

+1160
-227
lines changed

rawpy/__init__.py

Lines changed: 57 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,47 @@
1-
from __future__ import absolute_import
1+
from __future__ import absolute_import, annotations
2+
3+
from typing import Union, TYPE_CHECKING
4+
5+
if TYPE_CHECKING:
6+
from typing import BinaryIO
7+
from rawpy._rawpy import (
8+
# Module-level attributes
9+
flags,
10+
libraw_version,
11+
# Main classes
12+
RawPy,
13+
Params,
14+
# Named tuples
15+
ImageSizes,
16+
Thumbnail,
17+
# Enums
18+
RawType,
19+
ThumbFormat,
20+
DemosaicAlgorithm,
21+
FBDDNoiseReductionMode,
22+
ColorSpace,
23+
HighlightMode,
24+
# Exceptions
25+
LibRawError,
26+
LibRawFatalError,
27+
LibRawNonFatalError,
28+
LibRawUnspecifiedError,
29+
LibRawFileUnsupportedError,
30+
LibRawRequestForNonexistentImageError,
31+
LibRawOutOfOrderCallError,
32+
LibRawNoThumbnailError,
33+
LibRawUnsupportedThumbnailError,
34+
LibRawInputClosedError,
35+
LibRawNotImplementedError,
36+
LibRawUnsufficientMemoryError,
37+
LibRawDataError,
38+
LibRawIOError,
39+
LibRawCancelledByCallbackError,
40+
LibRawBadCropError,
41+
LibRawTooBigError,
42+
LibRawMemPoolOverflowError,
43+
NotSupportedError,
44+
)
245

346
from ._version import __version__
447

@@ -59,21 +102,26 @@ def _check_multiprocessing_fork():
59102
# multiprocessing not available
60103
pass
61104

62-
def imread(pathOrFile, shot_select=0):
105+
def imread(pathOrFile: Union[str, BinaryIO], shot_select: int = 0) -> RawPy:
63106
"""
64107
Convenience function that creates a :class:`rawpy.RawPy` instance, opens the given file,
65108
and returns the :class:`rawpy.RawPy` instance for further processing.
66109
67-
:param str|file pathOrFile: path or file object of RAW image that will be read
68-
:param int shot_select: select which image to extract from RAW files that contain multiple images
69-
(e.g., Dual Pixel RAW). Default is 0 for the first/main image.
70-
:rtype: :class:`rawpy.RawPy`
110+
:param pathOrFile: path or file object of RAW image that will be read
111+
:type pathOrFile: str or file-like object
112+
:param shot_select: select which image to extract from RAW files that contain multiple images
113+
(e.g., Dual Pixel RAW). Default is 0 for the first/main image.
114+
:type shot_select: int
115+
:return: RawPy instance with the opened RAW image
116+
:rtype: rawpy.RawPy
71117
"""
72118
_check_multiprocessing_fork()
73119
d = RawPy()
74-
if hasattr(pathOrFile, 'read'):
75-
d.open_buffer(pathOrFile)
76-
else:
120+
if isinstance(pathOrFile, str):
121+
# pathOrFile is a string file path
77122
d.open_file(pathOrFile)
123+
else:
124+
# pathOrFile is a file-like object with read() method
125+
d.open_buffer(pathOrFile)
78126
d.set_unpack_params(shot_select=shot_select)
79127
return d

0 commit comments

Comments
 (0)