Replace factory functions with classes. #405
Open
+98
−5
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.
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:
The difference is that before this change we could not use isinstance:
While now:
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:
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.
As far as I can tell, this change should not break backward compatibility. Someone would have to go out of their way to test that Element is a function and not a class. As mentioned above, both are
callable
.Still, I must admit that I am not convinced that this is the correct approach. Specifically, it seems a bit of a hack to have an Element class that ends up creating an _Element class.