Skip to content

Commit 40a7ad8

Browse files
committed
Create 3 TypeAlias. overload read().
1 parent 0e9efcd commit 40a7ad8

1 file changed

Lines changed: 19 additions & 10 deletions

File tree

soundfile.py

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,17 @@
1616
from collections.abc import Generator
1717
from ctypes.util import find_library as _find_library
1818
from os import SEEK_CUR, SEEK_END, SEEK_SET
19-
from typing import Any, BinaryIO, Final, TypeAlias
19+
from typing import Any, BinaryIO, Final, Literal, TypeAlias, overload
2020

21-
import numpy.typing
21+
import numpy
2222
from typing_extensions import Self
2323

2424
from _soundfile import ffi as _ffi
2525

2626
FileDescriptorOrPath: TypeAlias = str | int | BinaryIO | _os.PathLike[Any]
27-
AudioData: TypeAlias = numpy.typing.NDArray[Any]
27+
AudioData: TypeAlias = numpy.ndarray[tuple[int, ...], numpy.dtype[numpy.float32 | numpy.float64 | numpy.int32 | numpy.int16]]
28+
AudioData_2d: TypeAlias = numpy.ndarray[tuple[int, int], numpy.dtype[numpy.float32 | numpy.float64 | numpy.int32 | numpy.int16]]
29+
dtype_str: TypeAlias = Literal['float64', 'float32', 'int32', 'int16']
2830
_snd: Any
2931
_ffi: Any
3032

@@ -223,13 +225,20 @@
223225
__libsndfile_version__ = __libsndfile_version__[len('libsndfile-'):]
224226

225227

226-
227-
def read(file: FileDescriptorOrPath, frames: int = -1, start: int = 0, stop: int | None = None,
228-
dtype: str = 'float64', always_2d: bool = False,
229-
fill_value: float | None = None, out: AudioData | None = None,
230-
samplerate: int | None = None, channels: int | None = None,
231-
format: str | None = None, subtype: str | None = None,
232-
endian: str | None = None, closefd: bool = True) -> tuple[AudioData, int]:
228+
@overload
229+
def read(file: FileDescriptorOrPath, frames: int = -1, start: int = 0, stop: int | None = None, dtype: dtype_str = 'float64',
230+
*, always_2d: Literal[True], fill_value: float | None = None, out: AudioData_2d | None = None,
231+
samplerate: int | None = None, channels: int | None = None, format: str | None = None, subtype: str | None = None,
232+
endian: str | None = None, closefd: bool = True) -> tuple[AudioData_2d, int]:...
233+
@overload
234+
def read(file: FileDescriptorOrPath, frames: int = -1, start: int = 0, stop: int | None = None, dtype: dtype_str = 'float64',
235+
always_2d: bool = False, fill_value: float | None = None, out: AudioData | None = None,
236+
samplerate: int | None = None, channels: int | None = None, format: str | None = None, subtype: str | None = None,
237+
endian: str | None = None, closefd: bool = True) -> tuple[AudioData, int]:...
238+
def read(file: FileDescriptorOrPath, frames: int = -1, start: int = 0, stop: int | None = None, dtype: dtype_str = 'float64',
239+
always_2d: bool = False, fill_value: float | None = None, out: AudioData | None = None,
240+
samplerate: int | None = None, channels: int | None = None, format: str | None = None, subtype: str | None = None,
241+
endian: str | None = None, closefd: bool = True) -> tuple[AudioData | AudioData_2d, int]:
233242

234243
"""Provide audio data from a sound file as NumPy array.
235244

0 commit comments

Comments
 (0)