Skip to content

Conversation

@gazpachoking
Copy link
Contributor

@gazpachoking gazpachoking commented Jun 2, 2025

This is a very messy, and unfinished, implementation for #122 to allow some initial discussion

Goals:

  • Be able to do typing for specific html elements
  • IDE autocompletion for attribute names

I did a couple parts here:

  • Wrote a utility script that scrapes all the html elements and their attributes from MDN and dumps it to a json file.
  • Wrote a source generator that takes this json and generates the classes for each html element

Things are very messy at the moment, just wanted to put initial work up for discussion.

TODO:

  • figure where files should live, and what they should be named
  • either remove deprecated elements from the list, or mark them as deprecated somehow
  • Generate the instances of these classes for use
  • Pycharm is complaining that the signature for the __call__ method of subclasses of Element don't match the base class. Is this a real problem? What to do?

@gazpachoking
Copy link
Contributor Author

@pelme Haven't had a chance to work on this further, but does this seem like a reasonable approach? We end up with a pretty gigantic file, but it seems like that's what's necessary for best class autocomplete/static analysis.

@gazpachoking
Copy link
Contributor Author

Still plan to look at this some more when I get the time. Seems like maybe https://html.spec.whatwg.org/ is a better source to scrape elements/attributes from. MDN doesn't seem to be as up to date.

@pelme
Copy link
Owner

pelme commented Aug 5, 2025

If we go this route, maybe we should use pyi files for typing and get rid of typing from the .py files altogether. Making a bunch of subclasses/overloading call may cause performance problems.

In the beginning of htpy I had separate .pyi and .py files but it was easier to just keep them the same: c3afb62

Then we would be certain that these huge files would not affect import times or runtime performance.

Btw, the "typing council" recommends to accept PEP 728: python/typing-council#22 (comment) so hopefully there will be some movement there. But that will earliest land in Python 3.15 so there will be years before everyone can use it anyways.

@gazpachoking
Copy link
Contributor Author

I was starting to wonder if this could be better handled with ide plugins that could delegate to the HTML completer. I'm doing some experimentation, but that is further out of my wheelhouse so we'll see how it goes.

@pelme
Copy link
Owner

pelme commented Aug 5, 2025

it would be nice to solve it at the python level, maintaining plugins for all sorts of IDEs seems very annoying at best 🙂 but maybe that could provide richer HTML completion than type hints can provide

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