Skip to content

Commit 0c6b539

Browse files
committed
magicbot: feedback: Allow fixed-length homogenous tuples
1 parent 8b94e8e commit 0c6b539

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

magicbot/magic_tunable.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,8 +285,10 @@ def _get_topic_type(
285285
origin = getattr(return_annotation, "__origin__", None)
286286
args = typing.get_args(return_annotation)
287287
if origin in (list, tuple, collections.abc.Sequence) and args:
288-
# Ensure tuples are tuple[T, ...]
289-
if origin is tuple and not (len(args) == 2 and args[1] is Ellipsis):
288+
# Ensure tuples are tuple[T, ...] or homogenous
289+
if origin is tuple and not (
290+
(len(args) == 2 and args[1] is Ellipsis) or len(set(args)) == 1
291+
):
290292
return None
291293

292294
inner_type = args[0]

tests/test_magicbot_feedback.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Sequence
1+
from typing import Sequence, Tuple
22

33
import ntcore
44
from wpimath import geometry
@@ -32,6 +32,10 @@ def get_rotation(self) -> geometry.Rotation2d:
3232
def get_rotation_array(self) -> Sequence[geometry.Rotation2d]:
3333
return [geometry.Rotation2d()]
3434

35+
@magicbot.feedback
36+
def get_rotation_2_tuple(self) -> Tuple[geometry.Rotation2d, geometry.Rotation2d]:
37+
return (geometry.Rotation2d(), geometry.Rotation2d())
38+
3539
@magicbot.feedback
3640
def get_int(self) -> int:
3741
return 0
@@ -90,6 +94,11 @@ def test_feedbacks_with_type_hints():
9094

9195
for name, struct_type, value in (
9296
("type_hinted/rotation_array", geometry.Rotation2d, [geometry.Rotation2d()]),
97+
(
98+
"type_hinted/rotation_2_tuple",
99+
geometry.Rotation2d,
100+
[geometry.Rotation2d(), geometry.Rotation2d()],
101+
),
93102
):
94103
assert nt.getTopic(name).getTypeString() == f"struct:{struct_type.__name__}[]"
95104
topic = nt.getStructArrayTopic(name, struct_type)

0 commit comments

Comments
 (0)