8000 Add type hinting (#470) · robotpy/robotpy-wpilib@29845e2 · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Nov 13, 2023. It is now read-only.

Commit 29845e2

Browse files
ArchdukeTimvirtuald
authored andcommitted
Add type hinting (#470)
* Add PEP484 type hints * Remove unnecessaary parentheses * Add Optional type to None defaults * Remove simport typehint * Add type to Callable * Remove sensorbase imports * Found more incorrect type hints * Nitpick * Nitpick * Use generic for default values * Add generic to sendablechooser. Put vararg type hints back * Use Iterables for all NT array types * Use variable length tuple type hint * Covariance for SendableChooser * Use Any type for sendablechooser
1 parent 91a0e00 commit 29845e2

File tree

101 files changed

+1172
-1210
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

101 files changed

+1172
-1210
lines changed

wpilib/wpilib/adxl345_i2c.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
# must be accompanied by the FIRST BSD license file in the root directory of
66
# the project.
77
# ----------------------------------------------------------------------------
8+
from typing import Tuple, Optional
89

910
import hal
10-
11-
from .interfaces import Accelerometer
1211
from .i2c import I2C
1312
from .sendablebase import SendableBase
13+
from .interfaces import Accelerometer
14+
from .sendablebuilder import SendableBuilder
1415

1516
__all__ = ["ADXL345_I2C"]
1617

@@ -44,13 +45,13 @@ class Axes:
4445
kY = 0x02
4546
kZ = 0x04
4647

47-
def __init__(self, port, range, address=None):
48+
def __init__(
49+
self, port: I2C.Port, range: Range, address: Optional[int] = None
50+
) -> None:
4851
"""Constructor.
4952
5053
:param port: The I2C port the accelerometer is attached to.
51-
:type port: :class:`.I2C.Port`
5254
:param range: The range (+ or -) that the accelerometer will measure.
53-
:type range: :class:`.ADXL345_I2C.Range`
5455
:param address: the I2C address of the accelerometer (0x1D or 0x53)
5556
"""
5657
if address is None:
@@ -69,18 +70,17 @@ def __init__(self, port, range, address=None):
6970

7071
self.setName("ADXL345_I2C", port)
7172

72-
def close(self):
73+
def close(self) -> None:
7374
self.i2c.close()
7475
super().close()
7576

7677
# Accelerometer interface
7778

78-
def setRange(self, range):
79+
def setRange(self, range: Range) -> None:
7980
"""Set the measuring range of the accelerometer.
8081
8182
:param range: The maximum acceleration, positive or negative, that
8283
the accelerometer will measure.
83-
:type range: :class:`ADXL345_I2C.Range`
8484
"""
8585
if range == self.Range.k2G:
8686
value = 0
@@ -96,28 +96,28 @@ def setRange(self, range):
9696
# Specify the data format to read
9797
self.i2c.write(self.kDataFormatRegister, self.kDataFormat_FullRes | value)
9898

99-
def getX(self):
99+
def getX(self) -> float:
100100
"""Get the x axis acceleration
101101
102102
:returns: The acceleration along the x axis in g-forces
103103
"""
104104
return self.getAcceleration(self.Axes.kX 7802 )
105105

106-
def getY(self):
106+
def getY(self) -> float:
107107
"""Get the y axis acceleration
108108
109109
:returns: The acceleration along the y axis in g-forces
110110
"""
111111
return self.getAcceleration(self.Axes.kY)
112112

113-
def getZ(self):
113+
def getZ(self) -> float:
114114
"""Get the z axis acceleration
115115
116116
:returns: The acceleration along the z axis in g-forces
117117
"""
118118
return self.getAcceleration(self.Axes.kZ)
119119

120-
def getAcceleration(self, axis):
120+
def getAcceleration(self, axis: Axes) -> float:
121121
"""Get the acceleration of one axis in Gs.
122122
123123
:param axis: The axis to read from.
@@ -128,7 +128,7 @@ def getAcceleration(self, axis):
128128
rawAccel = (data[1] << 8) | data[0]
129129
return rawAccel * self.kGsPerLSB
130130

131-
def getAccelerations(self):
131+
def getAccelerations(self) -> Tuple[float, float, float]:
132132
"""Get the acceleration of all axes in Gs.
133133
134134
:returns: X,Y,Z tuple of acceleration measured on all axes of the
@@ -147,13 +147,13 @@ def getAccelerations(self):
147147
rawData[2] * self.kGsPerLSB,
148148
)
149149

150-
def _updateValues(self):
150+
def _updateValues(self) -> None:
151151
data = self.getAccelerations()
152152
self._entryX.setDouble(data[0])
153153
self._entryY.setDouble(data[1])
154154
self._entryZ.setDouble(data[2])
155155

156-
def initSendable(self, builder):
156+
def initSendable(self, builder: SendableBuilder) -> None:
157157
builder.setSmartDashboardType("3AxisAccelerometer")
158158
self._entryX = builder.getEntry("X")
159159
self._entryY = builder.getEntry("Y")

wpilib/wpilib/adxl345_spi.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
# must be accompanied by the FIRST BSD license file in the root directory of
66
# the project.
77
# ----------------------------------------------------------------------------
8+
from typing import Tuple
89

910
import hal
1011

1112
from .interfaces import Accelerometer
1213
from .spi import SPI
1314
from .sendablebase import SendableBase
15+
from .sendablebuilder import SendableBuilder
1416

1517
__all__ = ["ADXL345_SPI"]
1618

@@ -48,14 +50,12 @@ class Axes:
4850
kY = 0x02
4951
kZ = 0x04
5052

51-
def __init__(self, port, range):
53+
def __init__(self, port: SPI.Port, range: Range) -> None:
5254
"""Constructor. Use this when the device is the first/only device on
5355
the bus
5456
5557
:param port: The SPI port that the accelerometer is connected to
56-
:type port: :class:`.SPI.Port`
5758
:param range: The range (+ or -) that the accelerometer will measure.
58-
:type range: :class:`.ADXL345_SPI.Range`
5959
"""
6060
self.spi = SPI(port)
6161
self.spi.setClockRate(500000)
@@ -75,18 +75,17 @@ def __init__(self, port, range):
7575

7676
self.setName("ADXL345_SPI", port)
7777

78-
def close(self):
78+
def close(self) -> None:
7979
self.spi.close()
8080
super().close()
8181

8282
# Accelerometer interface
8383

84-
def setRange(self, range):
84+
def setRange(self, range: Range) -> None:
8585
"""Set the measuring range of the accelerometer.
8686
8787
:param range: The maximum acceleration, positive or negative, that
8888
the accelerometer will measure.
89-
:type range: :class:`ADXL345_SPI.Range`
9089
"""
9190
if range == self.Range.k2G:
9291
value = 0
@@ -101,28 +100,28 @@ def setRange(self, range):
101100

102101
self.spi.write([self.kDataFormatRegister, self.kDataFormat_FullRes | value])
103102

104-
def getX(self):
103+
def getX(self) -> float:
105104
"""Get the x axis acceleration
106105
107106
:returns: The acceleration along the x axis in g-forces
108107
"""
109108
return self.getAcceleration(self.Axes.kX)
110109

111-
def getY(self):
110+
def getY(self) -> float:
112111
"""Get the y axis acceleration
113112
114113
:returns: The acceleration along the y axis in g-forces
115114
"""
116115
return self.getAcceleration(self.Axes.kY)
117116

118-
def getZ(self):
117+
def getZ(self) -> float:
119118
"""Get the z axis acceleration
120119
121120
:returns: The acceleration along the z axis in g-forces
122121
"""
123122
return self.getAcceleration(self.Axes.kZ)
124123

125-
def getAcceleration(self, axis):
124+
def getAcceleration(self, axis: Axes) -> float:
126125
"""Get the acceleration of one axis in Gs.
127126
128127
:param axis: The axis to read from.
@@ -138,7 +137,7 @@ def getAcceleration(self, axis):
138137
rawAccel = (data[2] << 8) | data[1]
139138
return rawAccel * self.kGsPerLSB
140139

141-
def getAccelerations(self):
140+
def getAccelerations(self) -> Tuple[float, float, float]:
142141
"""Get the acceleration of all axes in Gs.
143142
144143
:returns: X,Y,Z tuple of acceleration measured on all axes of the
@@ -161,13 +160,13 @@ def getAccelerations(self):
161160
)
162161

