Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace factory functions with classes. #405

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

udifuchs
Copy link

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.

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.

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.
udifuchs added a commit to udifuchs/types-lxml that referenced this pull request Feb 18, 2024
Element() 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant