|
| 1 | +--- |
| 2 | +name: writing-docs |
| 3 | +description: >- |
| 4 | + House style for docstrings, code comments, and `Changed in`/`New in` version |
| 5 | + markers in music21. Use whenever you write or edit a docstring or comment, add |
| 6 | + a version marker, or decide where a bug fix's test belongs. Covers the length |
| 7 | + target, the rule against narrating bugs you just fixed, and why regression |
| 8 | + cases go in unittests rather than doctests. |
| 9 | +--- |
| 10 | + |
| 11 | +# Writing docs and comments |
| 12 | + |
| 13 | +## Length |
| 14 | + |
| 15 | +Aim for about 40% of the length an LLM writes by default. Cut qualifiers, |
| 16 | +restatements, and the sentence that explains the sentence before it. A comment |
| 17 | +earning its place says something the code cannot. |
| 18 | + |
| 19 | +## Say what is, not what was or what not to do |
| 20 | + |
| 21 | +Describe current behavior. Do not narrate the bug you just fixed, the old |
| 22 | +spelling of an API, or when upstream changed something. |
| 23 | + |
| 24 | +```python |
| 25 | +# yes |
| 26 | +# ligature brackets; manual beams are plain [ and ] |
| 27 | +return self.backslash + '[ ' |
| 28 | + |
| 29 | +# no |
| 30 | +# \[ used to be emitted for beams, which was wrong -- LilyPond renamed |
| 31 | +# this in 2.16 and it silently produced nothing |
| 32 | +``` |
| 33 | + |
| 34 | +The commit message is where a fixed bug belongs: why it was wrong, how it was |
| 35 | +found, what it broke. That costs nothing until someone runs `git log`, and |
| 36 | +`git blame` leads them there from the line itself. |
| 37 | + |
| 38 | +The exception is a mistake that is likely to recur — a genuine trap that the |
| 39 | +next person would otherwise walk into. Rare. Prefer stating the rule positively |
| 40 | +even then. |
| 41 | + |
| 42 | +## Version markers |
| 43 | + |
| 44 | +`* Changed in v[X]: one line.` or `* New in v[X].` Only for user-facing changes |
| 45 | +to the public interface: a signature, a return type, an output format. Keep to |
| 46 | +what a reader must act on; drop the before-picture. |
| 47 | + |
| 48 | +``` |
| 49 | +* Changed in v11: `stringOutput()` always returns a `str`, never None. |
| 50 | +* Changed in v11: emits `\tuplet`; the arguments are now actual, normal. |
| 51 | +* New in v11. |
| 52 | +``` |
| 53 | + |
| 54 | +A plain bug fix — code now does what it always claimed — gets no marker and no |
| 55 | +doctest. It goes in the commit message. |
| 56 | + |
| 57 | +See the `bump-version` skill for which digit to change and the odd/even |
| 58 | +convention. |
| 59 | + |
| 60 | +## Doctests are not regression tests |
| 61 | + |
| 62 | +Doctests are documentation that happens to be verified. Every example must earn |
| 63 | +its place by teaching the reader something about how to use the object. |
| 64 | + |
| 65 | +Regression cases go in the module's `Test(unittest.TestCase)` class, where a |
| 66 | +comment or the method name can name the issue: |
| 67 | + |
| 68 | +```python |
| 69 | +def testMetronomeMarkWrittenInStream(self): |
| 70 | + # https://github.com/cuthbertLab/music21/issues/1852 |
| 71 | + ... |
| 72 | +``` |
| 73 | + |
| 74 | +So: a fix for a crash on an edge case, a check that some input no longer |
| 75 | +produces invalid output, an assertion tied to an issue number — unittest. An |
| 76 | +example a user would want to read — doctest. |
| 77 | + |
| 78 | +Naming the guarded bug **is** appropriate in a unittest; that is what the test |
| 79 | +is for. The rule against narrating old bugs applies to docstrings and to |
| 80 | +comments in shipping code, not to tests. |
0 commit comments