You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on May 13, 2026. It is now read-only.
Repository navigation
This repository was archived by the owner on May 13, 2026. It is now read-only.
return type for lxml.etree.fromstring should be Optional (?) #63
$ python
Python 3.10.4 (main, Mar 25 2022, 00:00:00) [GCC 11.2.1 20220127 (Red Hat 11.2.1-9)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import lxml.etree as etree
>>> parser = etree.HTMLParser(recover=True, encoding="utf-8")
>>> tree = etree.fromstring(b"", parser)
>>> tree
>>> tree is None
True
But the type annotation says we'll always return an _Element:
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 None return value is a rare artefact of using recover=True with no data, but given that it exists, I agreed on both points, thanks for bringing this up. Maybe you can send a PR for these changes? We might actually be lacking a test for something that passes a parser at all. seems worth adding.
Activity
scoder commentedon May 3, 2022
The
Nonereturn value is a rare artefact of usingrecover=Truewith no data, but given that it exists, I agreed on both points, thanks for bringing this up. Maybe you can send a PR for these changes? We might actually be lacking a test for something that passes a parser at all. seems worth adding.Improve annotation of `fromstring`
DMRobertson commentedon May 3, 2022
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.
Improve "parser=" and return annotations of fromstring() and parse() (G…