Skip to content

Adjust pixel length checks#151

Merged
G3zz merged 2 commits intoastro-pi:masterfrom
RaspberryPiFoundation:adjust-pixel-length-checks
Mar 2, 2026
Merged

Adjust pixel length checks#151
G3zz merged 2 commits intoastro-pi:masterfrom
RaspberryPiFoundation:adjust-pixel-length-checks

Conversation

@patch0
Copy link
Contributor

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

See also RaspberryPiFoundation/editor-ui#1348 and RaspberryPiFoundation/sense_hat#1

grega and others added 2 commits January 6, 2026 16:50
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:52
@patch0 patch0 self-assigned this Feb 27, 2026
@patch0 patch0 requested a review from G3zz February 27, 2026 08:52
Copy link

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 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 before len() calls in set_pixels() and set_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.

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>
Copy link
Contributor

@G3zz G3zz left a comment

Choose a reason for hiding this comment

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

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.

@G3zz G3zz merged commit 48b9a55 into astro-pi:master Mar 2, 2026
4 checks passed
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.

4 participants