typeshed icon indicating copy to clipboard operation
typeshed copied to clipboard

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

Open hterik opened this issue 3 months ago • 2 comments

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

hterik avatar Sep 03 '25 09:09 hterik