Skip to content

ET.Element.tag does not allow comparision with ET.Comment #14671

@hterik

Description

@hterik

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 #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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions