From 93d38423456b94513c91289041877feeb53e8e49 Mon Sep 17 00:00:00 2001 From: Michael Scott Asato Cuthbert Date: Thu, 6 Aug 2026 10:20:17 -1000 Subject: [PATCH 1/2] fix: honor displayType 'always' when nothing precedes the pitch Pitch.updateAccidentalDisplay checked cautionaryAll and a displayType of 'always'/'even-tied' only after the `if not pitchPastAll:` early return, so the first pitch of a part -- the one case with nothing in the past -- never reached it and fell through to the key-signature branch instead. An explicitly requested accidental was silently switched off whenever the key signature made it redundant, and since MusicXML export gates on displayStatus, it was dropped from the exported file entirely. Hoist the block above the early return. When pitchPastAll is non-empty this is a no-op: the only thing it now precedes is the conflict loop, whose two outcomes (conflicting alteration -> True, same name -> break into this block -> True) both already ended in True. Port of the same fix in music21j (cuthbertLab/music21j#325). Version bumped to invalidate cached pickles: the musedata parser sets displayType='always', so cached scores can hold the old display statuses. AI-assisted (Claude) --- music21/_version.py | 2 +- music21/base.py | 2 +- music21/pitch.py | 34 ++++++++++++++++++-------- music21/test/test_pitch.py | 49 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 75 insertions(+), 12 deletions(-) diff --git a/music21/_version.py b/music21/_version.py index d45d6c0cd..240ab9a1f 100644 --- a/music21/_version.py +++ b/music21/_version.py @@ -47,7 +47,7 @@ ''' from __future__ import annotations -__version__ = '11.0.0b7' +__version__ = '11.0.0b8' def get_version_tuple(vv): v = vv.split('.') diff --git a/music21/base.py b/music21/base.py index 85ae8bbe7..3a3c407d2 100644 --- a/music21/base.py +++ b/music21/base.py @@ -26,7 +26,7 @@ >>> music21.VERSION_STR -'11.0.0b7' +'11.0.0b8' Alternatively, after doing a complete import, these classes are available under the module "base": diff --git a/music21/pitch.py b/music21/pitch.py index 8d909b8d4..2830c9529 100644 --- a/music21/pitch.py +++ b/music21/pitch.py @@ -4984,7 +4984,20 @@ def updateAccidentalDisplay( >>> a4.accidental is None True + `cautionaryAll` and a `displayType` of 'always' or 'even-tied' are honored + even when nothing precedes the pitch, and regardless of the key signature: + + >>> fSharp = pitch.Pitch('F#4') + >>> fSharp.accidental.displayType = 'always' + >>> fSharp.updateAccidentalDisplay(alteredPitches=key.Key('G').alteredPitches) + >>> fSharp.accidental.displayStatus + True + v8 -- made keyword-only and added `otherSimultaneousPitches`. + + * Changed in v11: `cautionaryAll` and `displayType` of 'always'/'even-tied' + are honored for the first pitch of a part (previously an empty `pitchPast` + fell through to key-signature logic and could turn the accidental off). ''' # N.B. -- this is a very complex method # do not alter it without significant testing. @@ -5048,6 +5061,17 @@ def set_displayStatus(newDisplayStatus: bool) -> None: set_displayStatus(True) return + # here tied and always are treated the same; we assume that + # making ties sets the displayStatus, and thus we would not be + # overriding that display status here + if (cautionaryAll is True + or (acc is not None + and acc.displayType in ('even-tied', 'always'))): + # show all accidentals, no matter what is in the past + # or in the key signature + set_displayStatus(True) + return # do not search past + # no pitches in the past list if not pitchPastAll: # if we have no past, we show the accidental if this pitch name @@ -5089,16 +5113,6 @@ def set_displayStatus(newDisplayStatus: bool) -> None: break # nope, no conflicting accidentals at this name and octave in the past - # here tied and always are treated the same; we assume that - # making ties sets the displayStatus, and thus we would not be - # overriding that display status here - if (cautionaryAll is True - or (acc is not None - and acc.displayType in ('even-tied', 'always'))): - # show all accidentals, even if past encountered - set_displayStatus(True) - return # do not search past - # store if a match was found and display set from past pitches setFromPitchPast = False diff --git a/music21/test/test_pitch.py b/music21/test/test_pitch.py index 80cfedfe0..309fe3f3c 100644 --- a/music21/test/test_pitch.py +++ b/music21/test/test_pitch.py @@ -396,6 +396,55 @@ def testOverrideDisplayStatus(self): n.pitch.updateAccidentalDisplay(overrideStatus=True, alteredPitches=k.alteredPitches) self.assertIs(n.pitch.accidental.displayStatus, False) + def testDisplayTypeAlwaysWithNothingInPast(self): + ''' + `displayType='always'` and `cautionaryAll` win over the key signature + even for the first pitch of a part, where nothing precedes it. + + AI-assisted (Claude). + ''' + p = Pitch('An2') + p.accidental.displayType = 'always' + p.updateAccidentalDisplay() + self.assertIs(p.accidental.displayStatus, True) + + # a sharp already implied by the key signature still shows + p = Pitch('F#4') + p.accidental.displayType = 'always' + p.updateAccidentalDisplay(alteredPitches=key.Key('G').alteredPitches) + self.assertIs(p.accidental.displayStatus, True) + + # cautionaryAll creates the natural that was not there + p = Pitch('B4') + p.updateAccidentalDisplay(cautionaryAll=True) + self.assertEqual(p.accidental.name, 'natural') + self.assertIs(p.accidental.displayStatus, True) + + # 'even-tied' behaves the same as 'always' here + p = Pitch('E-3') + p.accidental.displayType = 'even-tied' + p.updateAccidentalDisplay(alteredPitches=key.Key('E-').alteredPitches) + self.assertIs(p.accidental.displayStatus, True) + + # in a stream, every note of the part shows, not just the ones after the first + s = converter.parse('tinyNotation: AAn2 Fn') + for n in s.recurse().notes: + n.pitch.accidental.displayType = 'always' + s.makeAccidentals(inPlace=True) + self.assertEqual([n.pitch.accidental.displayStatus for n in s.recurse().notes], + [True, True]) + + # regression guard: a normal displayType is untouched by the above + p = Pitch('F#4') + p.updateAccidentalDisplay(alteredPitches=key.Key('G').alteredPitches) + self.assertIs(p.accidental.displayStatus, False) + + # regression guard: with a non-empty past, repeated pitches are unchanged + past = [Pitch('c#4')] + p = Pitch('c#4') + p.updateAccidentalDisplay(pitchPast=past) + self.assertIs(p.accidental.displayStatus, False) + def testImplicitToExplicitNatural(self): p = converter.parse('tinyNotation: 2/4 f4 fn4') last_note = p.recurse().notes.last() From 58ab28e2d5fd75981dd4c593765848a0a0da9b7c Mon Sep 17 00:00:00 2001 From: Michael Scott Asato Cuthbert Date: Thu, 6 Aug 2026 10:29:45 -1000 Subject: [PATCH 2/2] docs: drop the doctest and version marker from updateAccidentalDisplay Doctests demonstrate future usage, not bug fixes -- regression coverage belongs in test_pitch.py, where testDisplayTypeAlwaysWithNothingInPast already covers this. A simple bug fix does not warrant a "Changed in vXX" marker either. Also: the previous commit's stated reason for the version bump was wrong. It claimed cached musedata pickles could hold stale display statuses, but musedata/__init__.py sets displayStatus=True alongside displayType='always', so updateAccidentalDisplay returns at the existing displayStatus check well before the hoisted block, and makeAccidentals does not pass overrideStatus. No parsed output changes and no cache is invalidated. The bump to 11.0.0b8 stands only on the ordinary bug-fix rule. AI-assisted (Claude) --- music21/pitch.py | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/music21/pitch.py b/music21/pitch.py index 2830c9529..40680a092 100644 --- a/music21/pitch.py +++ b/music21/pitch.py @@ -4984,20 +4984,7 @@ def updateAccidentalDisplay( >>> a4.accidental is None True - `cautionaryAll` and a `displayType` of 'always' or 'even-tied' are honored - even when nothing precedes the pitch, and regardless of the key signature: - - >>> fSharp = pitch.Pitch('F#4') - >>> fSharp.accidental.displayType = 'always' - >>> fSharp.updateAccidentalDisplay(alteredPitches=key.Key('G').alteredPitches) - >>> fSharp.accidental.displayStatus - True - v8 -- made keyword-only and added `otherSimultaneousPitches`. - - * Changed in v11: `cautionaryAll` and `displayType` of 'always'/'even-tied' - are honored for the first pitch of a part (previously an empty `pitchPast` - fell through to key-signature logic and could turn the accidental off). ''' # N.B. -- this is a very complex method # do not alter it without significant testing.