163162
# Live Window code, only does anything if live window is activated.
164-
def _updateValues(self):
163+
def _updateValues(self) -> None:
165164
data = self.getAccelerations()
166165
self._entryX.setDouble(data[0])
167166
self._entryY.setDouble(data[1])
168167
self._entryZ.setDouble(data[2])
169168

170-
def initSendable(self, builder):
169+
def initSendable(self, builder: SendableBuilder) -> None:
171170
builder.setSmartDashboardType("3AxisAccelerometer")
172171
self._entryX = builder.getEntry("X")
173172
self._entryY = builder.getEntry("Y")

wpilib/wpilib/adxl362.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55
# must be accompanied by the FIRST BSD license file in the root directory of
66
# the project.
77
# ----------------------------------------------------------------------------
8+
from typing import Tuple, Optional
89

910
import hal
1011

1112
from .driverstation import DriverStation
1213
from .interfaces import Accelerometer
1314
from .spi import SPI
1415
from .sendablebase import SendableBase
16+
from .sendablebuilder import SendableBuilder
1517

1618
__all__ = ["ADXL362"]
1719

@@ -49,13 +51,11 @@ class Axes:
4951
kY = 0x02
5052
kZ = 0x04
5153

52-
def __init__(self, range, port=None):
54+
def __init__(self, range: Range, port: Optional[SPI.Port] = None) -> None:
5355
"""Constructor.
5456
5557
:param range: The range (+ or -) that the accelerometer will measure.
56-
:type range: :class:`.ADXL362.Range`
5758
:param port: The SPI port that the accelerometer is connected to
58-
:type port: :class:`.SPI.Port`
5959
"""
6060
if port is None:
6161
port = SPI.Port.kOnboardCS1
@@ -93,20 +93,19 @@ def __init__(self, range, port=None):
9393

