From 10a5416c8e512fa8386fd61f06eb134336f77b27 Mon Sep 17 00:00:00 2001 From: Jusong Yu Date: Sun, 1 Dec 2024 13:47:39 +0100 Subject: [PATCH] Exclude tests/.* for ruff linting --- .pre-commit-config.yaml | 2 +- tests/base/test_statemachine.py | 8 ++-- tests/persistence/test_inmemory.py | 3 +- tests/persistence/test_pickle.py | 3 +- tests/rmq/test_process_comms.py | 6 +-- tests/test_expose.py | 3 +- tests/test_process_comms.py | 12 ++---- tests/test_processes.py | 69 +++++++----------------------- tests/utils.py | 45 ++++++++----------- 9 files changed, 48 insertions(+), 103 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 69b1054e..970f9d71 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -20,7 +20,7 @@ repos: - id: ruff-format exclude: &exclude_ruff > (?x)^( - + tests/.*| )$ - id: ruff diff --git a/tests/base/test_statemachine.py b/tests/base/test_statemachine.py index 9d89a41a..5b4b73d8 100644 --- a/tests/base/test_statemachine.py +++ b/tests/base/test_statemachine.py @@ -25,7 +25,7 @@ def __init__(self, player, track): super().__init__(player) self.track = track self._last_time = None - self._played = 0. + self._played = 0.0 def __str__(self): if self.in_state: @@ -55,8 +55,7 @@ class Paused(state_machine.State): TRANSITIONS = {STOP: STOPPED} def __init__(self, player, playing_state): - assert isinstance(playing_state, Playing), \ - 'Must provide the playing state to pause' + assert isinstance(playing_state, Playing), 'Must provide the playing state to pause' super().__init__(player) self.playing_state = playing_state @@ -117,14 +116,13 @@ def stop(self): class TestStateMachine(unittest.TestCase): - def test_basic(self): cd_player = CdPlayer() self.assertEqual(cd_player.state, STOPPED) cd_player.play('Eminem - The Real Slim Shady') self.assertEqual(cd_player.state, PLAYING) - time.sleep(1.) + time.sleep(1.0) cd_player.pause() self.assertEqual(cd_player.state, PAUSED) diff --git a/tests/persistence/test_inmemory.py b/tests/persistence/test_inmemory.py index 22b84e70..7bddfa7b 100644 --- a/tests/persistence/test_inmemory.py +++ b/tests/persistence/test_inmemory.py @@ -1,9 +1,10 @@ # -*- coding: utf-8 -*- import unittest -import plumpy from test.utils import ProcessWithCheckpoint +import plumpy + class TestInMemoryPersister(unittest.TestCase): def test_save_load_roundtrip(self): diff --git a/tests/persistence/test_pickle.py b/tests/persistence/test_pickle.py index cddb0207..b7769ed4 100644 --- a/tests/persistence/test_pickle.py +++ b/tests/persistence/test_pickle.py @@ -5,9 +5,10 @@ if getattr(tempfile, 'TemporaryDirectory', None) is None: from backports import tempfile -import plumpy from test.utils import ProcessWithCheckpoint +import plumpy + class TestPicklePersister(unittest.TestCase): def test_save_load_roundtrip(self): diff --git a/tests/rmq/test_process_comms.py b/tests/rmq/test_process_comms.py index 6bbf27db..7223b888 100644 --- a/tests/rmq/test_process_comms.py +++ b/tests/rmq/test_process_comms.py @@ -3,13 +3,13 @@ import copy import kiwipy -from kiwipy import rmq import pytest import shortuuid +from kiwipy import rmq import plumpy -from plumpy import process_comms import plumpy.communications +from plumpy import process_comms from .. import utils @@ -44,7 +44,6 @@ def sync_controller(thread_communicator: rmq.RmqThreadCommunicator): class TestRemoteProcessController: - @pytest.mark.asyncio async def test_pause(self, thread_communicator, async_controller): proc = utils.WaitForSignalProcess(communicator=thread_communicator) @@ -123,7 +122,6 @@ def on_broadcast_receive(**msg): class TestRemoteProcessThreadController: - @pytest.mark.asyncio async def test_pause(self, thread_communicator, sync_controller): proc = utils.WaitForSignalProcess(communicator=thread_communicator) diff --git a/tests/test_expose.py b/tests/test_expose.py index 8b3ad721..4e855f98 100644 --- a/tests/test_expose.py +++ b/tests/test_expose.py @@ -1,10 +1,11 @@ # -*- coding: utf-8 -*- import unittest +from test.utils import NewLoopProcess + from plumpy.ports import PortNamespace from plumpy.process_spec import ProcessSpec from plumpy.processes import Process -from test.utils import NewLoopProcess def validator_function(input, port): diff --git a/tests/test_process_comms.py b/tests/test_process_comms.py index 6d3d335c..3e15d228 100644 --- a/tests/test_process_comms.py +++ b/tests/test_process_comms.py @@ -1,23 +1,17 @@ # -*- coding: utf-8 -*- -import asyncio -from test import utils -import unittest - -from kiwipy import rmq import pytest +from test import utils import plumpy -from plumpy import communications, process_comms +from plumpy import process_comms class Process(plumpy.Process): - def run(self): pass class CustomObjectLoader(plumpy.DefaultObjectLoader): - def load_object(self, identifier): if identifier == 'jimmy': return Process @@ -49,7 +43,7 @@ async def test_continue(): @pytest.mark.asyncio async def test_loader_is_used(): - """ Make sure that the provided class loader is used by the process launcher """ + """Make sure that the provided class loader is used by the process launcher""" loader = CustomObjectLoader() proc = Process() persister = plumpy.InMemoryPersister(loader=loader) diff --git a/tests/test_processes.py b/tests/test_processes.py index f118b77c..c18e891b 100644 --- a/tests/test_processes.py +++ b/tests/test_processes.py @@ -1,22 +1,22 @@ # -*- coding: utf-8 -*- """Process tests""" + import asyncio import copy import enum -from plumpy.process_comms import KILL_MSG, MESSAGE_KEY -from test import utils import unittest import kiwipy import pytest +from test import utils import plumpy from plumpy import BundleKeys, Process, ProcessState +from plumpy.process_comms import KILL_MSG, MESSAGE_KEY from plumpy.utils import AttributesFrozendict class ForgetToCallParent(plumpy.Process): - def __init__(self, forget_on): super().__init__() self.forget_on = forget_on @@ -44,9 +44,7 @@ def on_kill(self, msg): @pytest.mark.asyncio async def test_process_scope(): - class ProcessTaskInterleave(plumpy.Process): - async def task(self, steps: list): steps.append(f'[{self.pid}] started') assert plumpy.Process.current() is self @@ -66,7 +64,6 @@ async def task(self, steps: list): class TestProcess(unittest.TestCase): - def test_spec(self): """ Check that the references to specs are doing the right thing... @@ -84,12 +81,10 @@ class Proc(utils.DummyProcess): self.assertIs(p.spec(), Proc.spec()) def test_dynamic_inputs(self): - class NoDynamic(Process): pass class WithDynamic(Process): - @classmethod def define(cls, spec): super().define(spec) @@ -102,9 +97,7 @@ def define(cls, spec): proc.execute() def test_inputs(self): - class Proc(Process): - @classmethod def define(cls, spec): super().define(spec) @@ -124,7 +117,6 @@ def test_raw_inputs(self): """ class Proc(Process): - @classmethod def define(cls, spec): super().define(spec) @@ -140,9 +132,7 @@ def define(cls, spec): self.assertDictEqual(dict(process.raw_inputs), {'a': 5, 'nested': {'a': 'value'}}) def test_inputs_default(self): - class Proc(utils.DummyProcess): - @classmethod def define(cls, spec): super().define(spec) @@ -201,7 +191,6 @@ def test_inputs_default_that_evaluate_to_false(self): for def_val in (True, False, 0, 1): class Proc(utils.DummyProcess): - @classmethod def define(cls, spec): super().define(spec) @@ -216,7 +205,6 @@ def test_nested_namespace_defaults(self): """Process with a default in a nested namespace should be created, even if top level namespace not supplied.""" class SomeProcess(Process): - @classmethod def define(cls, spec): super().define(spec) @@ -231,7 +219,6 @@ def test_raise_in_define(self): """Process which raises in its 'define' method. Check that the spec is not set.""" class BrokenProcess(Process): - @classmethod def define(cls, spec): super().define(spec) @@ -295,12 +282,11 @@ def test_run_kill(self): proc.execute() def test_get_description(self): - class ProcWithoutSpec(Process): pass class ProcWithSpec(Process): - """ Process with a spec and a docstring """ + """Process with a spec and a docstring""" @classmethod def define(cls, spec): @@ -326,9 +312,7 @@ def define(cls, spec): self.assertIsInstance(desc_with_spec['description'], str) def test_logging(self): - class LoggerTester(Process): - def run(self, **kwargs): self.logger.info('Test') @@ -442,7 +426,6 @@ async def async_test(): self.assertEqual(proc.state, ProcessState.FINISHED) def test_kill_in_run(self): - class KillProcess(Process): after_kill = False @@ -462,9 +445,7 @@ def run(self, **kwargs): self.assertEqual(proc.state, ProcessState.KILLED) def test_kill_when_paused_in_run(self): - class PauseProcess(Process): - def run(self, **kwargs): self.pause() self.kill() @@ -516,9 +497,7 @@ def test_run_multiple(self): self.assertDictEqual(proc_class.EXPECTED_OUTPUTS, result) def test_invalid_output(self): - class InvalidOutput(plumpy.Process): - def run(self): self.out('invalid', 5) @@ -542,7 +521,6 @@ def test_unsuccessful_result(self): ERROR_CODE = 256 class Proc(Process): - @classmethod def define(cls, spec): super().define(spec) @@ -556,11 +534,10 @@ def run(self): self.assertEqual(proc.result(), ERROR_CODE) def test_pause_in_process(self): - """ Test that we can pause and cancel that by playing within the process """ + """Test that we can pause and cancel that by playing within the process""" test_case = self class TestPausePlay(plumpy.Process): - def run(self): fut = self.pause() test_case.assertIsInstance(fut, plumpy.Future) @@ -580,12 +557,11 @@ def run(self): self.assertEqual(plumpy.ProcessState.FINISHED, proc.state) def test_pause_play_in_process(self): - """ Test that we can pause and play that by playing within the process """ + """Test that we can pause and play that by playing within the process""" test_case = self class TestPausePlay(plumpy.Process): - def run(self): fut = self.pause() test_case.assertIsInstance(fut, plumpy.Future) @@ -602,7 +578,6 @@ def test_process_stack(self): test_case = self class StackTest(plumpy.Process): - def run(self): test_case.assertIs(self, Process.current()) @@ -619,7 +594,6 @@ def test_nested(process): expect_true.append(process == Process.current()) class StackTest(plumpy.Process): - def run(self): # TODO: unexpected behaviour here # if assert error happend here not raise @@ -629,7 +603,6 @@ def run(self): test_nested(self) class ParentProcess(plumpy.Process): - def run(self): expect_true.append(self == Process.current()) StackTest().execute() @@ -652,21 +625,17 @@ def test_process_nested(self): """ class StackTest(plumpy.Process): - def run(self): pass class ParentProcess(plumpy.Process): - def run(self): StackTest().execute() ParentProcess().execute() def test_call_soon(self): - class CallSoon(plumpy.Process): - def run(self): self.call_soon(self.do_except) @@ -686,7 +655,6 @@ def test_exception_during_on_entered(self): """Test that an exception raised during ``on_entered`` will cause the process to be excepted.""" class RaisingProcess(Process): - def on_entered(self, from_state): if from_state is not None and from_state.label == ProcessState.RUNNING: raise RuntimeError('exception during on_entered') @@ -702,9 +670,7 @@ def on_entered(self, from_state): assert str(process.exception()) == 'exception during on_entered' def test_exception_during_run(self): - class RaisingProcess(Process): - def run(self): raise RuntimeError('exception during run') @@ -868,7 +834,7 @@ async def async_test(): loop.run_until_complete(async_test()) def test_wait_save_continue(self): - """ Test that process saved while in WAITING state restarts correctly when loaded """ + """Test that process saved while in WAITING state restarts correctly when loaded""" loop = asyncio.get_event_loop() proc = utils.WaitForSignalProcess() @@ -911,7 +877,6 @@ def _check_round_trip(self, proc1): class TestProcessNamespace(unittest.TestCase): - def test_namespaced_process(self): """ Test that inputs in nested namespaces are properly validated and the returned @@ -919,7 +884,6 @@ def test_namespaced_process(self): """ class NameSpacedProcess(Process): - @classmethod def define(cls, spec): super().define(spec) @@ -944,7 +908,6 @@ def test_namespaced_process_inputs(self): """ class NameSpacedProcess(Process): - @classmethod def define(cls, spec): super().define(spec) @@ -970,7 +933,6 @@ def test_namespaced_process_dynamic(self): namespace = 'name.space' class DummyDynamicProcess(Process): - @classmethod def define(cls, spec): super().define(spec) @@ -997,14 +959,12 @@ def test_namespaced_process_outputs(self): namespace_nested = f'{namespace}.nested' class OutputMode(enum.Enum): - NONE = 0 DYNAMIC_PORT_NAMESPACE = 1 SINGLE_REQUIRED_PORT = 2 BOTH_SINGLE_AND_NAMESPACE = 3 class DummyDynamicProcess(Process): - @classmethod def define(cls, spec): super().define(spec) @@ -1063,7 +1023,6 @@ def run(self): class TestProcessEvents(unittest.TestCase): - def test_basic_events(self): proc = utils.DummyProcessWithOutput() events_tester = utils.ProcessListenerTester( @@ -1083,11 +1042,14 @@ def test_killed(self): def test_excepted(self): proc = utils.ExceptionProcess() - events_tester = utils.ProcessListenerTester(proc, ( - 'excepted', - 'running', - 'output_emitted', - )) + events_tester = utils.ProcessListenerTester( + proc, + ( + 'excepted', + 'running', + 'output_emitted', + ), + ) with self.assertRaises(RuntimeError): proc.execute() proc.result() @@ -1126,7 +1088,6 @@ def on_broadcast_receive(_comm, body, sender, subject, correlation_id): class _RestartProcess(utils.WaitForSignalProcess): - @classmethod def define(cls, spec): super().define(spec) diff --git a/tests/utils.py b/tests/utils.py index 3aaaf61a..f2a58dfc 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -1,13 +1,11 @@ # -*- coding: utf-8 -*- """Utilities for tests""" + import asyncio import collections -from collections.abc import Mapping import copy import unittest - -import kiwipy.rmq -import shortuuid +from collections.abc import Mapping import plumpy from plumpy import persistence, process_states, processes, utils @@ -26,7 +24,9 @@ class DummyProcess(processes.Process): """ EXPECTED_STATE_SEQUENCE = [ - process_states.ProcessState.CREATED, process_states.ProcessState.RUNNING, process_states.ProcessState.FINISHED + process_states.ProcessState.CREATED, + process_states.ProcessState.RUNNING, + process_states.ProcessState.FINISHED, ] EXPECTED_OUTPUTS = {} @@ -60,14 +60,12 @@ def run(self, **kwargs): class KeyboardInterruptProc(processes.Process): - @utils.override def run(self): raise KeyboardInterrupt() class ProcessWithCheckpoint(processes.Process): - @utils.override def run(self): return process_states.Continue(self.last_step) @@ -77,7 +75,6 @@ def last_step(self): class WaitForSignalProcess(processes.Process): - @utils.override def run(self): return process_states.Wait(self.last_step) @@ -87,7 +84,6 @@ def last_step(self): class KillProcess(processes.Process): - @utils.override def run(self): msg = copy.copy(KILL_MSG) @@ -96,7 +92,7 @@ def run(self): class MissingOutputProcess(processes.Process): - """ A process that does not generate a required output """ + """A process that does not generate a required output""" @classmethod def define(cls, spec): @@ -105,7 +101,6 @@ def define(cls, spec): class NewLoopProcess(processes.Process): - def __init__(self, *args, **kwargs): kwargs['loop'] = plumpy.new_event_loop() super().__init__(*args, **kwargs) @@ -122,8 +117,7 @@ def called(cls, event): cls.called_events.append(event) def __init__(self, *args, **kwargs): - assert isinstance(self, processes.Process), \ - 'Mixin has to be used with a type derived from a Process' + assert isinstance(self, processes.Process), 'Mixin has to be used with a type derived from a Process' super().__init__(*args, **kwargs) self.__class__.called_events = [] @@ -169,7 +163,6 @@ def on_terminate(self): class ProcessEventsTester(EventsTesterMixin, processes.Process): - @classmethod def define(cls, spec): super().define(spec) @@ -197,7 +190,6 @@ def last_step(self): class TwoCheckpointNoFinish(ProcessEventsTester): - def run(self): self.out('test', 5) return process_states.Continue(self.middle_step) @@ -207,21 +199,18 @@ def middle_step(self): class ExceptionProcess(ProcessEventsTester): - def run(self): self.out('test', 5) raise RuntimeError('Great scott!') class ThreeStepsThenException(ThreeSteps): - @utils.override def last_step(self): raise RuntimeError('Great scott!') class ProcessListenerTester(plumpy.ProcessListener): - def __init__(self, process, expected_events): process.add_process_listener(self) self.expected_events = set(expected_events) @@ -253,7 +242,6 @@ def on_process_killed(self, process, msg): class Saver: - def __init__(self): self.snapshots = [] self.outputs = [] @@ -361,7 +349,11 @@ def on_process_killed(self, process, msg): TEST_PROCESSES = [DummyProcess, DummyProcessWithOutput, DummyProcessWithDynamicOutput, ThreeSteps] TEST_WAITING_PROCESSES = [ - ProcessWithCheckpoint, TwoCheckpointNoFinish, ExceptionProcess, ProcessEventsTester, ThreeStepsThenException + ProcessWithCheckpoint, + TwoCheckpointNoFinish, + ExceptionProcess, + ProcessEventsTester, + ThreeStepsThenException, ] TEST_EXCEPTION_PROCESSES = [ExceptionProcess, ThreeStepsThenException, MissingOutputProcess] @@ -406,7 +398,7 @@ def check_process_against_snapshots(loop, proc_class, snapshots): saver.snapshots[-j], snapshots[-j], saver.snapshots[-j], - exclude={'exception', '_listeners'} + exclude={'exception', '_listeners'}, ) j += 1 @@ -442,9 +434,8 @@ def compare_value(bundle1, bundle2, v1, v2, exclude=None): compare_value(bundle1, bundle2, list(v1), list(v2), exclude) elif isinstance(v1, set) and isinstance(v2, set): raise NotImplementedError('Comparison between sets not implemented') - else: - if v1 != v2: - raise ValueError(f'Dict values mismatch for :\n{v1} != {v2}') + elif v1 != v2: + raise ValueError(f'Dict values mismatch for :\n{v1} != {v2}') class TestPersister(persistence.Persister): @@ -453,7 +444,7 @@ class TestPersister(persistence.Persister): """ def save_checkpoint(self, process, tag=None): - """ Create the checkpoint bundle """ + """Create the checkpoint bundle""" persistence.Bundle(process) def load_checkpoint(self, pid, tag=None): @@ -473,7 +464,7 @@ def delete_process_checkpoints(self, pid): def run_until_waiting(proc): - """ Set up a future that will be resolved on entering the WAITING state """ + """Set up a future that will be resolved on entering the WAITING state""" from plumpy import ProcessState listener = plumpy.ProcessListener() @@ -494,7 +485,7 @@ def on_waiting(_waiting_proc): def run_until_paused(proc): - """ Set up a future that will be resolved when the process is paused """ + """Set up a future that will be resolved when the process is paused""" listener = plumpy.ProcessListener() paused = plumpy.Future()