From 5a1b1058435ed5a7e243aa68f432789d42726f18 Mon Sep 17 00:00:00 2001 From: "Matthew W. Thompson" Date: Tue, 6 Feb 2024 12:22:00 -0600 Subject: [PATCH] Revert --- openff/toolkit/_tests/test_forcefield.py | 52 +++++++++---------- .../typing/engines/smirnoff/forcefield.py | 9 ++-- 2 files changed, 28 insertions(+), 33 deletions(-) diff --git a/openff/toolkit/_tests/test_forcefield.py b/openff/toolkit/_tests/test_forcefield.py index 3fcf99e42..e7b836491 100644 --- a/openff/toolkit/_tests/test_forcefield.py +++ b/openff/toolkit/_tests/test_forcefield.py @@ -6,7 +6,6 @@ import copy import itertools import os -import re from tempfile import NamedTemporaryFile import numpy as np @@ -900,33 +899,31 @@ def test_create_forcefield_from_file(self, force_field): assert force_field.aromaticity_model == "OEAroModel_MDL" def test_load_bad_string(self): - # This exception is a multi-line string, and `.*` by default doesn't - # consider newlines, so need to copmile with re.DOTALL flag - with pytest.raises( - IOError, - match=re.compile( - "Source '1234' could not be read.*" - "openforcefield.*" - "SMIRNOFFParseError.*" - "syntax error", - flags=re.DOTALL, - ), - ): + with pytest.raises(IOError) as exception_info: ForceField("1234") + # This may need to be updated if the `openforcefields` package changes name; + # searching through `site-packages/` and other paths is probably unrelaiable + for match in [ + "Source '1234' could not be read", + "SMIRNOFFParseError", + "syntax error", + "openforcefields", + ]: + assert match in str(exception_info.value) + def test_load_bad_bytes(self): - with pytest.raises( - IOError, - match=re.compile( - "Source 'b'the holy grail.*" - "could not be read.*" - "SMIRNOFFParseError.*" - "syntax error.*", - flags=re.DOTALL, - ), - ) as exception_info: + with pytest.raises(IOError) as exception_info: ForceField(b"the holy grail of computational chemistry") + for match in [ + "Source 'b'the holy grail", + "could not be read", + "SMIRNOFFParseError", + "syntax error", + ]: + assert match in str(exception_info.value) + # See note in test_load_bad_string assert "openforcefields" not in str(exception_info.value) @@ -936,12 +933,13 @@ def test_load_filelike_object(self): def test_load_bad_filelike_object(self): with open(get_data_file_path("test_forcefields/mangled.offxml")) as f: - with pytest.raises( - IOError, - match="while trying to parse source as an object", - ): + with pytest.raises(IOError) as exception_info: ForceField(f) + assert "while trying to parse source as an object" in str( + exception_info.value + ) + def test_create_forcefield_from_xml_string(self): forcefield = ForceField(xml_simple_ff) assert len(forcefield._parameter_handlers["Bonds"]._parameters) == 3 diff --git a/openff/toolkit/typing/engines/smirnoff/forcefield.py b/openff/toolkit/typing/engines/smirnoff/forcefield.py index 27b0be4e6..b4adc8c5c 100644 --- a/openff/toolkit/typing/engines/smirnoff/forcefield.py +++ b/openff/toolkit/typing/engines/smirnoff/forcefield.py @@ -797,12 +797,9 @@ def parse_sources(self, sources, allow_cosmetic_attributes=True): # TODO: If a non-first source fails here, the force field might be partially modified for source in sources: smirnoff_data = self.parse_smirnoff_from_source(source) - try: - self._load_smirnoff_data( - smirnoff_data, allow_cosmetic_attributes=allow_cosmetic_attributes - ) - except Exception as error: - raise TypeError(f"{smirnoff_data=}\n{source=}\n{sources=}\n") from error + self._load_smirnoff_data( + smirnoff_data, allow_cosmetic_attributes=allow_cosmetic_attributes + ) def _to_smirnoff_data(self, discard_cosmetic_attributes=False) -> dict: """