Raise TypeError on malformed MIDI messages instead of silently dropping them - #507
Open
IshaanPotle wants to merge 1 commit into
Open
Raise TypeError on malformed MIDI messages instead of silently dropping them#507IshaanPotle wants to merge 1 commit into
IshaanPotle wants to merge 1 commit into
Conversation
normalize_midi_messages() iterates with if/elif and no else branch, so any message it cannot interpret is skipped. The caller gets a shorter list back with no indication that events were lost, which surfaces later as missing notes in the rendered audio with no traceback to debug from. Add an else branch that raises TypeError naming the offending index and value, and tests covering the shapes that were previously dropped: 1-tuples, 3-tuples, bare bytes, None and bare ints. Fixes spotify#489.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #489.
The problem
normalize_midi_messages()iterates withif/elifand noelse, so any message it can't interpret is skipped:The caller gets a shorter list back with no indication events were lost. In practice this surfaces much later as missing notes in the rendered audio, with no traceback to debug from.
Reproduced on
masterbefore the fix — every one of these silently loses a message:The change
An
elsebranch that raisesTypeErrornaming the offending index and value:Plus tests for the five shapes that were previously dropped, and one asserting valid input is unaffected.
Note on compatibility
This turns previously-silent behavior into an exception, so it's technically a behavior change — code passing malformed messages today gets an error instead of quietly wrong audio. That seemed like the intent of the issue, but happy to switch it to a
warnings.warn()instead if you'd rather keep it non-breaking for a release cycle.Test log
ruff checkreports the same 12 pre-existing findings before and after this change; I left those alone to keep the diff focused.Disclosure: I used an AI assistant while working on this. I reviewed the change, verified the failing/passing behavior locally, and can speak to any line of it.