lily: add typing. fix old/invalid LilyPond output - #1997
Merged
Conversation
Annotate every __init__, method and non-obvious local in lilyObjects.py and translate.py. stringOutput() is now `-> str` on all ~90 grammar classes. Eight subclasses returned None for empty contents, which is unrepresentable in an override and was also a latent crash: LyObject.__str__ does stringOutput().replace(...), so str(LyBookBody()) raised AttributeError. They return '' now. In translate.py the .classes string dispatch in loadFromMusic21Object and appendM21ObjectToContext became isinstance checks -- that is what lets mypy verify the dispatch -- and self.context.contents access moved behind contextContents()/setContextContents(), since context is legitimately polymorphic (LyLilypondTop, LyMusicList, LyCompositeMusic). LilyPond correctness, each checked against an installed LilyPond 2.24.4 rather than from memory. All four produced silently wrong or unparseable output: - barlineDict: 'dotted' was ":", 'dashed' was "dashed", 'heavy-heavy' was ".|.", and the repeats were "|:"/":|". None of those are defined in 2.24's bar-line.scm, and LilyPond draws an unknown bar name as nothing at all with no warning. Now ";", "!", "..", ".|:", ":|.". 'short' was commented out entirely, so a `short` barline raised KeyError; mapped to ",". All 11 entries of bar.barTypeList now round-trip. - \markuplines was renamed \markuplist in 2.16; the old name is a hard parse error today. - LyTempoRange joined a range with "~", a tie: `\tempo 4 = 70~100` fails with "not a duration" plus an unterminated-tie warning. Uses "-". - LyModeChangingHead emitted \notes for mode='note', which has never existed. Always \notemode for that mode; the other four shorthands are valid. Not changed, having verified they still work in 2.24: the pre-2.18 (parser location) music-function signatures in fictaDef/colorDef, and \times (deprecated for \tuplet but still valid). \[ \] and \~ are faithful to the grammar this file follows -- ligature brackets and the pes-or-flexa event -- so those only gained comments. Three bugs surfaced by the annotations: - LyOutputDefBody.stringOutput had an inverted guard: it raised when outputDefBody was set and dereferenced None when it was not. - LyMarkupBracedListBody.stringOutput called super().__init__(), resetting the object mid-render. - LilypondConverter.__init__ set self.LILYEXEC = None after setupTools() had already populated it. No version bump: lily is output-only, so no parse caches are invalidated. AI-assisted (Claude)
Folds in Emerson936's MetronomeMark work from PR #1993, adapted to the isinstance dispatch and the annotations on this branch, and finishes the job that branch started: bringing the emitted syntax up from the v2.14 grammar the module was written against to LilyPond 2.24 (December 2022). State the target version. lilyObjects' module docstring now says output is written for and checked against 2.24, that constructs from the linked v2.14 grammar are not necessarily still valid, and that convert-ly's convertrules.py is the authoritative list of what changed. Policy: support back about four years from a release, so raise the target when 2.24 falls outside that. Remaining pre-2.24 syntax, found by running LilyPond's own convert-ly over generated output declared as \version "2.10.0" and confirmed against a 2.24.4 run: - \times n/d -> \tuplet d/n (rule 2.17.11). The fraction inverts: \times 2/3 is \tuplet 3/2. setContextForTimeFraction's arguments are correspondingly swapped to (actual, normal), which is also the more natural direction for music21, whose tuplets already carry tupletActual and tupletNormal. - OctavateEight -> ClefModifier in the ossia context mods (rule 2.17.18). LilyPond does not complain about an \override on an unknown grob name, so this had been silently doing nothing. - Drop \consists "Default_bar_line_engraver" (rule 2.23.8); the engraver no longer exists and 2.24 warns twice per ossia staff. - define-music-function (parser location ...) -> (...) in fictaDef and colorDef (rule 2.19.22). 2.24 still accepts the old form, but convert-ly rewrites it. Not taken: convert-ly's 2.23.11 rule rewrites \bar ".|" and ".|:" to ".|-|" and ".|:-|", restoring the implicit end-of-line "|" those names lost in 2.23. The plain names are what 2.24 itself uses for startRepeatBarType and endRepeatBarType in engraver-init.ly, so the new behavior is the intended one and the suffixed forms are a compatibility shim for older files. convert-ly from 2.10 to 2.24 now reports no changes for generated output. AI-assisted (Claude)
Member
Author
|
(Attention @jeremy9959 -- if you have any time, I'd appreciate your thumbs up or down or comments before merging) |
AI-assisted (Claude)
Version markers keep only what a user acts on. AI-assisted (Claude)
House style for docstrings, comments and version markers: the length target, keeping fixed bugs in the commit message rather than the code, and regression cases belonging in unittests rather than doctests. AI-assisted (Claude)
Contributor
|
I haven't looked at lilypond recently so I can't say much useful. |
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.
Annotate most methods and non-obvious locals in lilyObjects.py and translate.py. Update things that were okay in 2.12 or 2.14 but don't fit with modern (2.14 / 2.16) Lilypond grammar.
stringOutput()is now-> stron all ~90 grammar classes, neverNone(Nones are now''Replace
.classeschecks withisinstancefor better tying.use
contextContents()andsetContextContents()instead ofself.context.contents- again for better typing.Fixes for modern Lilypond:
\timesbecame\tupletbut with opposite grammar.\markuplineswas renamed\markuplistin 2.16.LyTempoRangeno longer joins ranges with "~". Now\tempo 4 = 70-100.LyModeChangingHeadfixed\notemode.define-music-functionstop using parser and location.Bugs fixed:
LyOutputDefBody.stringOutputbugsLyMarkupBracedListBody.stringOutputcalled super().init(), resetting the object mid-render.LilypondConverter.__init__setself.LILYEXEC = NoneaftersetupTools()had already populated it.Note: I plan to make .parent a strong ref (not weakref) in v11 -- most weakrefs are on the chopping block.
AI-assisted (Claude)