From 953198cfbdea8e13d0ebccc7107e27786fb243ef Mon Sep 17 00:00:00 2001 From: Yaroslav Halchenko Date: Fri, 2 Feb 2024 16:03:30 -0500 Subject: [PATCH] BF: do or to "" to ensure that we do not end up treating None as str (#433) * BF: do or to "" to ensure that we do not end up treating None as str We started to get a test in dandi-cli to error out E + '"/opt/hostedtoolcache/Python/3.12.1/x64/lib/python3.12/site-packages/nwbinspector/nwbinspector.py", ' E + 'line 632, in _intercept_in_vitro_protein\n' E + ' and getattr(subject, "subject_id", "").startswith("protein")\n' E + ' ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n' E + "AttributeError: 'NoneType' object has no attribute 'startswith'\n", I didn't test yet but this I think should fix it * Update CHANGELOG.md * add test * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: Cody Baker <51133164+CodyCBakerPhD@users.noreply.github.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- CHANGELOG.md | 6 ++++++ src/nwbinspector/nwbinspector.py | 2 +- tests/test_inspector.py | 9 +++++++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 457c44060..6fa3f46e8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Upcoming +# v0.4.33 + +### Fixes + +* Add safer retrieval of `subject_id` for _in vitro_ protein filtering. [#433](https://github.com/NeurodataWithoutBorders/nwbinspector/pull/433) + # v0.4.32 diff --git a/src/nwbinspector/nwbinspector.py b/src/nwbinspector/nwbinspector.py index 378ca99df..64e01558b 100644 --- a/src/nwbinspector/nwbinspector.py +++ b/src/nwbinspector/nwbinspector.py @@ -629,7 +629,7 @@ def _intercept_in_vitro_protein(nwbfile_object: pynwb.NWBFile, checks: Optional[ if ( any(subject_related_dandi_requirements) and subject is not None - and getattr(subject, "subject_id", "").startswith("protein") + and (getattr(subject, "subject_id") or "").startswith("protein") ): non_subject_checks = [check for check in checks if check.__name__ not in subject_related_check_names] return non_subject_checks diff --git a/tests/test_inspector.py b/tests/test_inspector.py index b1863efba..1c207f4db 100644 --- a/tests/test_inspector.py +++ b/tests/test_inspector.py @@ -740,3 +740,12 @@ def test_dandi_config_in_vitro_injection(): inspect_nwbfile_object(nwbfile_object=nwbfile, config=config, importance_threshold=importance_threshold) ) assert messages == [] + + +def test_dandi_config_in_vitro_injection(): + """Test the safe subject ID retrieval of the in vitro injection.""" + nwbfile = make_minimal_nwbfile() + nwbfile.subject = Subject(subject_id=None, description="A detailed description about the in vitro setup.") + config = load_config(filepath_or_keyword="dandi") + messages = list(inspect_nwbfile_object(nwbfile_object=nwbfile, config=config)) + assert len(messages) != 0