Skip to content
This repository was archived by the owner on May 6, 2022. It is now read-only.

Commit 3281e5f

Browse files
authored
mypy config update with type fixes (#36)
1 parent 2330d4d commit 3281e5f

40 files changed

+182
-126
lines changed

.circleci/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ jobs:
3232
black spokestack tests
3333
- run: |
3434
source ~/venv/bin/activate
35-
mypy spokestack tests
35+
mypy spokestack
3636
- run: |
3737
source ~/venv/bin/activate
3838
python setup.py build_ext -i

.pre-commit-config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ repos:
2323
rev: v0.790
2424
hooks:
2525
- id: mypy
26+
exclude: ^tests/
2627
- repo: https://github.com/asottile/blacken-docs
2728
rev: v1.8.0
2829
hooks:

mypy.ini

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
[mypy]
2+
disallow_subclassing_any = True
3+
check_untyped_defs = True
4+
disallow_untyped_defs = True
5+
disallow_incomplete_defs = True
6+
no_implicit_optional = True
7+
warn_unused_ignores = True
8+
warn_no_return = True
9+
warn_unreachable = True
10+
[mypy-numpy.*]
11+
ignore_missing_imports = True
12+
[mypy-pynvml.*]
13+
ignore_missing_imports = True
14+
[mypy-tensorflow.*]
15+
ignore_missing_imports = True
16+
[mypy-google.*]
17+
ignore_missing_imports = True
18+
[mypy-websocket.*]
19+
ignore_missing_imports = True
20+
[mypy-pyaudio.*]
21+
ignore_missing_imports = True
22+
[mypy-tflite_runtime.*]
23+
ignore_missing_imports = True
24+
[mypy-tokenizers.*]
25+
ignore_missing_imports = True
26+
[mypy-spokestack.extensions.*]
27+
ignore_missing_imports = True
28+
[mypy-streamp3.*]
29+
ignore_missing_imports = True
30+
[mypy-pytest.*]
31+
ignore_missing_imports = True
32+
[mypy-Cython.*]
33+
ignore_missing_imports = True

setup.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import numpy as np
44
import setuptools
5-
from Cython.Build import cythonize # type: ignore
5+
from Cython.Build import cythonize
66

77
SOURCES = [
88
os.path.join("spokestack/extensions/webrtc", source)
@@ -61,7 +61,7 @@
6161

6262
setuptools.setup(
6363
name="spokestack",
64-
version="0.0.9",
64+
version="0.0.10",
6565
author="Spokestack",
6666
author_email="support@spokestack.io",
6767
description="Spokestack Library for Python",
@@ -75,14 +75,16 @@
7575
"Operating System :: OS Independent",
7676
],
7777
python_requires=">=3.8",
78+
setup_requires=["setuptools", "cython", "numpy"],
7879
install_requires=[
80+
"numpy",
7981
"pyaudio",
8082
"webrtcvad",
81-
"numpy",
8283
"websocket_client",
8384
"tokenizers",
8485
"requests",
8586
"streamp3",
87+
"cython",
8688
],
8789
ext_modules=cythonize(EXTENSIONS),
8890
include_dirs=[np.get_include()],

spokestack/activation_timeout.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
"""
22
This module manages the timeout for speech pipeline activation.
33
"""
4+
from typing import Any, Union
5+
6+
import numpy as np
7+
48
from spokestack.context import SpeechContext
59

610

@@ -14,15 +18,21 @@ class ActivationTimeout:
1418
"""
1519

1620
def __init__(
17-
self, frame_width=20, min_active=500, max_active=5000, **kwargs
21+
self,
22+
frame_width: int = 20,
23+
min_active: int = 500,
24+
max_active: int = 5000,
25+
**kwargs: Any
1826
) -> None:
1927

2028
self._min_active = min_active / frame_width
2129
self._max_active = max_active / frame_width
2230
self._is_speech = False
2331
self._active_length = 0
2432

25-
def __call__(self, context: SpeechContext, frame=None) -> None:
33+
def __call__(
34+
self, context: SpeechContext, frame: Union[np.ndarray, None] = None
35+
) -> None:
2636
"""Main entry point that manages timeout
2737
2838
Args:

spokestack/agc/webrtc.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
"""
22
This module contains the class for webrtc's automatic gain control
33
"""
4-
import numpy as np # type: ignore
4+
from typing import Any
5+
6+
import numpy as np
57

68
from spokestack.context import SpeechContext
7-
from spokestack.extensions.webrtc.agc import WebRtcAgc # type: ignore
9+
from spokestack.extensions.webrtc.agc import WebRtcAgc
810

911

1012
class AutomaticGainControl:
@@ -20,12 +22,12 @@ class AutomaticGainControl:
2022

2123
def __init__(
2224
self,
23-
sample_rate=16000,
24-
frame_width=20,
25-
target_level_dbfs=3,
26-
compression_gain_db=15,
27-
limit_enable=True,
28-
**kwargs
25+
sample_rate: int = 16000,
26+
frame_width: int = 20,
27+
target_level_dbfs: int = 3,
28+
compression_gain_db: int = 15,
29+
limit_enable: bool = True,
30+
**kwargs: Any
2931
) -> None:
3032
# validate sample rate
3133
self._sample_rate = sample_rate

spokestack/asr/google/speech_recognizer.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@
44
import logging
55
from queue import Queue
66
from threading import Thread
7-
from typing import Any, Union
7+
from typing import Any, Generator, Union
88

9-
import numpy as np # type: ignore
10-
from google.cloud import speech # type: ignore
11-
from google.oauth2 import service_account # type: ignore
9+
import numpy as np
10+
from google.cloud import speech
11+
from google.oauth2 import service_account
1212

1313
from spokestack.context import SpeechContext
1414

15-
1615
_LOG = logging.getLogger(__name__)
1716

1817

@@ -37,7 +36,7 @@ def __init__(
3736
language: str,
3837
credentials: Union[None, str, dict] = None,
3938
sample_rate: int = 16000,
40-
**kwargs,
39+
**kwargs: Any,
4140
) -> None:
4241
if credentials:
4342
if isinstance(credentials, str):
@@ -83,14 +82,14 @@ def __call__(self, context: SpeechContext, frame: np.ndarray) -> None:
8382
if context.is_active:
8483
self._send(frame)
8584

86-
def _begin(self, context) -> None:
85+
def _begin(self, context: SpeechContext) -> None:
8786
self._thread = Thread(
8887
target=self._receive,
8988
args=(context,),
9089
)
9190
self._thread.start()
9291

93-
def _receive(self, context):
92+
def _receive(self, context: SpeechContext) -> None:
9493
for response in self._client.streaming_recognize(self._config, self._drain()):
9594
for result in response.results[:1]:
9695
for alternative in result.alternatives[:1]:
@@ -107,7 +106,7 @@ def _receive(self, context):
107106
context.event("timeout")
108107
_LOG.debug("timeout event")
109108

110-
def _drain(self):
109+
def _drain(self) -> Generator:
111110
while data := self._queue.get():
112111
yield data
113112

@@ -116,7 +115,7 @@ def _commit(self) -> None:
116115
self._thread.join()
117116
self._thread = None
118117

119-
def _send(self, frame) -> None:
118+
def _send(self, frame: np.ndarray) -> None:
120119
self._queue.put(speech.StreamingRecognizeRequest(audio_content=frame.tobytes()))
121120

122121
def reset(self) -> None:

spokestack/asr/keyword/tflite.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
from an audio stream.
44
"""
55
import os
6-
from typing import List
6+
from typing import Any, List
77

8-
import numpy as np # type: ignore
8+
import numpy as np
99

1010
from spokestack.context import SpeechContext
1111
from spokestack.models.tensorflow import TFLiteModel
@@ -34,7 +34,7 @@ def __init__(
3434
fft_hop_length: int = 10,
3535
model_dir: str = "",
3636
posterior_threshold: float = 0.5,
37-
**kwargs
37+
**kwargs: Any
3838
) -> None:
3939

4040
self.classes = classes
@@ -96,7 +96,7 @@ def __init__(
9696
self._prev_sample: float = 0.0
9797
self._is_active = False
9898

99-
def __call__(self, context: SpeechContext, frame) -> None:
99+
def __call__(self, context: SpeechContext, frame: np.ndarray) -> None:
100100

101101
self._sample(context, frame)
102102

@@ -105,7 +105,7 @@ def __call__(self, context: SpeechContext, frame) -> None:
105105

106106
self._is_active = context.is_active
107107

108-
def _sample(self, context: SpeechContext, frame) -> None:
108+
def _sample(self, context: SpeechContext, frame: np.ndarray) -> None:
109109
# convert the PCM-16 audio to float32 in (-1.0, 1.0)
110110
frame = frame.astype(np.float32) / (2 ** 15 - 1)
111111
frame = np.clip(frame, -1.0, 1.0)
@@ -138,7 +138,7 @@ def _analyze(self, context: SpeechContext) -> None:
138138
# compute mel spectrogram
139139
self._filter(context, frame)
140140

141-
def _filter(self, context: SpeechContext, frame) -> None:
141+
def _filter(self, context: SpeechContext, frame: np.ndarray) -> None:
142142
# add the batch dimension and compute the mel spectrogram with filter model
143143
frame = np.expand_dims(frame, 0)
144144
frame = self.filter_model(frame)[0]

spokestack/asr/spokestack/cloud_client.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
import json
99
from typing import Any, Dict, List, Union
1010

11-
import numpy as np # type: ignore
12-
from websocket import WebSocket # type: ignore
11+
import numpy as np
12+
from websocket import WebSocket
1313

1414

1515
class CloudClient:
@@ -137,7 +137,7 @@ def disconnect(self) -> None:
137137
self._socket.close()
138138
self._socket = None
139139

140-
def send(self, frame: np.ndarray):
140+
def send(self, frame: np.ndarray) -> None:
141141
"""sends a single frame of audio
142142
143143
Args:
@@ -149,14 +149,14 @@ def send(self, frame: np.ndarray):
149149
else:
150150
raise ConnectionError("Not Connected")
151151

152-
def end(self):
152+
def end(self) -> None:
153153
""" sends empty string in binary to indicate last frame """
154154
if self._socket:
155155
self._socket.send_binary(b"")
156156
else:
157157
raise ConnectionError("Not Connected")
158158

159-
def receive(self):
159+
def receive(self) -> None:
160160
""" receives the api response """
161161
if self._socket:
162162
timeout = self._socket.timeout
@@ -191,7 +191,7 @@ def idle_count(self) -> int:
191191
return self._idle_count
192192

193193
@idle_count.setter
194-
def idle_count(self, value: int):
194+
def idle_count(self, value: int) -> None:
195195
""" sets the idle counter"""
196196
self._idle_count = value
197197

spokestack/asr/spokestack/speech_recognizer.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
the speech pipeline
44
"""
55
import logging
6+
from typing import Any
67

7-
import numpy as np # type: ignore
8+
import numpy as np
89

910
from spokestack.asr.spokestack.cloud_client import CloudClient
1011
from spokestack.context import SpeechContext
1112

12-
1313
_LOG = logging.getLogger(__name__)
1414

1515

@@ -33,7 +33,7 @@ def __init__(
3333
sample_rate: int = 16000,
3434
frame_width: int = 20,
3535
idle_timeout: int = 5000,
36-
**kwargs,
36+
**kwargs: Any,
3737
) -> None:
3838

3939
self._client: CloudClient = CloudClient(
@@ -78,10 +78,10 @@ def _begin(self) -> None:
7878
self._is_active = True
7979
self._client.idle_count = 0
8080

81-
def _send(self, frame) -> None:
81+
def _send(self, frame: np.ndarray) -> None:
8282
self._client.send(frame)
8383

84-
def _receive(self, context):
84+
def _receive(self, context: SpeechContext) -> None:
8585
self._client.receive()
8686
hypotheses = self._client.response.get("hypotheses")
8787
if hypotheses:

0 commit comments

Comments
 (0)