Skip to content

Commit

Permalink
add scratch tests and deprecations
Browse files Browse the repository at this point in the history
  • Loading branch information
stephprince committed Dec 18, 2024
1 parent 93ae7a1 commit 01be0fb
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/pynwb/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,21 +129,22 @@ def __init__(self, **kwargs):
super().__init__(**kwargs)
if notes != '':
self._error_on_new_warn_on_construct(
error_msg='The `notes` argument of ScratchData.__init__ has been deprecated. Use description instead.'
error_msg=("The `notes` argument of ScratchData.__init__ has been deprecated. "
"Use description instead.")
)
if notes != '' and description != '':
if notes != '' and description is not None:
self._error_on_new_warn_on_construct(
error_msg='Cannot provide both notes and description to ScratchData.__init__. The description '
'argument is recommended.'
'argument is recommended.'
)
description = notes
description = notes
if not description:
self._error_on_new_warn_on_construct(error_msg='ScratchData.description is required by PyNWB > 3.0.')
self.description = description

@property
def notes(self):
raise DeprecationWarning('Use of ScratchData.notes has been deprecated. Use ScratchData.description instead.')
warn('Use of ScratchData.notes has been deprecated. Use ScratchData.description instead.', DeprecationWarning)

@notes.setter
def notes(self, value):
Expand Down
27 changes: 27 additions & 0 deletions tests/unit/test_scratch.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ def test_constructor_list(self):
self.assertListEqual(sd.data, [1, 2, 3, 4])
self.assertEqual(sd.description, 'test scratch')

def test_scratch_notes_deprecation(self):
msg = "The `notes` argument of ScratchData.__init__ has been deprecated. Use description instead."
with self.assertRaisesWith(ValueError, msg):
ScratchData(name='test', data=[1, 2, 3, 4, 5], notes='test notes')

# create object in construct mode, modelling the behavior of the ObjectMapper on read
data = ScratchData.__new__(ScratchData, in_construct_mode=True)
with self.assertWarnsWith(UserWarning, msg):
data.__init__(name='test', data=[1, 2, 3, 4, 5], notes='test notes')
self.assertEqual(data.description, 'test notes')

def test_add_scratch_int(self):
ret = self.nwbfile.add_scratch(2, name='test', description='test data')
self.assertIsInstance(ret, ScratchData)
Expand Down Expand Up @@ -106,6 +117,22 @@ def test_add_scratch_dynamictable(self):
self.nwbfile.add_scratch(data)
self.assertIs(self.nwbfile.get_scratch('test', convert=False), data)
self.assertIs(self.nwbfile.scratch['test'], data)

def test_add_scratch_notes_deprecation(self):
msg = ("Use of the `notes` or `table_description` argument is deprecated. "
"Use the `description` argument instead.")
with self.assertWarnsWith(DeprecationWarning, msg):
self.nwbfile.add_scratch(name='test', data=[1, 2, 3, 4, 5], notes='test notes')
self.assertEqual(self.nwbfile.scratch['test'].description, 'test notes')

def test_add_scratch_table_description_deprecation(self):
msg = ("Use of the `notes` or `table_description` argument is deprecated. "
"Use the `description` argument instead.")
df = pd.DataFrame(data={'col1': [1, 2, 3, 4], 'col2': ['a', 'b', 'c', 'd']})
with self.assertWarnsWith(DeprecationWarning, msg):
self.nwbfile.add_scratch(name='test', data=df,
table_description='test table_description')
self.assertEqual(self.nwbfile.scratch['test'].description, 'test table_description')

def test_get_scratch_list_convert_false(self):
self.nwbfile.add_scratch([1, 2, 3, 4], name='test', description='test description')
Expand Down

0 comments on commit 01be0fb

Please sign in to comment.