Skip to content

Add IDE autocomplete for known html attributes#111

Draft
gazpachoking wants to merge 9 commits intopelme:mainfrom
gazpachoking:autocomplete_attrs
Draft

Add IDE autocomplete for known html attributes#111
gazpachoking wants to merge 9 commits intopelme:mainfrom
gazpachoking:autocomplete_attrs

Conversation

@gazpachoking
Copy link
Copy Markdown
Contributor

Not sure if this is the best way to do this, but it's the easiest way I found in my testing. Would be sorta nice to have it only autocomplete the valid attributes for a given tag, but I haven't found a clean way to accomplish that. Cursory tests seem to indicate pyright and mypy are both fine with unknown attributes being passed in still. Autocomplete was tested in PyCharm, haven't tested vscode.

@gazpachoking
Copy link
Copy Markdown
Contributor Author

gazpachoking commented Apr 18, 2025

Looks like my type checking tests must have been too loose. I'll investigate further.

EDIT: Crap. My tests were totally flawed. I was testing a script using htpy, not the codebase itself. This error makes sense to me, and I was sorta wondering why it was fine. Not sure I can come up with a good solution though.

@gazpachoking gazpachoking marked this pull request as draft April 18, 2025 23:26
@gazpachoking
Copy link
Copy Markdown
Contributor Author

Well, closer maybe. PEP 728 does what we want for this, which allows us to type the attr dict as well as the kwargs. It also allows tightening the typing on class_ vs other attributes. typing_extensions has provisional support for this, which I switched to here, as does pyright. pyright now passes on the main codebase, but for some reason doesn't like it when passing extra arguments in, still not sure what's up with that, whether it's just incomplete pep 728 support or something else.

Unless there's some other way to get this to work I think we have to wait for more support of PEP 728.

@pelme
Copy link
Copy Markdown
Owner

pelme commented Apr 20, 2025

Nice, I think this is the way to go. We just need to wait for PEP 728 to land and for mypy/pyright to add support for it.

@gazpachoking
Copy link
Copy Markdown
Contributor Author

Pyright started supporting PEP 728 for this purpose after I put in this ticket, so that bit seems to work. microsoft/pyright#10352
It however didn't like the TypedDict to type the attrs dictionary argument, not fully sure why that didn't work, so I ditched that part for now.

This looks like the issue to watch for mypy support: python/mypy#18176

It would be super cool if this feature got added to allow us to make Components generic based on a TypedDict. Then each tag could declare which attributes were valid for that particular tag. python/typing#1399

@gazpachoking
Copy link
Copy Markdown
Contributor Author

Now I'm wondering if this would be better as an IDE extension. Ideally it would recognize the context of being in an htpy element, and provide the auto completions from the native HTML language completer. (With a couple rewrites like class_)

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.

2 participants