Skip to content

Commit 6ca92b4

Browse files
committed
remove asserts + other PR feedback
1 parent ca0270d commit 6ca92b4

File tree

2 files changed

+8
-17
lines changed

2 files changed

+8
-17
lines changed

adafruit_midi/__init__.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
2727
"""
2828
try:
29-
from typing import Union, Tuple, Any, List, Optional, Dict
29+
from typing import Union, Tuple, Any, List, Optional, Dict, BinaryIO
3030
except ImportError:
3131
pass
3232

@@ -58,8 +58,8 @@ class MIDI:
5858

5959
def __init__(
6060
self,
61-
midi_in: Optional[Any] = None,
62-
midi_out: Optional[Any] = None,
61+
midi_in: Optional[BinaryIO] = None,
62+
midi_out: Optional[BinaryIO] = None,
6363
*,
6464
in_channel: Optional[Union[int, Tuple[int, ...]]] = None,
6565
out_channel: int = 0,
@@ -109,8 +109,7 @@ def out_channel(self) -> int:
109109
return self._out_channel
110110

111111
@out_channel.setter
112-
def out_channel(self, channel: Optional[int]) -> None:
113-
assert channel is not None
112+
def out_channel(self, channel: int) -> None:
114113
if not 0 <= channel <= 15:
115114
raise RuntimeError("Invalid output channel")
116115
self._out_channel = channel
@@ -126,7 +125,6 @@ def receive(self) -> Optional[MIDIMessage]:
126125
# If the buffer here is not full then read as much as we can fit from
127126
# the input port
128127
if len(self._in_buf) < self._in_buf_size:
129-
assert self._midi_in is not None
130128
bytes_in = self._midi_in.read(self._in_buf_size - len(self._in_buf))
131129
if bytes_in:
132130
if self._debug:
@@ -174,5 +172,4 @@ def send(self, msg: MIDIMessage, channel: Optional[int] = None) -> None:
174172
def _send(self, packet: bytes, num: int) -> None:
175173
if self._debug:
176174
print("Sending: ", [hex(i) for i in packet[:num]])
177-
assert self._midi_out is not None
178175
self._midi_out.write(packet, num)

adafruit_midi/midi_message.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,10 @@ def register_message_type(cls) -> None:
132132
### These must be inserted with more specific masks first
133133
insert_idx = len(MIDIMessage._statusandmask_to_class)
134134
for idx, m_type in enumerate(MIDIMessage._statusandmask_to_class):
135-
assert cls._STATUSMASK is not None
136135
if cls._STATUSMASK > m_type[0][1]:
137136
insert_idx = idx
138137
break
139138

140-
assert cls._STATUS is not None
141-
assert cls._STATUSMASK is not None
142139
MIDIMessage._statusandmask_to_class.insert(
143140
insert_idx, ((cls._STATUS, cls._STATUSMASK), cls)
144141
)
@@ -147,8 +144,8 @@ def register_message_type(cls) -> None:
147144
@classmethod
148145
def _search_eom_status(
149146
cls,
150-
buf: Dict[int, bool],
151-
eom_status: bool,
147+
buf: bytearray,
148+
eom_status: Optional[int],
152149
msgstartidx: int,
153150
msgendidxplusone: int,
154151
endidx: int,
@@ -177,15 +174,14 @@ def _search_eom_status(
177174
@classmethod
178175
def _match_message_status(
179176
cls, buf: bytearray, msgstartidx: int, msgendidxplusone: int, endidx: int
180-
) -> Tuple[Optional[Any], bool, bool, bool, bool, int]:
177+
) -> Tuple[Optional[Any], int, bool, bool, bool, int]:
181178
msgclass = None
182179
status = buf[msgstartidx]
183180
known_msg = False
184181
complete_msg = False
185182
bad_termination = False
186183

187184
# Rummage through our list looking for a status match
188-
assert msgclass is not None
189185
for status_mask, msgclass in MIDIMessage._statusandmask_to_class:
190186
masked_status = status & status_mask[1]
191187
if status_mask[0] == masked_status:
@@ -265,7 +261,6 @@ def from_message_bytes(
265261
channel_match_orna = True
266262
if complete_message and not bad_termination:
267263
try:
268-
assert msgclass is not None
269264
msg = msgclass.from_bytes(midibytes[msgstartidx:msgendidxplusone])
270265
if msg.channel is not None:
271266
channel_match_orna = channel_filter(msg.channel, channel_in)
@@ -299,7 +294,6 @@ def from_message_bytes(
299294
def __bytes__(self) -> bytes:
300295
"""Return the ``bytes`` wire protocol representation of the object
301296
with channel number applied where appropriate."""
302-
assert self._STATUS is not None
303297
return bytes([self._STATUS])
304298

305299
# databytes value present to keep interface uniform but unused
@@ -312,7 +306,7 @@ def from_bytes(cls, msg_bytes: bytes) -> "MIDIMessage":
312306
representation of the MIDI message."""
313307
return cls()
314308

315-
def __str__(self):
309+
def __str__(self) -> str:
316310
"""Print an instance"""
317311
cls = self.__class__
318312
if slots := getattr(cls, "_message_slots", None):

0 commit comments

Comments
 (0)