diff --git a/lxml-stubs/etree.pyi b/lxml-stubs/etree.pyi index 78535e4..4cf8622 100644 --- a/lxml-stubs/etree.pyi +++ b/lxml-stubs/etree.pyi @@ -496,8 +496,11 @@ def parse( source: _FileSource, parser: XMLParser = ..., base_url: _AnyStr = ... ) -> _ElementTree: ... def fromstring( - text: _AnyStr, parser: XMLParser = ..., *, base_url: _AnyStr = ... -) -> _Element: ... + text: _AnyStr, + parser: Union[XMLParser, HTMLParser] = ..., + *, + base_url: _AnyStr = ..., +) -> Optional[_Element]: ... @overload def tostring( element_or_tree: _ElementOrTree, diff --git a/test-data/test-etree.yml b/test-data/test-etree.yml index f0e68ac..4959252 100644 --- a/test-data/test-etree.yml +++ b/test-data/test-etree.yml @@ -3,7 +3,15 @@ main: | from lxml import etree document = etree.fromstring("") - reveal_type(document) # N: Revealed type is "lxml.etree._Element" + reveal_type(document) # N: Revealed type is "Union[lxml.etree._Element, None]" + +- case: etree_from_empty_string_with_parser_recovery_returns_none + disable_cache: true + main: | + from lxml import etree + parser = etree.HTMLParser(recover=True) + document = etree.fromstring("", parser) + reveal_type(document) # N: Revealed type is "Union[lxml.etree._Element, None]" - case: etree_element_find disable_cache: true