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

Fix a few type hints #34

merged 2 commits into from
Apr 8, 2025

Conversation

Myoldmopar
Copy link
Contributor

This PR fixes three type hints:

  • The DS18X20 class constructor was accepting an integer address, but it actually needs a OneWireAddress. I imported OneWireAddress and updated the type hint.
  • While in there, I also noticed that the temperature property was not type hinted, so I added float
  • And with a run of mypy, I picked up that the RESOLUTION tuple could be improved. As it is now, it is just a tuple of ints, not constrained to 9, 10, 11, 12. And thus, the resolution properties were not being strictly typed. I added a type hint to RESOLUTION so that each entry in the tuple 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

Copy link
Contributor Author

@Myoldmopar Myoldmopar left a 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
Copy link
Contributor Author

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

@@ -74,7 +75,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.

@@ -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:
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.

@Myoldmopar
Copy link
Contributor Author

Let me know if there's anything else needed here, otherwise I think it's ready to go. Thanks!

Copy link
Contributor

@FoamyGuy FoamyGuy left a 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!

@FoamyGuy FoamyGuy merged commit 5d8d6f8 into adafruit:main Apr 8, 2025
1 check passed
@Myoldmopar Myoldmopar deleted the FixTypeHint branch April 8, 2025 18:20
adafruit-adabot added a commit to adafruit/Adafruit_CircuitPython_Bundle that referenced this pull request Apr 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Incorrect type hint for DS18X20 class constructor
2 participants