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

オープン
#100,710 コメント 0 件 リアクション 1 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
1/5
見積もり時間
1時間未満
初心者へのやさしさ
25/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
停滞
技術スタック
python
領域
backend

調査の方向性

Lib/xml/dom/minidom.py の、issue でリンクされている Node の定義から始め、その属性を派生ノードクラスで定義されている属性と比較します。Node がドキュメントに沿って一貫して nodeValue を公開し、静的解析ツールが Node 参照を通じてそれにアクセスできるようになれば変更は完了です。ほかのドキュメント化された Node 属性にも同じ欠落がないか確認してください。

索引モデルが issue の本文から書いたものです。

説明

topic-typing topic-XML type-bug

[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時間
マージ済み PR(30日)
558

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

python/cpython のほかの issue

python/cpython の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。