From 51e81f322084353acba8b27eb89aadb6658fd1a0 Mon Sep 17 00:00:00 2001 From: Tim Hopper Date: Wed, 17 Dec 2025 15:43:26 -0500 Subject: [PATCH] io: Add context manager stubs to AudioFile class Problem Type checkers (mypy, pyright) report "invalid-context-manager" errors when using AudioFile as a context manager because the AudioFile class stub doesn't have __enter__ and __exit__ methods defined, even though all subclasses (ReadableAudioFile, WriteableAudioFile) and the runtime implementation do support the context manager protocol. Solution Add __enter__ and __exit__ method stubs to the AudioFile base class. The __enter__ method returns Union[ReadableAudioFile, WriteableAudioFile] to match the existing __new__ overloads, allowing type checkers to understand that AudioFile instances are valid context managers. Result Type checkers will no longer report errors when using AudioFile as a context manager with `with AudioFile(...) as f:` syntax. --- pedalboard_native/io/__init__.pyi | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/pedalboard_native/io/__init__.pyi b/pedalboard_native/io/__init__.pyi index 4ad83698..ac95df0a 100644 --- a/pedalboard_native/io/__init__.pyi +++ b/pedalboard_native/io/__init__.pyi @@ -122,6 +122,16 @@ class AudioFile: those classes below for documentation. """ + def __enter__(self) -> typing.Union[ReadableAudioFile, WriteableAudioFile]: + """ + Use this :class:`AudioFile` as a context manager, automatically closing the file and releasing resources when the context manager exits. + """ + + def __exit__(self, arg0: object, arg1: object, arg2: object) -> None: + """ + Stop using this :class:`AudioFile` as a context manager, close the file, release its resources. + """ + @classmethod @typing.overload def __new__(cls, filename: str) -> ReadableAudioFile: