Improve and bug fix _validate_bids_directory #323
Improve and bug fix _validate_bids_directory #323candleindark wants to merge 5 commits intocon:mainfrom
_validate_bids_directory #323Conversation
The name of `path` is more appropriate since there is no guarantee that it is a path to a directory
_validate_bids_directory _validate_bids_directory
There was a problem hiding this comment.
Pull request overview
This PR improves the _validate_bids_directory function to correctly handle edge cases where paths or their parents point to files rather than directories. The previous implementation had bugs that would either produce unclear errors or incorrectly pass validation in these scenarios.
Changes:
- Fixed validation logic to properly distinguish between directories and files for both the target path and its parent
- Improved error messages to provide clearer feedback about validation failures
- Enhanced the docstring with comprehensive documentation of validation criteria, parameters, return values, and exceptions
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #323 +/- ##
==========================================
+ Coverage 84.80% 84.88% +0.08%
==========================================
Files 39 39
Lines 1494 1502 +8
==========================================
+ Hits 1267 1275 +8
Misses 227 227
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
6e98902 to
64ef0b8
Compare
So that the case of a given path that points to a file is handled properly
8778356 to
6926073
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
6926073 to
398368d
Compare
| if not path.parent.is_dir(): | ||
| if not path.parent.exists(): | ||
| raise ValueError(f"The parent path ({path.parent}) does not exist.") |
There was a problem hiding this comment.
How does this make any sense? At this point you have passed the check that the path exists, so how could it exist without a parent?
There was a problem hiding this comment.
At this point, path is actually confirmed to not exist.
The following demo the case that the logic is handling.
from pathlib import Path
from nwb2bids._core._validate_existing_bids import _validate_bids_directory
p = Path("some_missing_parent/child")
try:
_validate_bids_directory(p)
except ValueError as e:
print(e)
"""
The parent path (some_missing_parent) does not exist.
"""
| if path.is_dir(): | ||
| return _validate_existing_directory_as_bids(path) | ||
| if path.exists(): | ||
| raise ValueError(f"The path ({path}) exists but is not a directory.") |
There was a problem hiding this comment.
These checks might make more sense re-ordered as follows:
check the parent properties -> check the current path is not a file -> do the full BIDS check
There was a problem hiding this comment.
I am really just building on the existing implementation,
nwb2bids/src/nwb2bids/_core/_validate_existing_bids.py
Lines 5 to 13 in 972adeb
While what you are suggesting here seems reasonable, the implementation proposed by this PR is more or less in the reverse order of what you are suggesting, trigger logic to handle a problem only when it occurs instead of checking preemptively like what you may be suggesting.
The two additional situations mentioned above are demonstrated by the two added tests https://github.com/candleindark/nwb2bids/blob/398368d5c8a9f5f913284a08a4176a5543ba2e2c/tests/unit/test_run_config.py#L37-L59.
| if not path.parent.is_dir(): | ||
| if not path.parent.exists(): |
There was a problem hiding this comment.
I'm pretty sure that path.parent.is_file() also checks for existence
There was a problem hiding this comment.
Yes, it does, but I am not just checking if path.parent is a file. I have raise ValueError(f"The parent path ({path.parent}) exists but is not a directory.") in the outer if-statement as well.
| - it points to an existing BIDS directory, which contains a valid `dataset_description.json` file. | ||
| - it points to an empty directory, in which case a minimal `dataset_description.json` file will be | ||
| created and added to the directory. | ||
| - it does not point to an object in the file system, but its parent exists as a directory. |
There was a problem hiding this comment.
Remind me why we care about the parent at all?
There was a problem hiding this comment.
This is an adherence to the previous documentation and implementation,
.
This PR improves
_validate_bids_directory, which is used to validate thebids_directoryfield in theRunConfigmodel.Particularly,