Conversation
In Astro Pi we were getting TypeErrors when checking len() on non-iterable variables, e.g. when a user calls set_pixel() with just three arguments. This PR ensures that the pixel has a __len__ attribute before testing its length. Also fixes checking the pixel list in set_pixels.
There was a problem hiding this comment.
Pull request overview
This PR improves error handling in the Sense HAT library by preventing TypeErrors when users pass non-iterable arguments to pixel-related methods. The changes add hasattr checks before calling len() on pixel arguments, ensuring that helpful ValueError messages are raised instead of cryptic TypeErrors.
Changes:
- Added
hasattr(_, '__len__')checks beforelen()calls inset_pixels()andset_pixel()methods to gracefully handle non-iterable arguments - Refactored validation logic in
set_pixel()to consolidate error checking in a single location - Fixed markdown heading format in README.md
Reviewed changes
Copilot reviewed 1 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| sense_hat/sense_hat.py | Added hasattr checks for pixel validation in set_pixels() and set_pixel() methods to prevent TypeErrors when non-iterable arguments are passed; consolidated validation logic in set_pixel() |
| README.md | Fixed markdown heading syntax by adding space after # |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
…`sense_hat.py` errors (#1348) Closes RaspberryPiFoundation/digital-maintenance-team#7 This commit specifically fixes the issue triggered by using incorrect arguments in the `set_pixel` and `set_pixels` functions. `TypeError: object of type 'int' has no len() on line 1081 of sense_hat.py` I've added an explicit check to see if the pixel object has a `__len__` attribute. I've opened an identical PR on the [Sense Hat library](RaspberryPiFoundation/sense_hat#1), and [here](RaspberryPiFoundation/python-sense-hat#2) and [here](astro-pi/python-sense-hat#151)! ## Traceback mangling I changed the error message when the filename is `./sense_hat.py` to point to the next file in the stack, hopefully the user's code. The issue with this is that it effectively masks errors in the `sense_hat` library, blaming the user instead. At the moment it only catches specific errors: `ValueError`, `RuntimeError` which are specifically mentioned in the sense hat shim. ## Package update This also updates the package version to 0.34.7. ## Example python ```python # Import the libraries from sense_hat import SenseHat from time import sleep # Set up the Sense HAT sense = SenseHat() sense.set_pixel(1, 2, 3) # Or: sense.set_pixels(1) # Or: # sense.set_pixels([1]*64) ``` ## Before <img width="1473" height="347" alt="image" src="https://github.com/user-attachments/assets/0f8f6e1d-42bc-454f-88b5-a7c22568c012" /> ## After Tested on https://staging-editor-static.raspberrypi.org/branches/1348_merge/web-component.html <img width="1200" height="390" alt="image" src="https://github.com/user-attachments/assets/f30ff39c-0c47-4dbf-a7ae-d1aed3951259" /> --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
G3zz
left a comment
There was a problem hiding this comment.
I have my reservations about converting TypeErrors to ValueErrors, but having looked at the PR in editor-ui I can see that this simplifies the stack trace filtering in that project.
…`sense_hat.py` errors (#1348) Closes RaspberryPiFoundation/digital-maintenance-team#7 This commit specifically fixes the issue triggered by using incorrect arguments in the `set_pixel` and `set_pixels` functions. `TypeError: object of type 'int' has no len() on line 1081 of sense_hat.py` I've added an explicit check to see if the pixel object has a `__len__` attribute. I've opened an identical PR on the [Sense Hat library](RaspberryPiFoundation/sense_hat#1), and [here](RaspberryPiFoundation/python-sense-hat#2) and [here](astro-pi/python-sense-hat#151)! ## Traceback mangling I changed the error message when the filename is `./sense_hat.py` to point to the next file in the stack, hopefully the user's code. The issue with this is that it effectively masks errors in the `sense_hat` library, blaming the user instead. At the moment it only catches specific errors: `ValueError`, `RuntimeError` which are specifically mentioned in the sense hat shim. ## Package update This also updates the package version to 0.34.7. ## Example python ```python # Import the libraries from sense_hat import SenseHat from time import sleep # Set up the Sense HAT sense = SenseHat() sense.set_pixel(1, 2, 3) # Or: sense.set_pixels(1) # Or: # sense.set_pixels([1]*64) ``` ## Before <img width="1473" height="347" alt="image" src="https://github.com/user-attachments/assets/0f8f6e1d-42bc-454f-88b5-a7c22568c012" /> ## After Tested on https://staging-editor-static.raspberrypi.org/branches/1348_merge/web-component.html <img width="1200" height="390" alt="image" src="https://github.com/user-attachments/assets/f30ff39c-0c47-4dbf-a7ae-d1aed3951259" /> --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
In Astro Pi we were getting TypeErrors when checking len() on non-iterable variables, e.g. when a user calls set_pixel() with just three arguments.
This PR ensures that the pixel has a len attribute before testing its length.
Also fixes checking the pixel list in set_pixels.
See also RaspberryPiFoundation/editor-ui#1348 and RaspberryPiFoundation/sense_hat#1