Skip to content

Commit

Permalink
magicbot: feedback: Allow fixed-length homogenous tuples
Browse files Browse the repository at this point in the history
  • Loading branch information
auscompgeek committed Aug 7, 2024
1 parent 8b94e8e commit 0c6b539
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
6 changes: 4 additions & 2 deletions magicbot/magic_tunable.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,10 @@ def _get_topic_type(
origin = getattr(return_annotation, "__origin__", None)
args = typing.get_args(return_annotation)
if origin in (list, tuple, collections.abc.Sequence) and args:
# Ensure tuples are tuple[T, ...]
if origin is tuple and not (len(args) == 2 and args[1] is Ellipsis):
# Ensure tuples are tuple[T, ...] or homogenous
if origin is tuple and not (
(len(args) == 2 and args[1] is Ellipsis) or len(set(args)) == 1
):
return None

inner_type = args[0]
Expand Down
11 changes: 10 additions & 1 deletion tests/test_magicbot_feedback.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Sequence
from typing import Sequence, Tuple

import ntcore
from wpimath import geometry
Expand Down Expand Up @@ -32,6 +32,10 @@ def get_rotation(self) -> geometry.Rotation2d:
def get_rotation_array(self) -> Sequence[geometry.Rotation2d]:
return [geometry.Rotation2d()]

@magicbot.feedback
def get_rotation_2_tuple(self) -> Tuple[geometry.Rotation2d, geometry.Rotation2d]:
return (geometry.Rotation2d(), geometry.Rotation2d())

@magicbot.feedback
def get_int(self) -> int:
return 0
Expand Down Expand Up @@ -90,6 +94,11 @@ def test_feedbacks_with_type_hints():

for name, struct_type, value in (
("type_hinted/rotation_array", geometry.Rotation2d, [geometry.Rotation2d()]),
(
"type_hinted/rotation_2_tuple",
geometry.Rotation2d,
[geometry.Rotation2d(), geometry.Rotation2d()],
),
):
assert nt.getTopic(name).getTypeString() == f"struct:{struct_type.__name__}[]"
topic = nt.getStructArrayTopic(name, struct_type)
Expand Down

0 comments on commit 0c6b539

Please sign in to comment.