`ET.Element.tag` does not allow comparision with `ET.Comment`
Personne n'a encore pris cette issue.
- Langage dominant
- Python
- Étoiles
- 5.1k
- Forks
- 2.1k
- Merge moyen
- 1 j 19 h
- PR mergées (30 j)
- 82
Description
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
Guide de contribution
Ouvrir le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Piste de recherche
Commencez par localiser les définitions de types de xml.etree.ElementTree et reproduisez le problème avec l’extrait test.py fourni sous Python 3.13 et mypy 1.17 en utilisant --strict. Le travail est terminé lorsque la comparaison d’identité entre el.tag et ET.Comment ne produit plus comparison-overlap, tout en conservant une représentation correcte de l’analyse des commentaires.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- python
- Domaine
- tooling
- Type d'issue
- Bug
- Difficulté
- 3/5
- Temps estimé
- 1-2 jours
- Activité
- À l'abandon
- Clarté
- Plutôt claire
- Accessibilité débutants
- 45/100