Skip to content

Commit 1eaf2e7

Browse files
Fix add_transformations broken state after invalid input (#5265)
* Fixes #2558 * Added input validation in add_transformations to raise TypeError if any transformation is not callable, preventing broken trajectory state * Added test test_add_transformations_rejects_non_callable in testsuite/MDAnalysisTests/coordinates/base.py * Add Harshit Gajjela to AUTHORS and update CHANGELOG
1 parent 900d20b commit 1eaf2e7

File tree

4 files changed

+23
-1
lines changed

4 files changed

+23
-1
lines changed

package/AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ Chronological list of authors
272272
- Khushi Phougat
273273
- Kushagar Garg
274274
- Jeremy M. G. Leung
275+
- Harshit Gajjela
275276

276277
External code
277278
-------------

package/CHANGELOG

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,14 @@ The rules for this file:
1515

1616
-------------------------------------------------------------------------------
1717
??/??/?? IAlibay, orbeckst, marinegor, tylerjereddy, ljwoods2, marinegor,
18-
spyke7, talagayev, tanii1125, BradyAJohnston, hejamu, jeremyleung521
18+
spyke7, talagayev, tanii1125, BradyAJohnston, hejamu, jeremyleung521,
19+
harshitgajjela-droid
1920

2021
* 2.11.0
2122

2223
Fixes
24+
* Fixed bug in add_transformations allowing non-callable transformations
25+
(Issue #2558, PR #2558)
2326
* Drop/Replace lock file test in test_xdr (Issue #5236, PR #5237)
2427
* HydrogenBondAnalysis: Fixed `count_by_time()` when using `run(FrameIterator)` that
2528
results in `self.start` and `self.end` being None (Issue #5200, PR #5202)

package/MDAnalysis/coordinates/base.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1424,8 +1424,19 @@ def add_transformations(self, *transformations):
14241424
--------
14251425
:mod:`MDAnalysis.transformations`
14261426
1427+
Raises
1428+
------
1429+
TypeError
1430+
If any transformation is not callable
1431+
14271432
"""
14281433

1434+
for transform in transformations:
1435+
if not callable(transform):
1436+
raise TypeError(
1437+
"All transformations must be callable; "
1438+
f"got object of type {type(transform)}"
1439+
)
14291440
try:
14301441
self.transformations = transformations
14311442
except ValueError:

testsuite/MDAnalysisTests/coordinates/base.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,13 @@ def transformed(ref):
290290
)
291291
return transformed
292292

293+
def test_add_transformations_rejects_non_callable(self):
294+
u = mda.Universe.empty(1, trajectory=True)
295+
with pytest.raises(
296+
TypeError, match="All transformations must be callable"
297+
):
298+
u.trajectory.add_transformations([1, 2, 3])
299+
293300
def test_n_atoms(self, ref, reader):
294301
assert_equal(reader.n_atoms, ref.n_atoms)
295302

0 commit comments

Comments
 (0)