Until now, Element and ElementTree were factory functions.
Element() returned an _Element class and
ElementTree() returned an _ElementTree class.
This PR turns them into classes. These classes should behave exactly the same
as the factory function. Specifically, in both cases:
>>> element = Element("test")
>>> type(element)
<class 'lxml.etree._Element'>
>>> callable(Element)
True
The difference is that before this change we could not use isinstance:
>>> isinstance(element, etree.Element)
TypeError: isinstance() arg 2 must be a type, a tuple of types, or a union
While now:
>>> isinstance(element, etree.Element)
True
>>> issubclass(_Element, Element)
True
These checks work because _Element is registered as a virtual subclass of Element.
The motivation for this PR is to support type annotations.
Currently, type stubs for lxml need to annotate the factory functions like this:
def Element(...) -> _Element: ...
Furthermore, the _Element class members are all annotated.
But _Element was supposed to be kept as an internal implementation.
With this PR, the Element class can be used as an interface class.
This will allow us to create modified stubs for lxml where the _Element class
is not mentioned at all.
added a commit to udifuchs/types-lxml that referenced this pull request
Feb 18, 2024Element() and ElementTree() were factory functions until now. These are now defined as classes. Stop exposing _Element and _ElementTree, which are internal implementations. Use Element and ElementTree instead. This change is not backward compatible. Any user of types-lxml that used _Element in their type annotations would get an error. This change only makes sense if lxml merges lxml/lxml#405
udifuchs added a commit to udifuchs/types-lxml that referenced this pull request
Feb 13, 2025Element() and ElementTree() were factory functions until now. They are now defined as classes. With this change, we can annotate code with the public Element class, instead of using the private _Element class. For example: ``` def print_tree(tree: e.ElementTree[e.Element]) -> None: ... ``` There is a related PR for lxml: lxml/lxml#405 In the lxml PR, _Element is a virtual subclass of Element. This means that when we create an Element it actually creates an _Element class, but the created object is also an instance of Element: ``` el = Element() reveal_type(el) # _Element isinstance(element, Element) # True isinstance(element, _Element) # True ``` I don't know if it is possible to have the equivalent of a virtual subclass in a type stub. Therefore, I just made Element a type alias of _Element. The lxms stubs should work with all existing versions of lxml. But I am not sure it make sense to merge this PR until the related PR for lxml is merged.
Closed
Merged
3 tasks
udifuchs added a commit to udifuchs/types-lxml that referenced this pull request
Aug 11, 2025Element() and ElementTree() were factory functions until now. They are now defined as classes. With this change, we can annotate code with the public Element class, instead of using the private _Element class. For example: ``` def print_tree(tree: e.ElementTree[e.Element]) -> None: ... ``` There is a related PR that was merged into lxml: lxml/lxml#405 In the lxml PR, _Element is a virtual subclass of Element. This means that when we create an Element it actually creates an _Element class, but the created object is also an instance of Element: ``` el = Element() reveal_type(el) # _Element isinstance(element, Element) # True isinstance(element, _Element) # True ```
Merged