Skip to content

Add safe YAML file reading and writing - #196

Open
Gonghan-Princess wants to merge 3 commits into
fabiocaccamo:mainfrom
Gonghan-Princess:codex/feat-safe-yaml-files
Open

Gonghan-Princess wants to merge 3 commits into
fabiocaccamo:mainfrom
Gonghan-Princess:codex/feat-safe-yaml-files

Conversation

@Gonghan-Princess

Copy link
Copy Markdown
Contributor

Summary

Fixes #43.

  • Add public read_file_yaml and write_file_yaml helpers for a single YAML document, using PyYAML's safe loader and dumper.
  • Keep PyYAML optional through python-fsutil[yaml] and a lazy dependency check with an actionable installation hint; importing fsutil does not require PyYAML.
  • Support explicit text encoding, existing atomic-write behavior, and safe-dumper formatting options.
  • Serialize before opening the destination so unsupported objects or invalid dumper arguments cannot truncate an existing file. Reject custom output streams.
  • Document installation, usage, and limitations in the README and changelog.

Validation

Tested on Windows with Python 3.12.10 and PyYAML 6.0.3:

  • 32 YAML tests covering round trips, UTF-8/UTF-16, atomic and ordinary writes, formatting options, empty files, malformed/multiple documents, rejection of Python object tags, missing optional dependency, and preservation of existing files on serialization errors.
  • Full pytest suite: 197 passed, 3 skipped; 98.39% coverage (90% required). The skips are existing Windows-specific tests.
  • python -m pre_commit run --all-files --show-diff-on-failure: all configured checks passed, including mypy.
  • Built a wheel with python setup.py bdist_wheel, checked its conditional PyYAML dependency and source contents, and ran the README examples against the built wheel.
  • Verified pristine/modified/rollback behavior with the same YAML round-trip probe. Rollback restored the exact baseline archive hash, and the patch reconstructed the modified archive exactly.

The local environment has no pip module, so the initial pip-wheel command could not run; the wheel was subsequently built successfully with setuptools. Existing setuptools deprecation warnings remain. Other operating systems and Python versions were not run locally.

AI assistance

AI assisted with implementation, regression tests, validation, and this description. The contributor remains responsible for this submission. No third-party source code was copied; PyYAML is declared as an optional runtime dependency.

fabiocaccamo
fabiocaccamo previously approved these changes Oct 1, 2026

@fabiocaccamo fabiocaccamo left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Gonghan-Princess thank you for this PR. I updated the requirements to avoid an error in the CI, but there is still a failing test, could you fix it please?

@fabiocaccamo fabiocaccamo added the enhancement New feature or request label Oct 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

Status: In Progress

Development

Successfully merging this pull request may close these issues.

Add yaml files operation support.

2 participants