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

[xml.dom.minidom] nodeValue attr is not defined on Node class but directly on all derived classes #100710

Open
dylan-robins opened this issue Jan 3, 2023 · 0 comments
Labels
expert-XML type-bug An unexpected behavior, bug, or error

Comments

@dylan-robins
Copy link

dylan-robins commented Jan 3, 2023

[xml.dom.minidom] nodeValue attr is not defined on Node class but directly on all derived classes

According to the documentation, the Node class is supposed to have an attribute nodeValue that "has a different meaning for each node type; see the DOM specification for details.. [...] The value is a string or None."". However, this attribute is undefined on the Node class, but is then implemented on all derived classes (DocumentFragment, Attr, Element, ProcessingInstruction, CharacterData, DocumentType, Entity, Notation, Document).

As a result, IDEs and static analysers raise false positives when analysing "correct" code.

from xml.dom import minidom

dummy_xml: str = """<?xml version="1.0"?>
<Envelope>
  <Body>
    <StockName>T</StockName>
    <StockName>N</StockName>
  </Body>
</Envelope>
"""

dom: minidom.Document = minidom.parseString(dummy_xml)
body: minidom.Element = dom.getElementsByTagName("Body")[0]

child_node: minidom.Node
for child_node in body.childNodes:
  inner_node: minidom.Node | None = child_node.firstChild
  if inner_node is not None:
    	print(inner_node.nodeValue)

The example above prints T and N as expected, but Pylance reports the following issue:

Cannot access member "nodeValue" for type "Node"
Member "nodeValue" is unknown - Pylance (reportGeneralTypeIssues)

Indeed the nodes we process happen to be Elements - therefore the attribute is defined at runtime. However during static analysis this is unknown despite this being legal according to the documentation.

Proposed solution

As far as i can tell this would be very easy to fix: just by initializing this attribute to None in the Node class (as is done for other similar attributes), this problem could be fully resolved:

class Node(xml.dom.Node):
namespaceURI = None # this is non-null only for elements and attributes
parentNode = None
ownerDocument = None
nextSibling = None
previousSibling = None

While we're at it it'd be worth double checking if other attributes are affected by the same issue. I can submit a PR in a few hours if you'd like.

My environment

  • CPython versions tested on: 3.8.2, 3.10.8, 3.11.0
  • Operating system and architecture: RHEL7.9 x86_64, Windows 10 x86_64
@dylan-robins dylan-robins added the type-bug An unexpected behavior, bug, or error label Jan 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
expert-XML type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants