Skip to content

Replace factory functions with classes. Second try.#94

Merged
abelcheung merged 5 commits into
abelcheung:mainfrom
udifuchs:factory-to-classes-2025
Aug 19, 2025
Merged

Replace factory functions with classes. Second try.#94
abelcheung merged 5 commits into
abelcheung:mainfrom
udifuchs:factory-to-classes-2025

Conversation

@udifuchs
Copy link
Copy Markdown
Contributor

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:

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

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
```
Copy link
Copy Markdown
Owner

@abelcheung abelcheung left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you re-add the part of custom target parser removed in ElementTree overloads?

@abelcheung abelcheung added the enhancement New feature or request label Aug 12, 2025
@abelcheung abelcheung moved this to In Progress in Types-lxml progress Aug 12, 2025
@abelcheung abelcheung moved this from In Progress to In Review in Types-lxml progress Aug 12, 2025
@udifuchs udifuchs requested a review from abelcheung August 13, 2025 02:49
@abelcheung abelcheung force-pushed the factory-to-classes-2025 branch from 339ea08 to 2a4ea02 Compare August 13, 2025 06:50
Comment thread src/lxml-stubs/etree/_element.pyi
@abelcheung abelcheung merged commit a1315d5 into abelcheung:main Aug 19, 2025
1 check passed
@github-project-automation github-project-automation Bot moved this from In Review to Done in Types-lxml progress Aug 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

2 participants