-
Notifications
You must be signed in to change notification settings - Fork 15
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
Conversation
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.
Added a little line-by-line walkthrough of the changes.
@@ -29,14 +29,15 @@ | |||
from typing_extensions import Literal | |||
from circuitpython_typing import WriteableBuffer | |||
from adafruit_onewire.bus import OneWireBus # pylint: disable=ungrouped-imports | |||
from adafruit_onewire.device import OneWireAddress |
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.
New import only for type hinting, so it is inside the try block.
except ImportError: | ||
pass | ||
|
||
_CONVERT = b"\x44" | ||
_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) |
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.
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.
@@ -74,7 +75,7 @@ class DS18X20: | |||
|
|||
""" | |||
|
|||
def __init__(self, bus: OneWireBus, address: int) -> None: | |||
def __init__(self, bus: OneWireBus, address: OneWireAddress) -> None: |
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.
The main fix here - it's not an int
, it's a OneWireAddress
instance.
@@ -84,7 +85,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: |
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.
Minor tweak, this returns a float for the temperature.
Let me know if there's anything else needed here, otherwise I think it's ready to go. Thanks! |
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.
Thank you, appreciate the fix and type improvements!
Updating https://github.com/adafruit/Adafruit_CircuitPython_DS18X20 to 1.4.4 from 1.4.3: > Merge pull request adafruit/Adafruit_CircuitPython_DS18X20#34 from Myoldmopar/FixTypeHint
This PR fixes three type hints:
OneWireAddress
. I importedOneWireAddress
and updated the type hint.temperature
property was not type hinted, so I addedfloat
mypy
, I picked up that theRESOLUTION
tuple could be improved. As it is now, it is just a tuple of ints, not constrained to 9, 10, 11, 12. And thus, theresolution
properties were not being strictly typed. I added a type hint toRESOLUTION
so that each entry in thetuple
is constrained to the values 9, 10, 11, 12, and now it's more strictly typing that property.If you need me to add more explanation, change anything, or pull any of the changes back out, I'm happy to do it. I'm not planning on setting up a full dev environment to run the CI suite locally, so I'll watch to see any results.
Thanks!
Fixes #33