Skip to content

digital_inout.py: add exceptions when ipol register is absent #60

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 5 commits into from
Oct 8, 2024
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
19 changes: 14 additions & 5 deletions adafruit_mcp230xx/digital_inout.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,22 @@ def invert_polarity(self) -> bool:
"""The polarity of the pin, either True for an Inverted or
False for an normal.
"""
if _get_bit(self._mcp.ipol, self._pin):
return True
return False
try:
if _get_bit(self._mcp.ipol, self._pin):
return True
return False
except AttributeError:
return False
Copy link
Member

Choose a reason for hiding this comment

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

Proactively testing for the attribute is clearer than catching the error.

Suggested change
try:
if _get_bit(self._mcp.ipol, self._pin):
return True
return False
except AttributeError:
return False
if hasattr(self._mcp, "ipol") and _get_bit(self._mcp.ipol, self._pin):
return True
return False


@invert_polarity.setter
def invert_polarity(self, val: bool) -> None:
if val:
self._mcp.ipol = _enable_bit(self._mcp.ipol, self._pin)
try:
self._mcp.ipol = _enable_bit(self._mcp.ipol, self._pin)
except AttributeError as error:
raise ValueError("Inverted polarity is not supported.") from error
else:
self._mcp.ipol = _clear_bit(self._mcp.ipol, self._pin)
try:
self._mcp.ipol = _clear_bit(self._mcp.ipol, self._pin)
except AttributeError:
return