agentleFS
cuthbertLabcuthbertLab/music21/.agents/skills/warnings-and-environment/SKILL.md

warnings-and-environment

Skill2.6k starsChanged 10 days agoBSD-3-ClauseView on GitHub

How music21 tells users about problems: `warnings.warn` with a module Warning class, `environLocal.warn`, and `environLocal.printDebug`. Read before adding a warning or debug message, and before writing a test or doctest that triggers one — tests must never print warnings to the console.

We split this skill into the rules it gives an agent

3 rules. About 0 are common, 0 are distinctive, and 3 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈0

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 3

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Not unified. In 2026 the code has about 73 environLocal.warn, 41 warnings.warn (some with a module class, some bare), 800 printDebug, and …

    line 24
  2. Very specific:

    When the warning is the point of the example, record it and show it.

    line 62
  3. Very specific:

    When it is incidental, hide the silencing from the docs (see writing-docs for #_DOCS_HIDE/#_DOCS_SHOW):

    line 76

≈ “Common” is an estimate: at least 1 in 5 of the 308,232 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Warnings and the Environment

Three channels

CallGoes toCan a caller catch or silence it?
warnings.warn(MusicXMLWarning('...'), stacklevel=2)Python's warnings systemYes: filters, catch_warnings, assertWarns
environLocal.warn('...')sys.stderr.write, every timeNo (only by redirecting stderr)
environLocal.printDebug('...')stderr, only when debug is onToggle with music21/test/toggleDebug.py

environLocal = environment.Environment('moduleName') at the top of the module.

Where things stand

Not unified. In 2026 the code has about 73 environLocal.warn, 41 warnings.warn (some with a module class, some bare), 800 printDebug, and no logging. We are moving slowly toward Python's warnings and logging systems. It is low priority: convert a call when you are already changing it, never as a sweep.

For new code, prefer warnings.warn for anything a caller might reasonably want to catch, silence, or test. By default Python shows each distinct message once per call site, so a message that names the object (f'{p.nameWithOctave} is ...') shows for each different object. Use environLocal.warn only when the user must hear it every time.

Never print().

Warning classes

A module with its own warnings defines one class subclassing UserWarning (MusicXMLWarning in musicxml/xmlObjects.py, TranslateWarning in midi/translate.py). Warnings are not exceptions, so they do not subclass Music21Exception.

Set stacklevel so the reported line is in the caller's code, not ours: 2 from the method the user called, more if the warning comes from a helper.

Tests

In a unittest, catch it and check it:

with self.assertWarnsRegex(MusicXMLWarning, 'C-1'):
    mxPitch = MEX.pitchToXml(pitch.Pitch('C', octave=-1))

Doctests

doctest captures only stdout, so any warning goes straight to the console of whoever runs the tests. Every doctest that triggers a warning must catch it.

When the warning is the point of the example, record it and show it. simplefilter('always') keeps Python from swallowing a repeat of a warning an earlier test already raised:

>>> import warnings
>>> with warnings.catch_warnings(record=True) as w:
...     warnings.simplefilter('always')  #_DOCS_HIDE
...     musicxml.m21ToXml.moveOctaveIntoMusicxmlRange(12, 'C12')
9
>>> w[0].message
MusicXMLWarning('C12 is out of MusicXML range; writing octave 9')

When it is incidental, hide the silencing from the docs (see writing-docs for #_DOCS_HIDE/#_DOCS_SHOW):

>>> import warnings  #_DOCS_HIDE
>>> with warnings.catch_warnings():  #_DOCS_HIDE
...     warnings.simplefilter('ignore')  #_DOCS_HIDE
...     out = midi.translate.timeSignatureToMidiEvents(ts)  #_DOCS_HIDE
>>> #_DOCS_SHOW out = midi.translate.timeSignatureToMidiEvents(ts)

catch_warnings cannot stop environLocal.warn. If a doctest has to trigger one, wrap it in contextlib.redirect_stderr(io.StringIO()).

Download now (ZIP)

Trending

More talk lately.about the whole repository

Dec 2025: 0 mentionsJan 2026: 0 mentionsFeb 2026: 0 mentionsMar 2026: 0 mentionsApr 2026: 0 mentionsMay 2026: 0 mentionsJun 2026: 1 mentionJul 2026: 2 mentionsAug 2026: 1 mentionSep 2026: 2 mentionsOct 2026: 7 mentionsNov 2026: 0 mentions
Dec 2025Jun 2026Nov 2026
Mentions a month of the cuthbertLab/music21 repository that our weekly crawl found. There is no adoption data for this file yet.

Who's talking about cuthbertLab/music21

The biggest 5 of 36 mentions linking to the repository that our crawl found. Each line is their own headline.

See all 36

Talked about on GitHub and Stack Overflow (36 times).

On GitHub · 29

24 more on GitHub

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.