Skip to content

Avoid StopIteration when default YAML ends with comments - #222

Draft
Imkkey wants to merge 1 commit into
beetbox:mainfrom
Imkkey:fix/restore-comments-eof
Draft

Imkkey wants to merge 1 commit into
beetbox:mainfrom
Imkkey:fix/restore-comments-eof

Conversation

@Imkkey

@Imkkey Imkkey commented Oct 18, 2026 •

Copy link
Copy Markdown

Fixes #148.

Replace the unguarded next() in restore_yaml_comments with a for/else loop that stops cleanly when a trailing comment block has no following key. Orphan trailing comments and blank lines are discarded. Column-zero comment detection remains unchanged so indented # lines inside literal or folded block scalars remain string data.

Add 18 cases covering trailing comments with and without final newlines, blank endings, comment-only/empty defaults, reordered keys, Configuration.dump() with overridden defaults, and literal/folded scalar round trips. Include an Unreleased changelog entry.

Related prior proposals: #61 by @hebote and #196 by @binggao1230, both closed without merging. This is a narrow iterator-exhaustion fix; it does not adopt broader indented-comment handling or change duplicate-key behavior.

Validation on Linux/Python 3.12.14, PyYAML 6.0.3:

  • Before the fix, 12 added regression cases raise StopIteration; scalar-preservation controls pass.
  • After: all 50 focused YAML/dump tests pass.
  • Full suite after building docs: 318 passed, one pre-existing failure in test_bump_version_applies_sequential_changelog_updates. The same fixed-underline/current-date mismatch reproduces in a clean upstream worktree and is addressed separately in Make release changelog test independent of the date #211.
  • Ruff lint/format, type checking for the package and changed test files, Sphinx HTML build with warnings as errors, documentation lint, changed-changelog formatting, and patch whitespace checks pass.
  • Full-repository mypy on Python 3.12 still reports the baseline unused-ignore in test/test_valid.py:11; reproduced on unchanged upstream. Full-tree docstrfmt 2.0.1 now passes for all seven tracked RST files plus docs/conf.py, checked individually with --ignore-cache --pyproject-config pyproject.toml --check --no-format-python-code-blocks --preserve-adornments to avoid multiprocessing. The same checks pass on the clean upstream baseline.
  • Independent differential check: outputs are unchanged for 23,405 previously successful generated inputs; 14,044 inputs previously raising StopIteration now return normally.

No Windows/other-Python run or remote CI pass is claimed. Prepared and checked with OpenAI Codex assistance; draft for maintainer review.

Stop scanning when a trailing comment block has no following key. Keep column-zero comment detection unchanged so indented block scalar contents remain data.

Add direct and Configuration.dump regressions, scalar round trips, and a changelog entry.

Fixes beetbox#148. Related prior proposals: beetbox#61 (hebote) and beetbox#196 (binggao1230); this uses a narrow iterator-loop fix rather than their broader or flag-based implementations.

This branch has not been deployed

No deployments
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.

Configuration.dump raises StopIteration if the last line is empty.

1 participant