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

Aperta
#100,710 0 commenti 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
1/5
Tempo stimato
Meno di un'ora
Idoneità per principianti
25/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Ferma
Stack tecnologico
python
Ambito
backend

Direzione di ricerca

Inizia in Lib/xml/dom/minidom.py, alla definizione di Node collegata nell’issue, e confronta i suoi attributi con quelli definiti nelle classi di nodi derivate. La modifica è completa quando Node espone nodeValue in modo coerente con la documentazione e gli analizzatori statici possono accedervi tramite un riferimento a Node; verifica se altri attributi documentati di Node presentano la stessa omissione.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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
Lingua principale
Python
Stelle
77.2k
Fork
36k
Merge medio
1g 9h
PR unite (30g)
558

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di python/cpython

Tutte le issue di python/cpython

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.