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:
dylan-robins commentedJan 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
nodeValuethat "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.
The example above prints T and N as expected, but Pylance reports the following issue:
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
Nonein the Node class (as is done for other similar attributes), this problem could be fully resolved:cpython/Lib/xml/dom/minidom.py
Lines 34 to 40 in b99ac1d
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
The text was updated successfully, but these errors were encountered: