Skip to content
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

Improve annotation of fromstring #64

Merged
merged 5 commits into from
Jul 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions lxml-stubs/etree.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -493,12 +493,25 @@ def cleanup_namespaces(
keep_ns_prefixes: Optional[Iterable[_AnyStr]] = ...,
) -> None: ...
def parse(
source: _FileSource, parser: XMLParser = ..., base_url: _AnyStr = ...
) -> _ElementTree: ...
source: _FileSource,
parser: Union[XMLParser, HTMLParser] = ...,
base_url: _AnyStr = ...,
) -> Union[_ElementTree, Any]: ...
@overload
def fromstring(
text: _AnyStr, parser: XMLParser = ..., *, base_url: _AnyStr = ...
text: _AnyStr,
parser: None = ...,
*,
base_url: _AnyStr = ...,
) -> _Element: ...
@overload
def fromstring(
text: _AnyStr,
parser: Union[XMLParser, HTMLParser] = ...,
*,
base_url: _AnyStr = ...,
) -> Union[_Element, Any]: ...
@overload
def tostring(
element_or_tree: _ElementOrTree,
encoding: Union[Type[str], Literal["unicode"]],
Expand Down
8 changes: 8 additions & 0 deletions test-data/test-etree.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@
document = etree.fromstring("<doc></doc>")
reveal_type(document) # N: Revealed type is "lxml.etree._Element"

- 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, Any]"

- case: etree_element_find
disable_cache: true
main: |
Expand Down