-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments are not accounted for #46
Comments
So I got it all wrong.
from lxml import etree
tree = etree.parse(somefile)
result = tree.iter(etree.Comment) |
The code that I encountered these issues with looks roughly like this: from lxml import etree
root = etree.fromstring('<foo><!-- comment --></foo>')
comment: list[str | None] = [] # TODO: should be list[str]
for el in root:
if el.tag == etree.Comment: # type: ignore[comparison-overlap]
comment.append(el.text) To satisfy mypy, I need the Also, even if I narrow the typing of |
For the first problem, I don't have any good solution so far. This is the price needed to pay when I decided to follow lxml on how Right now you may want to try narrowing down the type of for el in root:
if isinstance(el, etree._Comment):
comment.append(el.text) On second problem, you're right. It is possible to use |
It is empty string even when there is no text content for __ContentOnlyElement. Partially addresses #46.
The |
The fixable part of this issue is addressed, so I'm closing. Please feel free to reopen if something related to this issue still doesn't work. |
At least the following make using the types somewhat challenging:
tag
of an element can beetree.Comment
, a function.text
of a comment is always astr
.Given these, I wouldn't be surprised to find other places where the types are misaligned with respect to comments.
The text was updated successfully, but these errors were encountered: