Replace factory functions with classes. Second try.#94
Merged
Conversation
Element() 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 ```
abelcheung
requested changes
Aug 12, 2025
Owner
abelcheung
left a comment
There was a problem hiding this comment.
Can you re-add the part of custom target parser removed in ElementTree overloads?
339ea08 to
2a4ea02
Compare
udifuchs
commented
Aug 13, 2025
abelcheung
approved these changes
Aug 19, 2025
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.
This PR replaces PR #81 as requested.
Element() 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:
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: