Summary
Reassess Python's shipped inline-frontmatter representation now that per-line hash-comment frontmatter is available. The current triple-quoted representation can change Python module semantics when frontmatter is created in an existing file.
Python-specific concerns
Module docstrings
The first string-literal statement becomes the module docstring and is exposed as module.doc. Creating triple-quoted YAML before an existing module docstring causes the metadata string to become module.doc and demotes the intended documentation to an unused string expression.
Future imports
Python permits future imports after the module docstring and comments. Inserting a metadata string before an existing module docstring creates a second string expression before the future import, which can make an otherwise valid file fail with a future-import placement SyntaxError.
Representative input:
"""Actual module documentation."""
from __future__ import annotations
Encoding declarations and shebangs
Python encoding declarations are recognized only in the legal leading-line positions, and executable scripts may also begin with a shebang. Frontmatter creation must preserve those semantics and positions.
Stub and tooling behavior
Python and Pyi files may be inspected by documentation generators, type checkers, and other tools that assign meaning to module docstrings. Frontmatter should not unintentionally replace that documentation surface.
Proposed direction
Prefer per-line hash-comment frontmatter for newly created Python and Pyi metadata:
# ---
# title: Example
# description: Demonstrates inline metadata.
# ---
"""Actual module documentation."""
Changing the shipped mapping outright would strand existing python-docstring frontmatter, so design a backward-compatible migration:
- Continue recognizing and mutating existing triple-quoted frontmatter.
- Create new Python frontmatter using the hash-comment representation.
- Define deterministic behavior if both representations are present.
- Preserve shebangs, encoding declarations, line endings, and surrounding source.
- Keep representation selection and rewriting inside md-utils so consumers only get and set decoded metadata.
Acceptance criteria
Related work
Summary
Reassess Python's shipped inline-frontmatter representation now that per-line hash-comment frontmatter is available. The current triple-quoted representation can change Python module semantics when frontmatter is created in an existing file.
Python-specific concerns
Module docstrings
The first string-literal statement becomes the module docstring and is exposed as module.doc. Creating triple-quoted YAML before an existing module docstring causes the metadata string to become module.doc and demotes the intended documentation to an unused string expression.
Future imports
Python permits future imports after the module docstring and comments. Inserting a metadata string before an existing module docstring creates a second string expression before the future import, which can make an otherwise valid file fail with a future-import placement SyntaxError.
Representative input:
Encoding declarations and shebangs
Python encoding declarations are recognized only in the legal leading-line positions, and executable scripts may also begin with a shebang. Frontmatter creation must preserve those semantics and positions.
Stub and tooling behavior
Python and Pyi files may be inspected by documentation generators, type checkers, and other tools that assign meaning to module docstrings. Frontmatter should not unintentionally replace that documentation surface.
Proposed direction
Prefer per-line hash-comment frontmatter for newly created Python and Pyi metadata:
Changing the shipped mapping outright would strand existing python-docstring frontmatter, so design a backward-compatible migration:
Acceptance criteria
Related work