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

Commits on Feb 17, 2024

  1. Replace factory functions with classes.

    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 committed Feb 17, 2024
    Configuration menu
    Copy the full SHA
    2712c8e View commit details
    Browse the repository at this point in the history