Skip to content

Change asserts to raised exceptions. #10

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

Closed
caternuson opened this issue Nov 28, 2018 · 7 comments
Closed

Change asserts to raised exceptions. #10

caternuson opened this issue Nov 28, 2018 · 7 comments

Comments

@caternuson
Copy link
Contributor

Replace asserts with appropriate raised exception with helpful message.

@ArthurDent62
Copy link
Contributor

Can you point me to a library which I can use as coding reference?

@caternuson
Copy link
Contributor Author

caternuson commented Dec 2, 2018

There are actually a few already in this library. Ex:
https://github.com/adafruit/Adafruit_CircuitPython_TLC5947/blob/master/adafruit_tlc5947.py#L190
Basically just replicate the conditional of the asserts and raise an exception if true.

if some_check:
    raise SomeException("Some useful info about what went wrong.")

Note that asserts trigger when the condition is false, so you'll want to kind of "invert" the existing checks. Also, pick an appropriate exception type. Read through the available ones and pick one that applies:
https://docs.python.org/3/library/exceptions.html#built-in-exceptions
But don't worry about it too much. If we think the exception type should change, we'll let you know as part of code review for the PR, and the change would be trivial.

@ArthurDent62
Copy link
Contributor

I think that if / raise is not pythonic - two other options come to my mind:

  1. assert statement supports helpful message (quick to do, but also not so much pythonic):
assert 0 <= channel < _CHANNELS * self._n , \
        "'channel' out of range (0,%s)" % _CHANNELS * self._n -1 
  1. using try/catch (pythonic but it is not so easy to spot the relationship between byte_start and channel:
    try:
        high_byte = self._shift_reg[byte_start]
        low_byte = self._shift_reg[byte_start+1]
    except IndexError:
        print("'channel' out of range(0,%s)" % _CHANNELS * self._n -1)

What do you think?

@caternuson
Copy link
Contributor Author

We've been doing if / raise in other CircuitPython drivers. With 1. it's a similar approach, but doesn't allow using a different exception type. The problem with 2. is that you are handling the exception internally and not passing it on.

@ArthurDent62
Copy link
Contributor

Thanks for your insight.
When done, I presume I shall provide the changes in a separate PR than the one currently ongoing, right?

@caternuson
Copy link
Contributor Author

Yes please.

ArthurDent62 added a commit to ArthurDent62/Adafruit_CircuitPython_TLC5947 that referenced this issue Jan 8, 2019
According to Issue adafruit#10 of the main repo, exceptions shall be used instead of asserts. I'm game.
@caternuson
Copy link
Contributor Author

Fixed with #15

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

No branches or pull requests

2 participants