Skip to content

adds Type Annotations to address Issue #17 #18

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

Merged
merged 2 commits into from
Feb 13, 2022
Merged
Changes from all 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
47 changes: 31 additions & 16 deletions adafruit_mcp9600.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@
from adafruit_register.i2c_bits import RWBits, ROBits
from adafruit_register.i2c_bit import RWBit, ROBit

try:
# Used only for typing
import typing # pylint: disable=unused-import
from busio import I2C
Copy link
Contributor

Choose a reason for hiding this comment

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

Lets add an "unused" typing import here as the first import so that the exception will get raised on the microcontroller when it runs. That will prevent importing ITC when it's not needed at runtime on the microcontroller. There is an example in this library:

https://github.com/adafruit/Adafruit_CircuitPython_NTP/blob/acc7d3608aa33e4740f9ac28eb4d5230b5eb86d7/adafruit_ntp.py#L26-L27

except ImportError:
pass

__version__ = "0.0.0-auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_MCP9600.git"

Expand Down Expand Up @@ -202,7 +209,13 @@ class MCP9600:

types = ("K", "J", "T", "N", "S", "E", "B", "R")

def __init__(self, i2c, address=_DEFAULT_ADDRESS, tctype="K", tcfilter=0):
def __init__(
self,
i2c: I2C,
address: int = _DEFAULT_ADDRESS,
tctype: str = "K",
tcfilter: int = 0,
) -> None:
self.buf = bytearray(3)
self.i2c_device = I2CDevice(i2c, address)
self.type = tctype
Expand All @@ -224,14 +237,14 @@ def __init__(self, i2c, address=_DEFAULT_ADDRESS, tctype="K", tcfilter=0):
def alert_config(
self,
*,
alert_number,
alert_temp_source,
alert_temp_limit,
alert_hysteresis,
alert_temp_direction,
alert_mode,
alert_state
):
alert_number: int,
alert_temp_source: int,
alert_temp_limit: float,
alert_hysteresis: float,
alert_temp_direction: int,
alert_mode: int,
alert_state: int
) -> None:
"""Configure a specified alert pin. Alert is enabled by default when alert is configured.
To disable an alert pin, use :meth:`alert_disable`.

Expand Down Expand Up @@ -296,7 +309,7 @@ def alert_config(
setattr(self, "_alert_%d_state" % alert_number, alert_state)
setattr(self, "_alert_%d_enable" % alert_number, True)

def alert_disable(self, alert_number):
def alert_disable(self, alert_number: int) -> None:
"""Configuring an alert using :meth:`alert_config` enables the specified alert by default.
Use :meth:`alert_disable` to disable an alert pin.

Expand All @@ -307,7 +320,9 @@ def alert_disable(self, alert_number):
raise ValueError("Alert pin number must be 1-4.")
setattr(self, "_alert_%d_enable" % alert_number, False)

def alert_interrupt_clear(self, alert_number, interrupt_clear=True):
def alert_interrupt_clear(
self, alert_number: int, interrupt_clear: bool = True
) -> None:
"""Turns off the alert flag in the MCP9600, and clears the pin state (not used if the alert
is in comparator mode). Required when ``alert_mode`` is ``INTERRUPT``.

Expand All @@ -320,13 +335,13 @@ def alert_interrupt_clear(self, alert_number, interrupt_clear=True):
setattr(self, "_alert_%d_interrupt_clear" % alert_number, interrupt_clear)

@property
def version(self):
def version(self) -> int:
""" MCP9600 chip version """
data = self._read_register(_REGISTER_VERSION, 2)
return unpack(">xH", data)[0]

@property
def ambient_temperature(self):
def ambient_temperature(self) -> float:
""" Cold junction/ambient/room temperature in Celsius """
data = self._read_register(_REGISTER_COLD_JUNCTION, 2)
value = unpack(">xH", data)[0] * 0.0625
Expand All @@ -335,7 +350,7 @@ def ambient_temperature(self):
return value

@property
def temperature(self):
def temperature(self) -> float:
""" Hot junction temperature in Celsius """
data = self._read_register(_REGISTER_HOT_JUNCTION, 2)
value = unpack(">xH", data)[0] * 0.0625
Expand All @@ -344,15 +359,15 @@ def temperature(self):
return value

@property
def delta_temperature(self):
def delta_temperature(self) -> float:
""" Delta temperature in Celsius """
data = self._read_register(_REGISTER_DELTA_TEMP, 2)
value = unpack(">xH", data)[0] * 0.0625
if data[1] & 0x80:
value -= 4096
return value

def _read_register(self, reg, count=1):
def _read_register(self, reg: int, count: int = 1) -> bytearray:
self.buf[0] = reg
with self.i2c_device as i2c:
i2c.write_then_readinto(self.buf, self.buf, out_end=count, in_start=1)
Expand Down