Ensure pixels have __len__ before checking with len()#1
Open
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 fixes TypeErrors that occur when users pass non-iterable arguments (e.g., integers) to pixel-setting methods. The changes add defensive checks using hasattr(obj, '__len__') before calling len() on user-provided inputs, preventing crashes and providing clearer error messages.
Changes:
- Added
hasattr(..., '__len__')checks beforelen()calls inset_pixels()for both the pixel list and individual pixels - Refactored
set_pixel()to consolidate validation logic and addhasattr(..., '__len__')check beforelen()call
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This was referenced Feb 27, 2026
patch0
added a commit
to RaspberryPiFoundation/editor-ui
that referenced
this pull request
Feb 27, 2026
…`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>
jamiebenstead
pushed a commit
to RaspberryPiFoundation/editor-ui
that referenced
this pull request
Mar 3, 2026
…`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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 Astro Pi we were getting TypeErrors when checking
len()on non-iterable variables, e.g. when a user callsset_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.