-
Notifications
You must be signed in to change notification settings - Fork 15
add type hints, restrict return type of monitored callable #39
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.
Good use of Callable
! A few changes and requests for a second look from @FoamyGuy!
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.
Looking for a second opinion again from FoamyGuy
Note that, despite the description, class InputOutputType(Protocol):
value: bool |
@Neradoc oh very good point about there being other classes that make use of @awordforthat, you can add a Note that Python 3.7 has the |
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.
Thanks for working on this @awordforthat
A few things tekktrik noted I think we do want to do. The int()
calls I am open to including but also open to thoughts from other folks and definitely think we want to test it to make sure no unintended effects.
As for restricting the function to one returning a boolean, I'm for specifying it in the docstring and types (although I didn't know it was possible to do in the types, but that looks like what you've done for the Callable
neat usage of that). But I don't think we should put any further technical restriction on it necessarily like checking for it and raising a exception.
I think it's good to point folks toward using functions that return bools, but if someone has a different use case that doesn't happen to return bool but still does what they want then I think we should allow that behavior.
Thanks all for your input! I agree with everyone that we should not restrict the type of value that this can monitor. I'll get to work on cleaning up the rest of the types :) |
@Neradoc |
I don't know, it could be anything that converts to bool I guess. Adafruit_CircuitPython_Debouncer/adafruit_debouncer.py Lines 80 to 83 in e7a969f
Also there is |
@Neradoc ah shoot you're right. All of the get/set/toggle methods also "expect" a boolean, although they'd work with some other values. Since it's only a hint and not a restriction, I guess I'm ok typing it as |
I'm torn here. However, if we want to type for "truthiness", I think this is one of the rare times we can type the return as My vote is for the former, but am curious what others think. |
@echarles-dev are you still working on this one to make the discussed changes? I will try to wrap it up with these changes next week if you're not, or don't have time between now and then. |
Update: adafruit/Adafruit_CircuitPython_Typing#18 defined a Protocol for classes defining a |
I believe I made all of the changes requested in prior feedback with the latest commit. @tekktrik if you have a chance want to take a another look over this one? |
Sure thing! I can review this tonight. |
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.
Looks good to me! Thanks y'all!
Updating https://github.com/adafruit/Adafruit_CircuitPython_Debouncer to 2.0.1 from 2.0.0: > Merge pull request adafruit/Adafruit_CircuitPython_Debouncer#39 from awordforthat/add-typing > Set language to "en" for documentation > Switch to inclusive terminology > Increase min lines similarity > Patch .pre-commit-config.yaml > change discord badge > Patch: Replaced discord badge image > Updated gitignore > Update Black to latest. Updating https://github.com/adafruit/Adafruit_CircuitPython_Logging to 4.0.0 from 3.7.6: > Merge pull request adafruit/Adafruit_CircuitPython_Logging#28 from tekktrik/dev/cpython-subset > Set language to "en" for documentation > Switch to inclusive terminology > Increase min lines similarity > Patch .pre-commit-config.yaml
This is part of the effort to add type hinting to existing libraries.
As part of this, I'm suggesting a minor modification to the library. As written, you can monitor a function as if it were a digital input. This isn't a problem, theoretically. However, the library allows you to request the current value of the debounced input, which either returns a
bool
(if you're debouncing a DigitalInOut) or literally anything if you're using a function. I'm suggesting that debounced function should be restricted to returning abool
so the behavior of this debouncer is more predictable.Suggestions/comments welcome!