PoC: Have a go at fixing class MotorsBus(Protocol) #508
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
In the Robot class, we have the comment:
# TODO(rcadene, aliberts): Add unit test checking the protocol is implemented in the corresponding classes
(CC: @Cadene @aliberts I guess)
This is a proof of concept that it is possible to get a type checker to check our Protocol implementations.
In my case, I was using the pylance integration in vscode, and ignoring all of the other warnings and errors. I can have a crack at adding a mypy check in CI, but it might be quite invasive, and I haven't thought about how to split the work up and do it incrementally.
Making MotorsBus be an abstract base class would be an alternative way to do this. That would have the advantage of making "go to implementations" work in IDEs (I've not tested that approach yet). The disadvantage of abstract base class is that it becomes tempting to start using the Template Method Pattern and move some logic into the abstract base class (this can get difficult to reason about really quickly). If you want me to have a go at that for compariasson, I can.
If you like this static type checking approach, I can try cleaning this PR up. Things that would need to be done include:
values
type and return type for apply_calibration() and revert_calibration()NDArray[np.int64] | NDArray[np.float64] | list[int] | list[float]
everywhereA follow-up PR could then do:
What this does
Explain what this PR does. Feel free to tag your PR with the appropriate label(s).
Examples:
How it was tested
Explain/show how you tested your changes.
apply_calibration()
, so the type checker thinks it returns None.How to checkout & try? (for the reviewer)
(see above vscode example)
This is a very early Proof of Concept, and I didn't spend much time on it. I would appreciate feedback on this approach, and if I should keep going down the static type checking route, or whether we want to just rely on unit tests for this kind of thing. If we don't want to take this approach then feel free to close this PR.