Tweaks in type stubs generation - #863
Open
contagon wants to merge 1 commit into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Ever since stub generation was built into pyscript, I've been leaning heavily into python typing (using pyrefly as my typechecker currently, previously basedpyright) as I find this has been catching A LOT of the bugs I had in my automations.
There was always a couple of typing oddities the type checkers didn't like in the pyscript stubs. I've been fixing them by hand for a while, but finally took the time to fix them in the generation.
DISCLOSURE: A lot of the code in the PR was LLM generated, but I reviewed it to the best of my ability. Also the PR is all hand-written:)
Missing entities
I had a number of missing entities in the stubs, namely
zone.homeand a number of template entities were the ones I noticed. This PR now uses the state machine (sorted to get more deterministic output) instead of the entity registry to get ALL entities. From what I can tell, this should include everything the entity registry also did.Poor defaults
There was a number of defaults in the stubs that didn't type check correctly. Examples include
delay_secs: int = 0.4,activity: int = None, andpipeline : str = 1. These are now transformed intodelay_secs: float = 0.4,arg: int | None = None, andpipeline: str | int = 1.Tests & Type Checkers
Tests have been added for each of these! I'd also be happy to add a section to the docs about setting up a type checker with pyscript. Between stubs giving autocompletion and the type checker making sure I call services correctly (bad types when calling -> type errors) and am only using entities that still exist (deleted entities -> generate new stubs -> type errors anywhere they are used), writing pyscript now feels just like native python code!
I'd recommend giving this a shot before merging, just to make sure it all works! Also happy to tweak whatever