Fix roman.RomanNumeral Key cache colliding across modes#1906
Merged
Conversation
The _keyCache in roman.py was keyed by Key.tonicPitchNameWithCase, which
only encodes major (upper case) vs minor (lower case). Any other mode
(lydian, dorian, etc.) falls back to the upper-case tonic, so a C lydian
Key was cached under 'C' and collided with C major: after creating a
RomanNumeral in C lydian, a later RomanNumeral('I', 'C') returned the
cached lydian Key instead of C major.
Add a _keyCacheKey() helper used at both cache-store sites: major/minor
keep the case-only form so fast string lookups still hit, and any other
mode is qualified with its mode name (e.g. 'C lydian') so it cannot be
returned for a C major request. Add deterministic regression tests that
clear the cache and check both orderings and the resulting cache keys.
AI-assisted (Claude)
Member
Author
|
Bug since at least 2015. Maybe older |
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.
Creating a
RomanNumeralin a non-major/minor mode (like Lydian or Dorian) poisons the cache for the major key (Lydian) or minor (Dorian, Phyrgian, etc.) on the same tonic.After creating
RomanNumeral('I', key.Key('C', 'lydian')), a subsequentRomanNumeral('I', 'C')returns a C lydian key instead of C major.Cause
roman.py's_keyCachewas keyed byKey.tonicPitchNameWithCase, which only distinguishes major (upper case) from minor (lower case). Every other mode falls back to the upper-case tonic, soC lydianwas stored under'C'and collided withC major.Fix
Add a
_keyCacheKey()helper used at both cache-store sites:'C','c') so existing fast string lookups still hit.'C lydian') so it can never be returned for aC majorrequest.AI-assisted (Claude)