fix: accept path-like objects in tofile and validate weight format before writing#381
Open
henryiii wants to merge 1 commit into
Open
fix: accept path-like objects in tofile and validate weight format before writing#381henryiii wants to merge 1 commit into
henryiii wants to merge 1 commit into
Conversation
…fore writing Bug 1: _open_write_file called filepath.endswith(...) which raises AttributeError for pathlib.Path objects. Fixed by converting to str via os.fsdecode(os.fspath(filepath)) before the suffix check. Widened the type annotation on _open_write_file and LesHouchesEvents.tofile from str to the existing PathLike alias to match the reading side (fromfile). Bug 2: LesHouchesEvents.write() validated nothing up-front, so calling tofile(path, weights=True) with the default rwgt=True would raise only when the first event was serialized — after the root tag, header, and init block had already been written, leaving a partial file on disk. Fixed by adding the same rwgt+weights mutual-exclusion check at the top of both write() and tofile(), before any I/O is performed. Assisted-by: ClaudeCode:claude-sonnet-4-6
🏎️ Benchmark Comparison |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #381 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 2 2
Lines 608 615 +7
=========================================
+ Hits 608 615 +7
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🤖 AI text below 🤖
Summary
Bug 1 (tofile + pathlib.Path):
_open_write_filecalledfilepath.endswith(...)which raisesAttributeErrorforpathlib.Pathobjects. Fixed by converting viaos.fsdecode(os.fspath(filepath))before the suffix check, and widened the type annotations on_open_write_fileandLesHouchesEvents.tofilefromstrto the existingPathLikealias (matching the reading side).Bug 2 (partial-file on validation failure):
LesHouchesEvents.write()performed no up-front validation, so callingtofile(path, weights=True)with the defaultrwgt=TrueraisedValueErroronly when the first event was serialized — after the root tag, header, and init block had already been written, leaving a partial/corrupt file on disk. Fixed by adding therwgt and weightsmutual-exclusion check at the top of bothwrite()andtofile(), before any I/O.Part of #378
Test plan
test_tofile_accepts_pathlib_path— writes viapathlib.Path, reads back, verifies 1 eventtest_tofile_accepts_pathlib_path_gz— writes.lhe.gzviapathlib.Path, checks gzip magic, reads back via pylhetest_write_raises_before_writing_when_both_formats_given— asserts target file does NOT exist after the failedtofile()calltest_write_stream_raises_before_writing_when_both_formats_given— assertsStringIOis empty after the failedwrite()call🤖 Generated with Claude Code