From 55a5abdfd8876c33e795aa3b9aa09de43ba15287 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Doma=C5=84ski?= Date: Sun, 5 Mar 2023 13:15:16 +0000 Subject: [PATCH] fix: get_writer errors if filename is none (#4043) * fix: get_writer errors if filename is none --- package/CHANGELOG | 3 ++- package/MDAnalysis/selections/__init__.py | 7 ++++--- testsuite/MDAnalysisTests/utils/test_selections.py | 9 +++++++++ 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/package/CHANGELOG b/package/CHANGELOG index b609f4add46..5fecfbd480b 100644 --- a/package/CHANGELOG +++ b/package/CHANGELOG @@ -15,10 +15,11 @@ The rules for this file: ------------------------------------------------------------------------------ ??/??/?? IAlibay, pgbarletta, mglagolev, hmacdope, manuel.nuno.melo, chrispfae, - ooprathamm, MeetB7, BFedder, v-parmar, MoSchaeffler, jbarnoud + ooprathamm, MeetB7, BFedder, v-parmar, MoSchaeffler, jbarnoud, jandom * 2.5.0 Fixes + * Fix uninitialized `format` variable issue when calling `selections.get_writer` directly (PR #4043) * Fix tests should use results.rmsf to avoid DeprecationWarning (Issue #3695) * Fix EDRReader failing when parsing single-frame EDR files (Issue #3999) * Fix element parsing from PSF files tests read via Parmed (Issue #4015) diff --git a/package/MDAnalysis/selections/__init__.py b/package/MDAnalysis/selections/__init__.py index 4ff08dc116d..92c1eef8d92 100644 --- a/package/MDAnalysis/selections/__init__.py +++ b/package/MDAnalysis/selections/__init__.py @@ -48,6 +48,7 @@ from .. import _SELECTION_WRITERS +from . import base from . import vmd from . import pymol from . import gromacs @@ -55,7 +56,7 @@ from . import jmol -def get_writer(filename, defaultformat): +def get_writer(filename: str, defaultformat: str) -> base.SelectionWriterBase: """Return a SelectionWriter for `filename` or a `defaultformat`. Parameters @@ -67,7 +68,7 @@ def get_writer(filename, defaultformat): Returns ------- - SelectionWriter : `type` + SelectionWriterBase : `type` the writer *class* for the detected format Raises @@ -75,7 +76,7 @@ def get_writer(filename, defaultformat): :exc:`NotImplementedError` for any format that is not defined """ - + format = None if filename: format = os.path.splitext(filename)[1][1:] # strip initial dot! format = format or defaultformat # use default if no fmt from fn diff --git a/testsuite/MDAnalysisTests/utils/test_selections.py b/testsuite/MDAnalysisTests/utils/test_selections.py index 727efb5ba51..2dec2698d8d 100644 --- a/testsuite/MDAnalysisTests/utils/test_selections.py +++ b/testsuite/MDAnalysisTests/utils/test_selections.py @@ -25,6 +25,7 @@ # use StringIO and NamedStream to write to memory instead to temp files import pytest from io import StringIO +from unittest.mock import patch import re @@ -34,6 +35,7 @@ from MDAnalysis.tests.datafiles import PSF, DCD import MDAnalysis +import MDAnalysis.selections as selections from MDAnalysis.lib.util import NamedStream @@ -196,3 +198,10 @@ def _assert_selectionstring(self, namedfile): err_msg="SPT file has wrong selection name") assert_array_equal(indices, self.ref_indices, err_msg="SPT indices were not written correctly") + + +class TestSelections: + @patch.object(selections, "_SELECTION_WRITERS", {"FOO": "BAR"}) + def test_get_writer_valid(self): + writer = selections.get_writer(None, "FOO") + assert writer == "BAR"