Skip to content

fix(core): add data type validation when setting topology attributes … - #5513

Open
dhrubchatterjee16-lang wants to merge 4 commits into
MDAnalysis:developfrom
dhrubchatterjee16-lang:attr-dtype-checks-2764
Open

dhrubchatterjee16-lang wants to merge 4 commits into
MDAnalysis:developfrom
dhrubchatterjee16-lang:attr-dtype-checks-2764

Conversation

@dhrubchatterjee16-lang

Copy link
Copy Markdown

Fixes #2764

Changes made in this Pull Request:

  • Added data type validation in MDAnalysis.core.topologyattrs._check_dtype invoked by the setter decorator to ensure assigned values match the attribute's expected data type.
  • String attributes (AtomStringAttr, ResidueStringAttr, SegmentStringAttr) now reject non-string inputs with a clear TypeError (e.g. atom.name = 1 raises TypeError: name must be a string rather than 'int' object is not iterable, and group assignments like atoms.names = [1, 2] raise TypeError: All values for names must be strings).
  • Integer attributes (Resids, Atomids, Resnums, etc.) reject floats and booleans with a TypeError rather than silently truncating or coercing them.
  • Numeric/float attributes (Masses, Charges, etc.) reject booleans and non-numeric types while allowing valid integers/floats.
  • Added comprehensive unit tests in TestDtypeChecks in MDAnalysisTests.core.test_topologyattrs.
  • Updated package/CHANGELOG and package/AUTHORS.

LLM / AI generated code disclosure

LLMs or other AI-powered tools (beyond simple IDE use cases) were used in this contribution: yes

Details of AI usage:

  • An AI assistant was used for interactive discussion of the attribute inheritance hierarchy in topologyattrs.py, designing the _check_dtype helper with O(1) NumPy subdtype checks, and designing edge-case unit tests.
  • All code, validation logic, and test cases have been personally reviewed, tested, and verified to ensure full adherence to MDAnalysis standards and AI policy.

PR Checklist

  • Issue raised/referenced?
  • Tests updated/added?
  • Documentation updated/added?
  • package/CHANGELOG file updated?
  • Is your name in package/AUTHORS? (If it is not, add it!)
  • I have read and understand the current AI Policy
  • LLM/AI disclosure was updated.

Developers Certificate of Origin

I certify that I can submit this code contribution as described in the Developer Certificate of Origin, under the MDAnalysis LICENSE.

@codecov

codecov Bot commented Oct 21, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.93%. Comparing base (6cd1f7e) to head (18a64f0).
⚠️ Report is 1 commits behind head on develop.

Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #5513      +/-   ##
===========================================
+ Coverage    93.91%   93.93%   +0.01%     
===========================================
  Files          184      184              
  Lines        22644    22696      +52     
  Branches      3219     3237      +18     
===========================================
+ Hits         21267    21319      +52     
  Misses         917      917              
  Partials       460      460              

☔ 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.

@BradyAJohnston BradyAJohnston 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.

I'm blocking this as we currently don't accept AI contributions due to our AI policy. This will likely be changing in the near future. If you do want to continue with it, you'll need to address the coverage and linting issues before we review it then.

@read-the-docs-community

read-the-docs-community Bot commented Oct 21, 2026 •

Copy link
Copy Markdown

@dhrubchatterjee16-lang

Copy link
Copy Markdown
Author

Hi @BradyAJohnston,

Thanks for the head-up. I'd definitely like to continue with this and see it through.

I've fixed the formatting with black (set to 79 chars) and added tests for boolean attributes and numpy arrays to cover all the branches. The linter and type checks are passing now.

Please let me know if anything else needs to be adjusted. Thanks!

This branch has not been deployed

No deployments
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.

Setting attributes should have dtype checks

2 participants