-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
getroot()
method of xml.etree.ElementTree
return type should be nullable
#7770
Comments
The problem with adding import xml.etree.ElementTree as ET
tree = ET.parse()
root = tree.getroot() # "root" is never None Our usual solution would be to return Obligatory comment that python/typing#566 would help with that as well. |
if "return Element | Any" were used, it's kind of overlapped since "Any" includes "Element". |
Indeed. You would expect For example, consider this: (mostly copy/paste from xml module docs) import xml.etree.ElementTree as ET
tree = ET.parse('country_data.xml')
root = tree.getroot()
print(root.tagg) # this line is a typo
In typeshed we unofficially call this "the Any trick". We tend to use it whenever something can be Another example of the Any trick is |
Hello,
I'm one of the maintainers of Sonar Python analyzer and we use typeshed for our type inference engine. (btw thanks a lot for maintaining typeshed!)
While investigating a false positive reported by a user, I noticed that that
getroot()
method ofElementTree
class has return type annotation-> Element
(see here).I think
getroot()
return type annotation should beElementTree | None
instead, because it can returns None when root element is not provided:In fact, e.g. 3.10 implementation of ElementTree is defined as follows:
I'm happy to open a PR if you think the reported issue is valid.
Thanks,
Andrea
The text was updated successfully, but these errors were encountered: