Skip to content

Commit 3aacf98

Browse files
authored
Merge pull request #1 from cwstryker/cwstryker/generic_types_test
Generic typing option using TypeVar
2 parents 32cb5f0 + 44df769 commit 3aacf98

File tree

7 files changed

+28
-18
lines changed

7 files changed

+28
-18
lines changed

.gittrack

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
[git-source-track]
22
upstream_root = ../allwpilib/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command
33
upstream_branch = main
4-
upstream_commit = 4595f84719759d71491e11e8df55d60ce2a6154c
5-
validation_root = commands2
4+
upstream_commit = 9206b47d6708cbae61506e962c633d722884a26f
5+
validation_root = commands2
6+

commands2/button/commandgenerichid.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def button(self, button: int, loop: Optional[EventLoop] = None) -> Trigger:
3939
"""
4040
if loop is None:
4141
loop = CommandScheduler.getInstance().getDefaultButtonLoop()
42-
return Trigger(loop, lambda: self._hid.getRawButtonPressed(button))
42+
return Trigger(loop, lambda: self._hid.getRawButton(button))
4343

4444
def pov(
4545
self, angle: int, *, pov: int = 0, loop: Optional[EventLoop] = None

commands2/deferredcommand.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,10 @@ def initSendable(self, builder: SendableBuilder):
5656
super().initSendable(builder)
5757
builder.addStringProperty(
5858
"deferred",
59-
lambda: "null"
60-
if self._command is self._null_command
61-
else self._command.getName(),
59+
lambda: (
60+
"null"
61+
if self._command is self._null_command
62+
else self._command.getName()
63+
),
6264
lambda _: None,
6365
)

commands2/profiledpidsubsystem.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22
# Open Source Software; you can modify and/or share it under the terms of
33
# the WPILib BSD license file in the root directory of this project.
44

5-
from typing import Union, cast
5+
from typing import Generic
66

77
from wpimath.trajectory import TrapezoidProfile
88

99
from .subsystem import Subsystem
10+
from .typing import GenericProfiledPIDController
1011

1112

12-
class ProfiledPIDSubsystem(Subsystem):
13+
class ProfiledPIDSubsystem(Subsystem, Generic[GenericProfiledPIDController]):
1314
"""
1415
A subsystem that uses a :class:`wpimath.controller.ProfiledPIDController`
1516
or :class:`wpimath.controller.ProfiledPIDControllerRadians` to
@@ -19,12 +20,12 @@ class ProfiledPIDSubsystem(Subsystem):
1920

2021
def __init__(
2122
self,
22-
controller,
23+
controller: GenericProfiledPIDController,
2324
initial_position: float = 0,
2425
):
2526
"""Creates a new PIDSubsystem."""
2627
super().__init__()
27-
self._controller = controller
28+
self._controller: GenericProfiledPIDController = controller
2829
self._enabled = False
2930
self.setGoal(initial_position)
3031

@@ -38,7 +39,7 @@ def periodic(self):
3839

3940
def getController(
4041
self,
41-
):
42+
) -> GenericProfiledPIDController:
4243
"""Returns the controller"""
4344
return self._controller
4445

commands2/sysid/sysidroutine.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
# validated: 2024-01-20 DS 707cb061057f sysid/SysIdRoutine.java
2-
from dataclasses import dataclass
1+
# validated: 2024-01-26 DV 19c155647273 sysid/SysIdRoutine.java
2+
from dataclasses import dataclass, field
33
from enum import Enum
44

55
from wpilib.sysid import SysIdRoutineLog, State
@@ -78,10 +78,10 @@ class Mechanism:
7878
drive: Callable[[volts], None]
7979
log: Callable[[SysIdRoutineLog], None]
8080
subsystem: Subsystem
81-
name: Optional[str] = None
81+
name: str = None # type: ignore[assignment]
8282

8383
def __post_init__(self):
84-
if self.name == None:
84+
if self.name is None:
8585
self.name = self.subsystem.getName()
8686

8787
class Direction(Enum):
@@ -96,7 +96,7 @@ def __init__(self, config: Config, mechanism: Mechanism):
9696
:param config: Hardware-independent parameters for the SysId routine.
9797
:param mechanism: Hardware interface for the SysId routine.
9898
"""
99-
super().__init__(mechanism.subsystem.getName())
99+
super().__init__(mechanism.name)
100100
self.config = config
101101
self.mechanism = mechanism
102102
self.outputVolts = 0.0

commands2/typing.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from typing import TypeVar
2+
3+
from wpimath.controller import ProfiledPIDController, ProfiledPIDControllerRadians
4+
5+
GenericProfiledPIDController = TypeVar(
6+
"GenericProfiledPIDController", ProfiledPIDControllerRadians, ProfiledPIDController
7+
)

tests/test_command_decorators.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,7 @@ def test_beforeStarting(scheduler: commands2.CommandScheduler):
6464

6565

6666
@pytest.mark.skip
67-
def test_andThenLambda(scheduler: commands2.CommandScheduler):
68-
...
67+
def test_andThenLambda(scheduler: commands2.CommandScheduler): ...
6968

7069

7170
def test_andThen(scheduler: commands2.CommandScheduler):

0 commit comments

Comments
 (0)