Skip to content

Commit 2e318c6

Browse files
committed
Switch _normalize_platform to match/case
Also adjust test to more clearly test against isinstance
1 parent 9065ef0 commit 2e318c6

2 files changed

Lines changed: 17 additions & 17 deletions

File tree

‎openpathsampling/engines/openmm/engine.py‎

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -242,19 +242,19 @@ def platform(self):
242242

243243
@staticmethod
244244
def _normalize_platform(platform):
245-
if platform is None:
246-
return None
247-
248-
if isinstance(platform, str):
249-
return platform
250-
251-
try:
252-
return platform.getName()
253-
except (AttributeError, TypeError) as err:
254-
raise TypeError(
255-
"platform must be None, a platform name string, or an "
256-
"openmm.Platform instance"
257-
) from err
245+
match platform:
246+
case None:
247+
return None
248+
case str() as name:
249+
return name
250+
case candidate if isinstance(candidate, openmm.Platform):
251+
return candidate.getName()
252+
case _:
253+
raise TypeError(
254+
"platform must be None, a platform name string, or an "
255+
"openmm.Platform instance. "
256+
f"Got {type(platform)} instead"
257+
)
258258

259259
@property
260260
def simulation(self):

‎openpathsampling/tests/test_openmm_engine.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -339,10 +339,10 @@ def test_initialize_uses_exact_platform_object(self):
339339
assert self.uninitialized_engine._platform is None
340340
assert self.uninitialized_engine.to_dict()['platform'] is None
341341

342-
def test_bad_platform_get_name_raises_typeerror(self):
343-
class BadPlatform(object):
342+
def test_duck_typed_platform_like_object_raises_typeerror(self):
343+
class PlatformLikeObject(object):
344344
def getName(self):
345-
raise TypeError("bad getName")
345+
return 'CPU'
346346

347347
integrator = mm.LangevinIntegrator(
348348
300*u.kelvin,
@@ -357,7 +357,7 @@ def getName(self):
357357
system,
358358
integrator,
359359
options={'n_steps_per_frame': 2, 'n_frames_max': 5},
360-
platform=BadPlatform()
360+
platform=PlatformLikeObject()
361361
)
362362

363363
def test_from_dict_without_platform_key_is_backward_compatible(self):

0 commit comments

Comments
 (0)