imgurbot12 / pyxml

Pure python3 alternative to stdlib xml.etree with HTML support
MIT License
1 stars 1 forks source link

Avoiding circular importing #4

Closed elisbyberi closed 1 year ago

elisbyberi commented 1 year ago

Which modules are susceptible to circular importing and require special handling to avoid errors?

imgurbot12 commented 1 year ago

The xpath library makes use of the Element object type in various places when processing the instructions, though the type doesn't have to be explicitly defined within the functions perhaps? The Element object then uses the xpath library in its find, findall, finditer, and gettext methods. example: https://github.com/imgurbot12/codon-xml/blob/master/xml/xpath/functions.py#L23 and https://github.com/imgurbot12/codon-xml/blob/master/xml/etree.py#L181

Right now serialize_xml references the "Special" element types which are define below to determine how to write the data and then called in ElementTree though everything can be restructured to theoretically avoid this issue. We can place the element definitions in their own element.codon file and then leave ElementTree with serialize_xml in it's own etree.codon file: https://github.com/imgurbot12/codon-xml/blob/master/xml/etree.py#L67 https://github.com/imgurbot12/codon-xml/blob/master/xml/etree.py#L192 https://github.com/imgurbot12/codon-xml/blob/master/xml/etree.py#L247

imgurbot12 commented 1 year ago

Similar importing to the serialize_xml instance also exist with the Parser and TreeBuilder types which get referenced with the ElementTree type. All of these can theoretically be fixed by moving the Element object types to their own file so I guess I'll do that now. The xpath thing seems to be the only major issue that I know of at the moment.

imgurbot12 commented 1 year ago

did my best to resolve the importing issues according to what you said in the codon issue. https://github.com/imgurbot12/codon-xml/commit/2f1dda0f3ae7a19821d41cfb9dd104d362adc7cb

elisbyberi commented 1 year ago

I am currently testing a scenario that involves demo classes, which is similar to the issue at hand.

elisbyberi commented 1 year ago

I have successfully tested this MRE in Codon. Please let me know if I have demonstrated the problematic logic correctly. Note that from . import xpath doesn't work.

# xpath/__init__.py
from ..element import Element

def find(elem: Element, xpath) -> Optional[Element]:  # Self == Element
    """
    find first matching element associated w/ xpath
    :param elem:  root element to search xpath from
    :param xpath: raw xpath expression
    :return:      first element found matching criteria
    """
    print(xpath, elem)
# element.py
class Element:
    def find(self, path) -> Optional[Element]:  # Self == Element
        from xpath import find    # `from . import xpath` doesn't work
        return find(self, path)

    def __repr__(self):
        return 'This is class Element.'

e = Element()
e.find('Find!')
imgurbot12 commented 1 year ago

That looks like it will work. yup! Thanks.

elisbyberi commented 1 year ago

It needs just a little work, but excessive caution to solve compatibility issues. Then, we can test the library in both Python and Codon at the same time.

imgurbot12 commented 1 year ago

Closing due to inactivity. Feel free to reopen if you have any questions or issues!