Skip to content

Add Type Hints to MDAnalysis.analysis.rms#5179

Open
Agarwalchetan wants to merge 7 commits intoMDAnalysis:developfrom
Agarwalchetan:develop
Open

Add Type Hints to MDAnalysis.analysis.rms#5179
Agarwalchetan wants to merge 7 commits intoMDAnalysis:developfrom
Agarwalchetan:develop

Conversation

@Agarwalchetan
Copy link
Copy Markdown

@Agarwalchetan Agarwalchetan commented Dec 15, 2025

Description

This PR adds comprehensive type hints to the MDAnalysis.analysis.rms module as part of the ongoing effort to improve type safety, code documentation, and developer experience in MDAnalysis.

Changes Made

New Imports

  • Added typing imports: Optional, Union, List, Dict, Any, Sequence, Tuple, TYPE_CHECKING
  • Added numpy.typing.NDArray for array type hints
  • Used TYPE_CHECKING guard for AtomGroup and Universe imports to avoid circular import issues at runtime

Function rmsd()

  • Added type annotations for all parameters:
    • a: NDArray, b: NDArray - coordinate arrays
    • weights: Optional[NDArray] - optional weight array
    • center: bool, superposition: bool - boolean flags
  • Added return type annotation: -> float

Function process_selection()

  • Added type hint for select parameter: Union[str, Tuple[str, str], Dict[str, str]]
  • Added return type annotation: -> Dict[str, Any]

Class RMSD

  • Added class-level type annotations for instance attributes:
    • atomgroup, reference, groupselections, weights
    • weights_groupselections, tol_mass, ref_frame
  • Added type hints to __init__() parameters including complex union types
  • Added return type annotations to _prepare() and _single_frame() methods: -> None
  • Added type assertions to help mypy with type inference in complex control flow

Class RMSF

  • Added type hints to __init__() parameters
  • Added return type annotations to _prepare(), _single_frame(), and _conclude() methods: -> None

Configuration

  • Updated mypy.ini to enable type checking for MDAnalysis.analysis.rms module

PR Checklist

  • Issue raised/referenced?
  • Tests updated/added? (No new tests needed - type hints only)
  • Documentation updated/added? (Type hints serve as inline documentation)
  • package/CHANGELOG file updated?
  • Is your name in package/AUTHORS?

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.


📚 Documentation preview 📚: https://mdanalysis--5179.org.readthedocs.build/en/5179/

Copy link
Copy Markdown

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Hello there first time contributor! Welcome to the MDAnalysis community! We ask that all contributors abide by our Code of Conduct and that first time contributors introduce themselves on GitHub Discussions so we can get to know you. You can learn more about participating here. Please also add yourself to package/AUTHORS as part of this PR.

@codecov
Copy link
Copy Markdown

codecov bot commented Dec 15, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.72%. Comparing base (24548e6) to head (3a97faa).
⚠️ Report is 22 commits behind head on develop.

Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #5179      +/-   ##
===========================================
- Coverage    92.73%   92.72%   -0.01%     
===========================================
  Files          180      180              
  Lines        22475    22485      +10     
  Branches      3190     3190              
===========================================
+ Hits         20842    20850       +8     
- Misses        1176     1178       +2     
  Partials       457      457              

☔ View full report in Codecov by Sentry.
📢 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.

@Agarwalchetan
Copy link
Copy Markdown
Author

Hi, just a gentle follow-up on this PR.
Happy to adjust anything further if needed.

def rmsd(a, b, weights=None, center=False, superposition=False):
def rmsd(
a: NDArray,
b: NDArray,
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.

proper/modern typing of NumPy arrays is tricky business -- see: https://github.com/numpy/numtype and https://numpy.org/numtype/ ; many project choose to handle this in a separate stub library

def process_selection(select):
def process_selection(
select: Union[str, Tuple[str, str], Dict[str, str]]
) -> Dict[str, Any]:
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.

Since Python 3.9, the preferred way to type a dictionary is to simply use the built-in dict


atomgroup: Union["AtomGroup", "Universe"]
reference: Union["AtomGroup", "Universe"]
groupselections: List[Dict[str, Any]]
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.

Should use built-in list and dict in Python 3.9+. But also, do we really need these? I think usually typing class and func signatures only should be needed?

Sequence[Union[str, Dict[str, str], Tuple[str, str]]]
] = None,
weights: Optional[Union[str, NDArray, List[Any]]] = None,
weights_groupselections: Union[bool, List[Any]] = False,
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.

list and dict from built-in should be preferred here and elsewhere

)

weights_gs = self.weights_groupselections
assert isinstance(weights_gs, list) # pragma: no cover
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.

this is starting to veer away from static type hints and into undesirable territory I'd say..

- Brady Johnston
2026
- Mohammad Ayaan
- Khushi Phougat
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.

not sure Brady and others would appreciate being purged from the AUTHORS file!

assert RMSD.results.rmsd.shape[0] == 2 # 2 frames (0 and 49)
assert (
RMSD.results.rmsd.shape[1] == 5
) # frame, time, rmsd, group1, group2
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.

This is pretty confusing--static typing should be separate from the need for runtime tests

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants