Skip to content

Fix bug with annotation description edits - #14186

Open
larsoner wants to merge 5 commits into
mne-tools:mainfrom
larsoner:annot
Open

Fix bug with annotation description edits#14186
larsoner wants to merge 5 commits into
mne-tools:mainfrom
larsoner:annot

Conversation

@larsoner

Copy link
Copy Markdown
Member

In main, creating Annotations and then editing a description to be longer than the longest-width string silently truncates:

>>> import mne
>>> annot = mne.Annotations([0, 1], 1, ['a', 'bb'])
>>> annot.description[0] = 'aaa'
>>> annot.description[0]
np.str_('aa')

By switching to NumPy 2.0's varible-width string dtype, on this branch it works:

i>>> import mne
>>> annot = mne.Annotations([0, 1], 1, ['a', 'bb'])
>>> annot.description[0] = 'aaa'
>>> annot.description[0]
'aaa'

Changes drafted by Claude Opus 5 and reviewed / understood by me.

@larsoner
larsoner requested a review from sappelhoff as a code owner August 21, 2026 14:55

@drammock drammock left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just 2 nitpicks. Thanks @larsoner

Comment thread mne/annotations.py
Comment on lines +507 to +513
label or condition name). The array uses NumPy's variable-width
:obj:`~numpy.dtypes.StringDType`, so assigning a longer string into an
existing entry does not truncate it.

.. versionchanged:: 1.13
Previously a fixed-width (``"<U"``) dtype was used, which silently
truncated values assigned in place.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need this info in the user-facing docstring? If they're reading this version of the docs, they're not affected by the bug... and we usually do versionchanged for intentional behavior changes, not bugfixes (it wasn't intentional that truncation happened before)

Comment thread mne/annotations.py
# Variable-width string dtype used for annotation descriptions. Unlike the fixed-width
# "<U" dtypes, it does not silently truncate when assigning into an existing array,
# e.g. ``annotations.description[0] = "a_much_longer_description"``.
_DESCRIPTION_DTYPE = np.dtypes.StringDType()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] a bit unexpected to use a variable for this. Is the motivation to just save a few characters? or is there some thought that we may want to easily change the dtype at some point in future? (I don't see why we would...)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants