Skip to content

Commit

Permalink
Improve annotation of fromstring
Browse files Browse the repository at this point in the history
`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.
  • Loading branch information
DMRobertson committed May 3, 2022
1 parent 5eaa0f8 commit 8970d00
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
7 changes: 5 additions & 2 deletions lxml-stubs/etree.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 9 additions & 1 deletion test-data/test-etree.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,15 @@
main: |
from lxml import etree
document = etree.fromstring("<doc></doc>")
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
Expand Down

0 comments on commit 8970d00

Please sign in to comment.