Skip to content

lily: add typing. fix old/invalid LilyPond output - #1997

Merged
mscuthbert merged 6 commits into
masterfrom
lily-typing
Aug 7, 2026
Merged

lily: add typing. fix old/invalid LilyPond output#1997
mscuthbert merged 6 commits into
masterfrom
lily-typing

Conversation

@mscuthbert

Copy link
Copy Markdown
Member

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 -> str on all ~90 grammar classes, never None (Nones are now ''

Replace .classes checks with isinstance for better tying.

use contextContents() and setContextContents() instead of self.context.contents - again for better typing.

Fixes for modern Lilypond:

  • \times became \tuplet but with opposite grammar.
  • barlineDict: 'dotted' was ":", 'dashed' was "dashed", 'heavy-heavy' was ".|.", and the repeats were "|:"/":|". Old. Now ";", "!", "..", ".|:", ":|.". Short barlines are now supported.
  • \markuplines was renamed \markuplist in 2.16.
  • LyTempoRange no longer joins ranges with "~". Now \tempo 4 = 70-100.
  • LyModeChangingHead fixed \notemode.
  • OctavateEight became ClefModifier
  • Default_bar_line_engraver was removed
  • define-music-function stop using parser and location.

Bugs fixed:

  • LyOutputDefBody.stringOutput bugs
  • LyMarkupBracedListBody.stringOutput called super().init(), resetting the object mid-render.
  • LilypondConverter.__init__ set self.LILYEXEC = None after setupTools() 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)

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)
@mscuthbert

Copy link
Copy Markdown
Member Author

(Attention @jeremy9959 -- if you have any time, I'd appreciate your thumbs up or down or comments before merging)

Version markers keep only what a user acts on.

AI-assisted (Claude)
@coveralls

coveralls commented Aug 7, 2026

Copy link
Copy Markdown

Coverage Status

coverage: 93.326% (+0.01%) from 93.312% — lily-typing into master

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)
@jeremy9959

Copy link
Copy Markdown
Contributor

I haven't looked at lilypond recently so I can't say much useful.

@mscuthbert
mscuthbert merged commit 3146cb0 into master Aug 7, 2026
7 checks passed
@mscuthbert
mscuthbert deleted the lily-typing branch August 7, 2026 17:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants