[xml.dom.minidom] nodeValue attr is not defined on Node class but directly on all derived classes
还没有人认领这个 Issue。
评估
调研方向
从 Lib/xml/dom/minidom.py 中 issue 链接的 Node 定义开始,将其属性与派生节点类中定义的属性进行比较。当 Node 按照文档一致地公开 nodeValue,并且静态分析器可以通过 Node 引用访问它时,修改即完成;检查其他有文档说明的 Node 属性是否也存在同样的遗漏。
由索引模型根据 Issue 内容生成。
描述
[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:
https://github.com/python/cpython/blob/b99ac1dbc081e4f2d2e68906e9c7c535e628611a/Lib/xml/dom/minidom.py#L34-L40
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
Linked PRs
- gh-155586
- 主要语言
- Python
- 星标
- 77.2k
- 派生
- 36k
- 平均合并
- 1 天 9 小时
- 30 天内合并 PR
- 558
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
python/cpython 的其他 Issue
-
docs pending
难度 2/5 1-3 小时 新手友好度 78/100
-
stdlib type-feature
难度 2/5 1-3 小时 新手友好度 78/100
-
stdlib type-feature
难度 2/5 1-3 小时 新手友好度 72/100
-
build type-bug
难度 2/5 1-3 小时 新手友好度 76/100
-
stdlib topic-email type-feature
难度 2/5 1-3 小时 新手友好度 70/100
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 82/100
-
难度 2/5 1-3 小时 新手友好度 84/100
-
难度 2/5 1-3 小时 新手友好度 68/100
-
难度 2/5 1-3 小时 新手友好度 86/100
-
🐛 Bug 🔔 Pending processing
难度 2/5 1-3 小时 新手友好度 84/100
jumpserver/jumpserver#17584 ·