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

Abierto
#100,710 0 comentarios 1 reacción 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
1/5
Tiempo estimado
Menos de una hora
Aptitud para principiantes
25/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Estancado
Stack tecnológico
python
Área
backend

Línea de trabajo

Empieza en Lib/xml/dom/minidom.py, en la definición de Node enlazada en la issue, y compara sus atributos con los definidos en las clases de nodos derivadas. El cambio está terminado cuando Node expone nodeValue de forma coherente con la documentación y los analizadores estáticos pueden acceder a él a través de una referencia a Node; comprueba si otros atributos documentados de Node presentan la misma omisión.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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
Lenguaje dominante
Python
Estrellas
77.2k
Forks
36k
Merge medio
1 d 9 h
PR fusionados (30 d)
558

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de python/cpython

Todos los issues de python/cpython

Issues similares

Más issues de Python

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.