-
Notifications
You must be signed in to change notification settings - Fork 28
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
return type for lxml.etree.fromstring
should be Optional (?)
#63
Comments
The |
`parser` seems like it can be an `HTMLParser. There's code in the wild that does this without obviously being wrong. As far as I can tell from the lxml Cython source, `parser` is a `_Baseparser`. I wasn't sure if the `iterparse` class ought to be allowed here too. I erred on the side of caution. It's possible for this function to return None, if the parser has `recover=True`. I couldn't find a good way to express this without affecting every other call to `fromstring`. (Perhaps one could make the Parser generic over some kind of `Recoverable` indicator type... but that seemed like overkill) Resolves lxml#63.
I've had a go at this in #64---though I'm not massively familiar with lxml's machinery. Hopefully it's a useful drive-by though.
For completeness: I interpreted this as a test in the stubs repository here, rather than a test in lxml proper. |
At least with lxml 4.8.0, I see the following:
But the type annotation says we'll always return an
_Element
:lxml-stubs/lxml-stubs/etree.pyi
Lines 498 to 500 in 5eaa0f8
Aside: I'm passing an HTMLParser here instead of an XMLParser. The type stub for
fromstring
doesn't allow that either, but again I'm not sure if that's a mistake in the annotations.For context, I came across this whilst trying to use the stubs to typecheck this snippet of Synapse.
The text was updated successfully, but these errors were encountered: