Skip to content

Add safe YAML file reading and writing - #196

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

Gonghan-Princess wants to merge 5 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
@fabiocaccamo

Copy link
Copy Markdown
Owner

@Gonghan-Princess any update?

@codecov

codecov Bot commented Oct 8, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.51%. Comparing base (7ed66a8) to head (24ba931).
⚠️ Report is 3 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #196      +/-   ##
==========================================
+ Coverage   98.48%   98.51%   +0.02%     
==========================================
  Files          13       13              
  Lines         794      808      +14     
==========================================
+ Hits          782      796      +14     
  Misses         12       12              
Flag Coverage Δ
unittests 98.51% <100.00%> (+0.02%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Gonghan-Princess

Copy link
Copy Markdown
Contributor Author

Hi Fabio, I'm sorry for the long delay in getting back to you. I've been busy with some other commitments recently. I've now fixed the isolated import test and the README formatting issue in commits e680dc4 and 24ba931. The latest GitHub Actions matrix is green. Thank you for your patience and for reviewing this PR.

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