-
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
Improve annotation of fromstring
#64
Changes from 1 commit
8970d00
fa254a2
07ab5e7
547e325
3aca181
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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]" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since Admittedly, you can also change the default parser globally, which then allows a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In typeshed we generally avoid Unions in return types, for reasons discussed in python/mypy#1693. In this case, it sounds like returning None is a very rare edge case, but if we return There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Many thanks both! On returning Union types: I'd read some of the discussion in python/typing#566 which sounded promising at first, but as Jukka writes here:
I'm about to push three commits which:
To be explicit: I don't have strong opinions about this PR. I like the idea of annotations that describe everything that a function can possibly do; but there's a balance to between that and pragmatism, and I'm happy to drop the |
||
|
||
- 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 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
HTMLParser
is also allowed here.