Add Type Hints to MDAnalysis.analysis.rms#5179
Add Type Hints to MDAnalysis.analysis.rms#5179Agarwalchetan wants to merge 7 commits intoMDAnalysis:developfrom
Conversation
There was a problem hiding this comment.
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 Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
|
Hi, just a gentle follow-up on this PR. |
| def rmsd(a, b, weights=None, center=False, superposition=False): | ||
| def rmsd( | ||
| a: NDArray, | ||
| b: NDArray, |
There was a problem hiding this comment.
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]: |
There was a problem hiding this comment.
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]] |
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
this is starting to veer away from static type hints and into undesirable territory I'd say..
| - Brady Johnston | ||
| 2026 | ||
| - Mohammad Ayaan | ||
| - Khushi Phougat |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
This is pretty confusing--static typing should be separate from the need for runtime tests
Description
This PR adds comprehensive type hints to the
MDAnalysis.analysis.rmsmodule as part of the ongoing effort to improve type safety, code documentation, and developer experience in MDAnalysis.Changes Made
New Imports
typingimports:Optional,Union,List,Dict,Any,Sequence,Tuple,TYPE_CHECKINGnumpy.typing.NDArrayfor array type hintsTYPE_CHECKINGguard forAtomGroupandUniverseimports to avoid circular import issues at runtimeFunction
rmsd()a: NDArray,b: NDArray- coordinate arraysweights: Optional[NDArray]- optional weight arraycenter: bool,superposition: bool- boolean flags-> floatFunction
process_selection()selectparameter:Union[str, Tuple[str, str], Dict[str, str]]-> Dict[str, Any]Class
RMSDatomgroup,reference,groupselections,weightsweights_groupselections,tol_mass,ref_frame__init__()parameters including complex union types_prepare()and_single_frame()methods:-> Nonemypywith type inference in complex control flowClass
RMSF__init__()parameters_prepare(),_single_frame(), and_conclude()methods:-> NoneConfiguration
mypy.inito enable type checking forMDAnalysis.analysis.rmsmodulePR Checklist
package/CHANGELOGfile updated?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/