-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add typing #19
Add typing #19
Changes from 3 commits
a39d006
ec1b921
0e2fbac
5584df4
08b0ecf
a226483
c4d7fe2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,14 @@ | |
import digitalio | ||
import adafruit_bus_device.spi_device as spi_device | ||
|
||
try: | ||
import typing | ||
from _typing import ReadableBuffer | ||
from microcontroller import Pin | ||
import busio | ||
except ImportError: | ||
pass | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You will have to import the values being typed against, e.g. microcontroller.Pin in a try block like can be seen in the issue. For another example see: adafruit/Adafruit_CircuitPython_NeoPixel@eab2911 I believe it will also be required for ReadableBuffer. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Got it, thanks for the feedback! I'll resubmit with those imports. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @gamblor21 - It's giving me a failure for pylint because of the unused typing module. Any advice on how to proceed? Will using those imports eat up too much RAM, or should I just remove the try statement in this case? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think Foamyguy answered this below with the pylint ignore comment? And I do not see the imports would eat away at RAM. I'm guessing in almost all cases if you import the library that requires those types, you will require the imports as well. Sorry was busy the last couple days so didn't reply right away. |
||
__version__ = "0.0.0-auto.0" | ||
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_74HC595.git" | ||
|
||
|
@@ -39,7 +47,11 @@ class DigitalInOut: | |
direction as input will raise an exception. | ||
""" | ||
|
||
def __init__(self, pin_number, shift_register_74hc595): | ||
def __init__( | ||
self, | ||
pin_number: Pin, | ||
shift_register_74hc595: "ShiftRegister74HC595", | ||
): | ||
"""Specify the pin number of the shift register (0...7) and | ||
ShiftRegister74HC595 instance. | ||
""" | ||
|
@@ -53,7 +65,7 @@ def __init__(self, pin_number, shift_register_74hc595): | |
# is unused by this class). Do not remove them, instead turn off pylint | ||
# in this case. | ||
# pylint: disable=unused-argument | ||
def switch_to_output(self, value=False, **kwargs): | ||
def switch_to_output(self, value: bool = False, **kwargs): | ||
"""``DigitalInOut switch_to_output``""" | ||
self.direction = digitalio.Direction.OUTPUT | ||
self.value = value | ||
|
@@ -72,7 +84,7 @@ def value(self): | |
) | ||
|
||
@value.setter | ||
def value(self, val): | ||
def value(self, val: bool): | ||
|
||
if ( | ||
self._pin >= 0 | ||
|
@@ -91,7 +103,7 @@ def direction(self): | |
return digitalio.Direction.OUTPUT | ||
|
||
@direction.setter | ||
def direction(self, val): # pylint: disable=no-self-use | ||
def direction(self, val: digitalio.Direction): # pylint: disable=no-self-use | ||
"""``Direction`` can only be set to ``OUTPUT``.""" | ||
if val != digitalio.Direction.OUTPUT: | ||
raise RuntimeError("Digital input not supported.") | ||
|
@@ -102,7 +114,7 @@ def pull(self): | |
return None | ||
|
||
@pull.setter | ||
def pull(self, val): # pylint: disable=no-self-use | ||
def pull(self, val: digitalio.Pull): # pylint: disable=no-self-use | ||
"""Only supports null/no pull state.""" | ||
if val is not None: | ||
raise RuntimeError("Pull-up and pull-down not supported.") | ||
|
@@ -113,7 +125,12 @@ class ShiftRegister74HC595: | |
and indicate the number of shift registers being used | ||
""" | ||
|
||
def __init__(self, spi, latch, number_of_shift_registers=1): | ||
def __init__( | ||
self, | ||
spi: busio.I2C, | ||
FoamyGuy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
latch: digitalio.DigitalInOut, | ||
number_of_shift_registers: int = 1, | ||
): | ||
self._device = spi_device.SPIDevice(spi, latch, baudrate=1000000) | ||
self._number_of_shift_registers = number_of_shift_registers | ||
self._gpio = bytearray(self._number_of_shift_registers) | ||
|
@@ -131,14 +148,14 @@ def gpio(self): | |
return self._gpio | ||
|
||
@gpio.setter | ||
def gpio(self, val): | ||
def gpio(self, val: ReadableBuffer): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can use |
||
self._gpio = val | ||
|
||
with self._device as spi: | ||
# pylint: disable=no-member | ||
spi.write(self._gpio) | ||
|
||
def get_pin(self, pin): | ||
def get_pin(self, pin: int) -> Pin: | ||
"""Convenience function to create an instance of the DigitalInOut class | ||
pointing at the specified pin of this 74HC595 device . | ||
""" | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can add a pylint ignore comment for this import.
The importing module doesn't get used from the rest of code, but it does serve the purpose of determining if the environment has typing.