9494
self.setName("ADXL362", port)
9595

96-
def close(self):
96+
def close(self) -> None:
9797
if self.spi:
9898
self.spi.close()
9999
self.spi = None
100100
super().close()
101101

102102
# Accelerometer interface
103103

104-
def setRange(self, range):
104+
def setRange(self, range: Range) -> None:
105105
"""Set the measuring range of the accelerometer.
106106
107107
:param range: The maximum acceleration, positive or negative, that
108108
the accelerometer will measure.
109-
:type range: :class:`ADXL362.Range`
110109
"""
111110
if not self.spi:
112111
return
@@ -128,28 +127,28 @@ def setRange(self, range):
128127
[self.kRegWrite, self.kFilterCtlRegister, self.kFilterCtl_ODR_100Hz | value]
129128
)
130129

131-
def getX(self):
130+
def getX(self) -> float:
132131
"""Get the x axis acceleration
133132
134133
:returns: The acceleration along the x axis in g-forces
135134
"""
136135
return self.getAcceleration(self.Axes.kX)
137136

138-
def getY(self):
137+
def getY(self) -> float:
139138
"""Get the y axis acceleration
140139
141140
:returns: The acceleration along the y axis in g-forces
142141
"""
143142
return self.getAcceleration(self.Axes.kY)
144143

145-
def getZ(self):
144+
def getZ(self) -> float:
146145
"""Get the z axis acceleration
147146
148147
:returns: The acceleration along the z axis in g-forces
149148
"""
150149
return self.getAcceleration(self.Axes.kZ)
151150

152-
def getAcceleration(self, axis):
151+
def getAcceleration(self, axis: Axes) -> float:
153152
"""Get the acceleration of one axis in Gs.
154153
155154
:param axis: The axis to read from.
@@ -164,7 +163,7 @@ def getAcceleration(self, axis):
164163
rawAccel = (data[2] << 8) | data[1]
165164
return rawAccel * self.gsPerLSB
166165

167-
def getAccelerations(self):
166+
def getAccelerations(self) -> Tuple[float, float, float]:
168167
"""Get the acceleration of all axes in Gs.
169168
170169
:returns: X,Y,Z tuple of acceleration measured on all axes in Gs.
@@ -189,13 +188,13 @@ def getAccelerations(self):
189188
rawData[2] * self.gsPerLSB,
190189
)
191190

192-
def _updateValues(self):
191+
def _updateValues(self) -> None:
193192
data = self.getAccelerations()
194193
self._entryX.setDouble(data[0])
195194
self._entryY.setDouble(data[1])
196195
self._entryZ.setDouble(data[2])
197196

198-
def initSendable(self, builder):
197+
def initSendable(self, builder: SendableBuilder) -> None:
199198
builder.setSmartDashboardType("3AxisAccelerometer")
200199
self._entryX = builder.getEntry("X")
201200
self._entryY = builder.getEntry("Y")

0 commit comments

Comments
 (0)
0