Skip to content

Commit ffbeeca

Browse files
committed
better error messages
1 parent 04d90d7 commit ffbeeca

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

commands2/button/joystickbutton.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,10 @@ def __init__(self, joystick: Joystick, button: int) -> None:
1818
:param joystick: The joystick on which the button is located.
1919
:param button: The number of the button on the joystick.
2020
"""
21+
if not isinstance(joystick, Joystick) or not isinstance(button, int):
22+
raise TypeError(
23+
"JoystickButton.__init__(): incompatible constructor arguments. The following argument types are supported:\n"
24+
"\t1. commands2.button.JoystickButton(joystick: Joystick, button: int)\n"
25+
f"Invoked with: {joystick}, {button}"
26+
)
2127
super().__init__(lambda: joystick.getRawButton(button))

commands2/button/networkbutton.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ def __init__(self, *args, **kwargs) -> None:
3434
num_args = len(args) + len(kwargs)
3535
if num_args == 1:
3636
entry: NetworkTableEntry = kwargs.get("entry") or args[0]
37+
38+
if not isinstance(entry, NetworkTableEntry):
39+
raise self._type_error(entry)
40+
3741
super().__init__(
3842
lambda: NetworkTables.isConnected() and entry.getBoolean(False)
3943
)
@@ -47,6 +51,13 @@ def __init__(self, *args, **kwargs) -> None:
4751
entry = table.getEntry(field)
4852
self.__init__(entry)
4953
else:
50-
raise TypeError(
51-
f"__init__() takes 1 or 2 positional arguments but {num_args} were given"
52-
)
54+
raise self._type_error(args)
55+
56+
def _type_error(self, *args):
57+
return TypeError(
58+
"NetworkButton.__init__(): incompatible constructor arguments. The following argument types are supported:\n"
59+
"\t1. commands2.button.NetworkButton(entry: NetworkTableEntry)\n"
60+
"\t2. commands2.button.NetworkButton(table: str, field: str)\n"
61+
"\t3. commands2.button.NetworkButton(table: NetworkTable, field: str)\n"
62+
f"Invoked with: {', '.join(map(str, args))}"
63+
)

commands2/button/povbutton.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,10 @@ def __init__(self, joystick: Joystick, angle: int, povNumber: int = 0) -> None:
1919
:param angle: The angle of the POV corresponding to a button press.
2020
:param povNumber: The number of the POV on the joystick.
2121
"""
22+
if not isinstance(joystick, Joystick) or not isinstance(angle, int) or not isinstance(povNumber, int):
23+
raise TypeError(
24+
"POVButton.__init__(): incompatible constructor arguments. The following argument types are supported:\n"
25+
"\t1. commands2.button.POVButton(joystick: Joystick, angle: int, povNumber: int)\n"
26+
f"Invoked with: {joystick}, {angle}, {povNumber}"
27+
)
2228
super().__init__(lambda: joystick.getPOV(povNumber) == angle)

0 commit comments

Comments
 (0)