Skip to content

dir() on a CPython struct lists its fields and methods - #48

Merged
bdbarnett merged 1 commit into
mainfrom
fix/struct-dir
Oct 27, 2026
Merged

bdbarnett merged 1 commit into
mainfrom
fix/struct-dir

Conversation

@bdbarnett

@bdbarnett bdbarnett commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #40. #47 has merged, so this is based on main.

On CPython, dir() on a struct instance now lists the struct's fields and methods. Struct types resolve these by name in getattro, so object.__dir__ never saw them. Each struct type now has a __dir__ that adds them.

>>> sorted(n for n in dir(lv.grad_dsc_t()) if not n.startswith("_"))
['conical_init', 'dir', 'extend', 'horizontal_init', 'init_stops', 'linear_init', 'params', 'radial_init', 'radial_set_focal', 'state', 'stops', 'stops_count', 'vertical_init']

The MicroPython build already listed fields. On the current Unix build, dir(lv.grad_dsc_t()) shows stops, params and the gradient methods. The new test checks it there too.

The new smoke test test_struct_dir failed on CPython before this change with dir(grad_dsc_t()) is missing ['stops', 'dir', 'extend', 'params', 'linear_init']. It passes after, along with the rest of the smoke test. pytest (109) passes and regenerate_all.sh --check matches.

CPython struct types resolve fields and methods in getattro, so object.__dir__ never saw them. Each struct type now has a __dir__ that adds them.
Base automatically changed from fix/grad-helpers to main October 27, 2026 16:16
@bdbarnett
bdbarnett merged commit 800a182 into main Oct 27, 2026
4 checks passed
@bdbarnett
bdbarnett deleted the fix/struct-dir branch October 27, 2026 16:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

CPython: dir() on a struct instance is empty

1 participant