Repository navigation
Stubs: flatten all service field sections, annotate multiple selectors as lists - #878
Conversation
craigbarratt
left a comment
There was a problem hiding this comment.
Thanks for this! Flattening sections and handling multiple selectors are both good improvements. I've left a few inline comments. The main one is the duplicate-parameter case, which can make the generated stub file fail to parse. The list[...] annotation is also stricter than what Home Assistant accepts, which leads to type errors on valid calls.
Also, #867 has since been merged and changes the same section-flattening code in _build_services (advanced_fields → additional_fields), so this will need a rebase onto current master.
f8db2c4 to
153b8fb
Compare
|
Thanks for feedback, rebased and addressed |
|
Thanks for the review. I've rebased onto current |
craigbarratt
left a comment
There was a problem hiding this comment.
Thanks for the quick turnaround! I've checked all the fixes against the code and they look good, and tests/test_stubs.py passes locally. Two small things remain, both easy to fix. Once they're in, I'm happy to merge. I'll handle a few other minor type-annotation edge cases in a follow-up commit after merging.
…ultiple selectors - treat any field with a dict of field dicts under `fields` and no `selector` as a UI section and flatten it, whatever its name - skip duplicate field names across top level and sections, so the generated stub module always parses - annotate `multiple: true` selectors as `T | list[T]`, matching cv.ensure_list, using the `multiple` flag of the selector entry that set the base type - check default/annotation compatibility on AST names, not a substring of the unparsed annotation - clean up generated stubs in a finally block in the new test Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
153b8fb to
860a26b
Compare
|
Thanks for the PR! |
Follow-up to #878: - parse SELECTOR_SIMPLE_TYPES entries so compound types like tuple[int, int, int] have real Name nodes for the default check - don't widen an annotation that already accepts the default (Any, or a Literal that includes it) - treat any field whose fields is a dict of dicts as a section, as HA's _SERVICE_SCHEMA does, even if it also has a selector - check the generated signature via ast instead of substrings Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
I encountered this when trying to make sure Supernotify worked well with Pyscript.
It has its own
notifyaction now, which is much richer than the plain vanillanotify.send_messagestyle to be easier to use from the actions UI. So a more complicated services.yaml, though other integrations might similarly be pushing what the configflow UI can do.Problem
pyscript.generate_stubsgets the signature wrong for services whoseservices.yamluses sections ormultipleselectors.additional_fieldsandadvanced_fields, but a section can have any name. Any other section becomes a bogus untyped parameter, and the fields inside it are dropped.multiple: true. Atext,selectorentityselector withmultiple: truetakes a list, but is annotated as a single value, so passing a list is reported as a type error.For example, a service with a
multimediasection and a multiple text field currently generates:Change
fieldsand noselector, whatever its name.multiple: trueis annotated aslist[...]of its usual type.The same service now generates:
Testing
Added
test_stubs_flatten_named_sections_and_multiple, which fails on master and passes with this change. The existing stub tests still pass.