Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add deprecation warning for validate paths argument #2024

Merged
merged 3 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- The validation methods have been updated with multiple breaking changes. @stephprince [#1911](https://github.com/NeurodataWithoutBorders/pynwb/pull/1911)
- The behavior of `pynwb.validate(io=...)` now matches the behavior of `pynwb.validate(path=...)`. In previous pynwb versions, `pynwb.validate(io=...)` did not use the cached namespaces during validation. To obtain the same behavior as in previous versions, you can update the function call to `pynwb.validate(io=..., use_cached_namespaces=False)`
- `pynwb.validate` will return only a list of validation errors instead of a tuple: (list of validation_errors, status code)
- the `pynwb.validate(path=...)` argument has been added as a replacement for `pynwb.validate(paths=[...])`, which will be deprecated in a future major release [#2024](https://github.com/NeurodataWithoutBorders/pynwb/pull/2024)
- The validate module has been renamed to `validation.py`. The validate method can be
imported using `import pynwb; pynwb.validate` or `from pynwb import validate`

Expand Down
32 changes: 31 additions & 1 deletion src/pynwb/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from hdmf.spec import NamespaceCatalog
from hdmf.build import BuildManager, TypeMap
from hdmf.utils import docval, getargs, AllowPositional
from hdmf.utils import docval, getargs, popargs, AllowPositional
from hdmf.backends.io import HDMFIO
from hdmf.validate import ValidatorMap

Expand Down Expand Up @@ -97,6 +97,13 @@
"doc": "A specific namespace to validate against.",
"default": None,
}, # Argument order is for back-compatability
{
"name": "paths",
"type": list,
"doc": ("List of NWB file paths. This argument will be deprecated in PyNWB 4.0. "
"Use 'path' instead."),
"default": None,
},
{
"name": "path",
"type": (str, Path),
Expand Down Expand Up @@ -134,6 +141,29 @@
compliance with the schema and compliance of data with NWB best practices.
"""

paths, path = popargs("paths", "path", kwargs)

if paths is not None:
warn("The 'paths' argument will be deprecated in PyNWB 4.0 "

Check warning on line 147 in src/pynwb/validation.py

View check run for this annotation

Codecov / codecov/patch

src/pynwb/validation.py#L147

Added line #L147 was not covered by tests
"Use 'path' instead. To migrate, call this function separately for "
"each path instead of passing a list.",
DeprecationWarning)

if path is not None:
raise ValueError("Both 'paths' and 'path' were specified. "

Check warning on line 153 in src/pynwb/validation.py

View check run for this annotation

Codecov / codecov/patch

src/pynwb/validation.py#L153

Added line #L153 was not covered by tests
"Please choose only one.")

validation_errors = []

Check warning on line 156 in src/pynwb/validation.py

View check run for this annotation

Codecov / codecov/patch

src/pynwb/validation.py#L156

Added line #L156 was not covered by tests
for p in paths:
validation_errors += _validate_single_file(path=p, **kwargs)

Check warning on line 158 in src/pynwb/validation.py

View check run for this annotation

Codecov / codecov/patch

src/pynwb/validation.py#L158

Added line #L158 was not covered by tests
else:
validation_errors = _validate_single_file(path=path, **kwargs)

return validation_errors


def _validate_single_file(**kwargs):

io, path, use_cached_namespaces, namespace, verbose, driver = getargs(
"io", "path", "use_cached_namespaces", "namespace", "verbose", "driver", kwargs
)
Expand Down
19 changes: 19 additions & 0 deletions tests/validation/test_validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,3 +332,22 @@ def test_validate_io_and_path_same(self):

# remove path from error messages since it will not be included in io outputs
self.assertEqual(str(e_io.exception), str(e_path.exception))

def test_validate_paths_deprecation(self):
"""Test paths argument deprecation warning."""

# test deprecation warning for 'paths' argument
msg = ("The 'paths' argument will be deprecated in PyNWB 4.0 "
"Use 'path' instead. To migrate, call this function separately for "
"each path instead of passing a list.")
with self.assertWarnsWith(DeprecationWarning, msg):
results = validate(paths=['tests/back_compat/1.1.2_nwbfile.nwb',
'tests/back_compat/2.1.0_nwbfile_with_extension.nwb'],)
self.assertEqual(results, [])

# test specifying both 'paths' and 'path' arguments
expected_error = "Both 'paths' and 'path' were specified. Please choose only one."
with self.assertWarnsWith(DeprecationWarning, msg):
with self.assertRaisesWith(ValueError, expected_error):
validate(paths=['tests/back_compat/1.0.2_nwbfile.nwb'],
path='tests/back_compat/1.0.2_nwbfile.nwb')
Loading