Skip to content
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

Merged
merged 7 commits into from
Oct 30, 2021
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 25 additions & 8 deletions adafruit_74hc595.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@
import digitalio
import adafruit_bus_device.spi_device as spi_device

try:
import typing
Copy link
Contributor

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.

import typing  # pylint: disable=unused-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.

from _typing import ReadableBuffer
from microcontroller import Pin
import busio
except ImportError:
pass

Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it, thanks for the feedback! I'll resubmit with those imports.

Copy link
Member Author

Choose a reason for hiding this comment

The 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?

Copy link
Member

Choose a reason for hiding this comment

The 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"

Expand All @@ -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.
"""
Expand All @@ -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
Expand All @@ -72,7 +84,7 @@ def value(self):
)

@value.setter
def value(self, val):
def value(self, val: bool):

if (
self._pin >= 0
Expand All @@ -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.")
Expand All @@ -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.")
Expand All @@ -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)
Expand All @@ -131,14 +148,14 @@ def gpio(self):
return self._gpio

@gpio.setter
def gpio(self, val):
def gpio(self, val: ReadableBuffer):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can use bytearray here instead of ReadableBuffer I think.

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 .
"""
Expand Down