Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding __contains__ stub to displayio.Group #7949

Merged
merged 2 commits into from
May 11, 2023

Conversation

FoamyGuy
Copy link
Collaborator

@FoamyGuy FoamyGuy commented May 8, 2023

resolves: #7948

I noticed that mypy raises this error while testing some of the suggested fixes discussed during the weeds section of the meeting inside of this PR: adafruit/Adafruit_CircuitPython_DisplayIO_Layout#85

This topic of the in operator didn't come up, but I noticed mypy outputting errors about it while working on that.

I tested the change successfully by building stubs and installing with pip install . and then running mypy on that PR branch again. Confirmed that it no longer outputs the error.

Copy link
Member

@tannewt tannewt left a comment

Choose a reason for hiding this comment

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

Does it actually work? It isn't clear to me how it works.

@FoamyGuy
Copy link
Collaborator Author

FoamyGuy commented May 9, 2023

Does it actually work? It isn't clear to me how it works.

It does work to eliminate that error from mypy. I'm not entirely sure of the 'how' beyond that mypy is looking at your installed libraries for known types when it runs to try to enforce it's rules. circuitpython-stubs seems to be where it's taking it's definition for displayio.Group.

I saw your message on the issue as well. Iter does feel more appropriate for this instance, although I think Group may support both. I'll double check whether it does support if something in group and if not I'll change the contains one to iter. If it supports both is it worth leaving contains and adding iter to the stubs? It may not make a difference for mypy since it does seem slightly confused about which one is used, but it will add more specific clarity in the docs pages.

@tannewt
Copy link
Member

tannewt commented May 10, 2023

If it supports both is it worth leaving contains and adding iter to the stubs?

Yup. But I don't think it supports contains. :-) I was talking about how it was implemented in CP. mypy is just a knock on effect.

@FoamyGuy
Copy link
Collaborator Author

FoamyGuy commented May 11, 2023

Latest commit adds __iter__stub.

As far as I can tell it supports both for _ in Group and if _ in Group. The following code executes successfully and outputs expected results:

import displayio
import board

bmp = displayio.Bitmap(100,100, 1)
palette = displayio.Palette(1)
palette[0] = 0x00ff00
tilegrid = displayio.TileGrid(bitmap=bmp, pixel_shader=palette)
bmp.fill(0)
main_group = displayio.Group()

main_group.append(tilegrid)
board.DISPLAY.show(main_group)

if tilegrid in main_group:
    print("tilegrid is in main_group")
else:
    print("Not in main group")

for _item in main_group:
    print(_item)
while True:
    pass

Output:

code.py output:
tilegrid is in main_group
<TileGrid>

Tested on:

Adafruit CircuitPython 8.1.0-beta.2 on 2023-04-26; Adafruit Feather ESP32-S2 TFT with ESP32S2
Board ID:adafruit_feather_esp32s2_tft

Copy link
Member

@tannewt tannewt left a comment

Choose a reason for hiding this comment

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

Thanks!

@tannewt tannewt merged commit c75a768 into adafruit:main May 11, 2023
347 checks passed
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.

mypy is unaware that displayio.Group supports the in operator
2 participants