From a4fd2e8095dbea29cee9eab7772029737e98d1c7 Mon Sep 17 00:00:00 2001 From: Jeppe Fihl-Pearson Date: Thu, 13 Jun 2024 14:37:29 +0100 Subject: [PATCH] Stop importing pyaudio as soon as the Microphone class is parsed The import is only used for type checking, so it can instead be put inside an `if TYPE_CHECKING:` block so the optional dependency isn't needed at all times. --- deepgram/audio/microphone/microphone.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/deepgram/audio/microphone/microphone.py b/deepgram/audio/microphone/microphone.py index 19bcf9e0..179a41b0 100644 --- a/deepgram/audio/microphone/microphone.py +++ b/deepgram/audio/microphone/microphone.py @@ -5,12 +5,15 @@ import inspect import asyncio import threading -from typing import Optional, Callable +from typing import Optional, Callable, TYPE_CHECKING import logging from ...utils import verboselogs from .constants import LOGGING, CHANNELS, RATE, CHUNK +if TYPE_CHECKING: + import pyaudio + class Microphone: # pylint: disable=too-many-instance-attributes """ @@ -20,10 +23,8 @@ class Microphone: # pylint: disable=too-many-instance-attributes _logger: verboselogs.VerboseLogger _exit: threading.Event - import pyaudio # pylint: disable=import-outside-toplevel - - _audio: pyaudio.PyAudio - _stream: pyaudio.Stream + _audio: "pyaudio.PyAudio" + _stream: "pyaudio.Stream" _chunk: int _rate: int _format: int