From 363e602c21c0299c542a57ca948fe99efeb56828 Mon Sep 17 00:00:00 2001 From: Isaac Turner Date: Mon, 29 Apr 2024 09:33:33 +0800 Subject: [PATCH] Deprecate deadlineWith and add deadlineFor instead From https://github.com/wpilibsuite/allwpilib/pull/6544 --- .gittrack | 2 +- commands2/command.py | 29 +++++++++++++++++++++++++++++ tests/test_command_decorators.py | 18 ++++++++++++++++++ 3 files changed, 48 insertions(+), 1 deletion(-) diff --git a/.gittrack b/.gittrack index 7831d1ec..cd0b7e97 100644 --- a/.gittrack +++ b/.gittrack @@ -1,6 +1,6 @@ [git-source-track] upstream_root = ../allwpilib/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command upstream_branch = main -upstream_commit = 25ad6eafd5eeaf57ed25db2414a20838bf098399 +upstream_commit = 0f8aa8aedff3bec7fca50dc43f803b1608d9a421 validation_root = commands2 diff --git a/commands2/command.py b/commands2/command.py index 5be9d733..3f0fa826 100644 --- a/commands2/command.py +++ b/commands2/command.py @@ -266,6 +266,35 @@ def deadlineWith(self, *parallel: Command) -> ParallelDeadlineGroup: command ends and interrupting all the others. Often more convenient/less-verbose than constructing a new ParallelDeadlineGroup explicitly. + .. note:: This decorator works by adding this command to a composition. + The command the decorator was called on cannot be scheduled + independently or be added to a different composition (namely, + decorators), unless it is manually cleared from the list of composed + commands with :func:`commands2.CommandScheduler.removeComposedCommand`. + The command composition returned from this method can be further + decorated without issue. + + :param parallel: the commands to run in parallel + :returns: the decorated command + """ + import warnings + + warnings.warn( + "deadlineWith is deprecated use deadlineFor instead", + DeprecationWarning, + stacklevel=2, + ) + + from .paralleldeadlinegroup import ParallelDeadlineGroup + + return ParallelDeadlineGroup(self, *parallel) + + def deadlineFor(self, *parallel: Command) -> ParallelDeadlineGroup: + """ + Decorates this command with a set of commands to run parallel to it, ending when the calling + command ends and interrupting all the others. Often more convenient/less-verbose than + constructing a new ParallelDeadlineGroup explicitly. + .. note:: This decorator works by adding this command to a composition. The command the decorator was called on cannot be scheduled independently or be added to a different composition (namely, diff --git a/tests/test_command_decorators.py b/tests/test_command_decorators.py index a6c42597..97f90278 100644 --- a/tests/test_command_decorators.py +++ b/tests/test_command_decorators.py @@ -96,6 +96,24 @@ def test_deadlineWith(scheduler: commands2.CommandScheduler): assert not group.isScheduled() +def test_deadlineFor(scheduler: commands2.CommandScheduler): + condition = OOBoolean(False) + condition.set(False) + + dictator = commands2.WaitUntilCommand(condition) + endsBefore = commands2.InstantCommand() + endsAfter = commands2.WaitUntilCommand(lambda: False) + + group = dictator.deadlineFor(endsBefore, endsAfter) + + scheduler.schedule(group) + scheduler.run() + assert group.isScheduled() + condition.set(True) + scheduler.run() + assert not group.isScheduled() + + def test_alongWith(scheduler: commands2.CommandScheduler): condition = OOBoolean() condition.set(False)