Skip to content

Add Feature validate_quantity#1521

Open
jwbear wants to merge 9 commits into
devfrom
j0
Open

Add Feature validate_quantity#1521
jwbear wants to merge 9 commits into
devfrom
j0

Conversation

@jwbear

@jwbear jwbear commented Jun 26, 2026

Copy link
Copy Markdown

ISSUE #531

Motivation

What was the reasoning behind this change? Please explain the changes briefly.

Fix #531

How to test the behavior?

Show how to reproduce the new behavior (can be a bug fix or a new feature)

Test FLAGS not in dictionary, integers, strings, and integers entered as strings eg "2"

Checklist

  • Did you update CHANGELOG.md with your changes?
  • Does the PR clearly describe the problem and the solution?
  • Have you reviewed our Contributing Guide?
  • Does the PR use "Fix #XXX" notation to tell GitHub to close the relevant issue numbered XXX when the PR is merged?

@codecov

codecov Bot commented Jun 26, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 90.90909% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 93.14%. Comparing base (b3685cb) to head (f9fac43).

Files with missing lines Patch % Lines
src/hdmf/spec/spec.py 90.90% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##              dev    #1521      +/-   ##
==========================================
- Coverage   93.22%   93.14%   -0.08%     
==========================================
  Files          41       41              
  Lines       10168    10186      +18     
  Branches     2100     2104       +4     
==========================================
+ Hits         9479     9488       +9     
- Misses        413      421       +8     
- Partials      276      277       +1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@jwbear

jwbear commented Jun 29, 2026

Copy link
Copy Markdown
Author

Added validate quantity and tests for validate_quantity, updated change log, and converted to private static method. Added name as parameter as outside scope for validate_quantity.

@jwbear jwbear marked this pull request as ready for review June 29, 2026 22:28
Comment thread src/hdmf/spec/spec.py Outdated
Comment thread CHANGELOG.md Outdated
Comment thread src/hdmf/spec/spec.py
'doc': 'The default name of this base storage container, used only if name is None', 'default': None},
{'name': 'attributes', 'type': list, 'doc': 'the attributes on this group', 'default': list()},
{'name': 'linkable', 'type': bool, 'doc': 'whether or not this group can be linked', 'default': True},
{'name': 'quantity', 'type': (str, int), 'doc': 'the required number of allowed instance', 'default': 1},

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Since the behavior for quantity has changed, it may be useful to clarify this also here in the doc for the parameter

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

This still seems correct as we simply expanded the handling inputs and not the required values.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Since we're updating how "quantity" is handled, this is a good time to make this docstring more informative. Could you update it to say something like f"the required number of instances. Must be a positive integer, a string representation of a positive integer, or one of the flag keys {list(FLAGS.keys())} or values {list(FLAGS.values())}."

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR addresses Issue #531 by adding validation/normalization for spec quantity values so that numeric strings can be handled consistently (instead of being treated as wildcard/many quantities).

Changes:

  • Added centralized quantity validation in BaseStorageSpec (including numeric-string handling).
  • Added unit tests for invalid quantity inputs on GroupSpec.
  • Added a changelog entry documenting the fix.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.

File Description
src/hdmf/spec/spec.py Introduces __validate_quantity and applies it during BaseStorageSpec initialization.
tests/unit/spec_tests/test_group_spec.py Adds regression tests for invalid quantity inputs.
CHANGELOG.md Adds a release note entry for Issue #531.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/hdmf/spec/spec.py
Comment thread src/hdmf/spec/spec.py Outdated
Comment on lines +528 to +534
Returns:
A validated quantity string.

Raises:
ValueError: If quantity is not in FLAGS or is not >=1.
TypeError: If quantity is not an integer.
"""

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

True removed type error

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The new docstring still says TypeError

Comment on lines +305 to +309
def test_quantity_with_string_float(self):
msg = ("Invalid quantity '3.5': must be greater than or equal to 1 or in '[?, *, +]'")
with self.assertRaisesWith(ValueError, msg):
GroupSpec(doc='A test group', name='MyGroup', quantity='3.5')

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Agreed, no valid testing code section for quantity

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

A positive test like what is described would be good to add.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You can add it to test_dataset_spec.py where the other positive tests live currently

Comment thread CHANGELOG.md Outdated
@oruebel

oruebel commented Jun 29, 2026

Copy link
Copy Markdown
Contributor

The code looks overall good to me. I added a few suggestions mostly related to documentation and asked Copilot to review. I'll let @rly do final review and approvals.

@jwbear

jwbear commented Jun 29, 2026

Copy link
Copy Markdown
Author

@oruebel Thanks, I updated everything and will push again based on the suggestions. Except I ddint see positive tests. copilot: "These tests cover invalid quantity strings, but they don't assert the new intended behavior from #531: numeric strings like '2' should be accepted and treated as integers (so '1' does not imply 'many'). Adding a positive-case regression test would prevent the original bug from reappearing."

Comment thread src/hdmf/spec/spec.py
Comment thread CHANGELOG.md Outdated
- Hardened the GitHub Actions CI: added least-privilege `permissions` blocks, pinned actions to commit SHAs (or immutable release tags), deduplicated the test setup into a composite action, passed untrusted inputs through environment variables, and added a zizmor security audit of the workflows. @rly [#1518](https://github.com/hdmf-dev/hdmf/pull/1518)

### Fixed
- Fix #531 Validate quantity specified with an integor or numeric string @jwbear [#1521]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please add the URL to the end of this line so that #1521 renders as a Markdown link in the changelog

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Also please make this past tense, and correct spelling of "integer". "Quantity" should be clarified to be GroupSpec and DatasetSpec quantity. We typically don't mention the issue number since the PR links back to the issue. Either way is fine. So something like:

- Fixed issue #531 where invalid values were accepted for `GroupSpec.quantity` and `DatasetSpec.quantity`. Now only positive integers, string representations of positive integers, and '?', '*', and '+' are allowed. @jwbear [#1521](https://github.com/hdmf-dev/hdmf/pull/1521)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Done.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please update text to be more descriptive of the change to users. Something like the above

Comment thread src/hdmf/spec/spec.py Outdated
Comment thread CHANGELOG.md Outdated
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.

Disallow or explicitly allow spec quantity to be specified with a numeric string

4 participants