Skip to content

[Bug]: Cannot set optional fields with default values to None #2105

@rly

Description

@rly

What happened?

Some optional fields in the schema have a default value specified in the PyNWB constructor. For example, TimeSeries.__init__ accepts the argument conversion with a default value of 1. A a user should be able to reset the value to None if they want so that it is not written at all. This would be used to represent an unknown value for the field. I can't think of any other use cases.

However, you cannot initialize a TimeSeries with its value set to None. As a result, all TimeSeries will have conversion set to either 1 or a user-set non-None value. A value is always written.

The core issue lies with docval in HDMF, but I want to track this issue in PyNWB as well.

Steps to Reproduce

from pynwb import TimeSeries
ts = TimeSeries(name='test', data=[1, 2], rate=1.0, unit='unit', conversion=None)

Traceback

Traceback (most recent call last):
  File "<string>", line 1, in <module>
    from pynwb import TimeSeries; ts = TimeSeries(name='test', data=[1, 2], rate=1.0, unit='unit', conversion=None); print(ts.conversion)
                                       ~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/rly/miniconda3/envs/test/lib/python3.13/site-packages/hdmf/utils.py", line 577, in func_call
    pargs = _check_args(args, kwargs)
  File "/Users/rly/miniconda3/envs/test/lib/python3.13/site-packages/hdmf/utils.py", line 570, in _check_args
    raise ExceptionType(msg)
TypeError: TimeSeries.__init__: incorrect type for 'conversion' (got 'NoneType', expected 'float')

Operating System

macOS

Python Executable

Conda

Python Version

3.13

Package Versions

No response

Code of Conduct

Metadata

Metadata

Assignees

No one assigned

    Labels

    category: bugerrors in the code or code behaviorpriority: lowalternative solution already working and/or relevant to only specific user(s)

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions