Skip to content

Fix a few type hints #34

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
Apr 8, 2025
Merged
Changes from 1 commit
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
8 changes: 4 additions & 4 deletions adafruit_ds18x20.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

import time
from micropython import const
from adafruit_onewire.device import OneWireDevice
from adafruit_onewire.device import OneWireAddress, OneWireDevice

try:
import typing # pylint: disable=unused-import
Expand All @@ -36,7 +36,7 @@
_RD_SCRATCH = b"\xBE"
_WR_SCRATCH = b"\x4E"
_CONVERSION_TIMEOUT = const(1)
RESOLUTION = (9, 10, 11, 12)
RESOLUTION: tuple[Literal[9, 10, 11, 12], ...] = (9, 10, 11, 12)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Declares RESOLUTION as a tuple where each value is in the set (9, 10, 11, 12). This allows IDEs and type checkers to more strictly validate the values being accessed inside this tuple variable.

# Maximum conversion delay in seconds, from DS18B20 datasheet.
_CONVERSION_DELAY = {9: 0.09375, 10: 0.1875, 11: 0.375, 12: 0.750}

Expand Down Expand Up @@ -74,7 +74,7 @@ class DS18X20:

"""

def __init__(self, bus: OneWireBus, address: int) -> None:
def __init__(self, bus: OneWireBus, address: OneWireAddress) -> None:
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The main fix here - it's not an int, it's a OneWireAddress instance.

if address.family_code in (0x10, 0x28):
self._address = address
self._device = OneWireDevice(bus, address)
Expand All @@ -84,7 +84,7 @@ def __init__(self, bus: OneWireBus, address: int) -> None:
raise ValueError("Incorrect family code in device address.")

@property
def temperature(self):
def temperature(self) -> float:
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Minor tweak, this returns a float for the temperature.

"""The temperature in degrees Celsius."""
self._convert_temp()
return self._read_temp()
Expand Down