Skip to content

Ensure pixels have __len__ before checking with len()#1

Open
patch0 wants to merge 1 commit intomainfrom
fix-pixel-error-checks
Open

Ensure pixels have __len__ before checking with len()#1
patch0 wants to merge 1 commit intomainfrom
fix-pixel-error-checks

Conversation

@patch0
Copy link
Copy Markdown

@patch0 patch0 commented Feb 27, 2026

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.

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`.
Copilot AI review requested due to automatic review settings February 27, 2026 08:17
@patch0 patch0 self-assigned this Feb 27, 2026
@patch0 patch0 requested a review from G3zz February 27, 2026 08:18
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 before len() calls in set_pixels() for both the pixel list and individual pixels
  • Refactored set_pixel() to consolidate validation logic and add hasattr(..., '__len__') check before len() call

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

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>
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

Successfully merging this pull request may close these issues.

2 participants