python / python/typeshed

`ET.Element.tag` does not allow comparision with `ET.Comment`

Aperta
#14,671 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

stubs: false positive
Lingua principale
Python
Stelle
5.1k
Fork
2.1k
Merge medio
1g 19h
PR unite (30g)
82

Descrizione

To Reproduce

import xml.etree.ElementTree as ET

parser = ET.XMLParser(target=ET.TreeBuilder(insert_comments=True))
doc = ET.ElementTree().parse("foo.xml", parser)
for el in doc.findall("*"):
    if el.tag is ET.Comment:     #  error: Non-overlapping identity check (left operand type: "str", right operand type: "Callable[[str | None], Element[Callable[..., Element[Any]]]]")  [comparison-overlap]
        print("Comment:", x.text)

Expected Behavior

el.tag should be allowed to be compared to ET.Comment

Actual Behavior

el.tag is constrained to str only and raises following comparision-overlap error when compared with ET.Comment:
# error: Non-overlapping identity check (left operand type: "str", right operand type: "Callable[[str | None], Element[Callable[..., Element[Any]]]]") [comparison-overlap]

This error started after upgrading from mypy 1.14 to 1.15, most likely caused by https://github.com/python/typeshed/pull/13349

Tbh this isn't exactly documented behaviour and parsing comments programmatically isn't exactly best practice either, but some times you are not in control of the xmls received and reading the tag is the the only way to identify the comments that i know.

Your Environment

  • Mypy version used: 1.17
  • Mypy command-line flags: mypy --strict test.py
  • Python version used: 3.13

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.

Direzione di ricerca

Inizia individuando le definizioni dei tipi di xml.etree.ElementTree e riproduci il problema con lo snippet test.py fornito usando Python 3.13 e mypy 1.17 con --strict. Il lavoro è completato quando il confronto di identità tra el.tag e ET.Comment non produce più comparison-overlap, mentre il parsing dei commenti continua a essere rappresentato correttamente.

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

Valutazione

Stack tecnologico
python
Ambito
tooling
Tipo di issue
Bug
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
45/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.