Skip to content

xml.etree.ElementTree: fix missing None in get(), findtext() return type - #3093

Merged
srittau merged 1 commit into
python:masterfrom
bluetech:xml-findtext-none
Jul 1, 2019
Merged

srittau merged 1 commit into
python:masterfrom
bluetech:xml-findtext-none

Conversation

@bluetech

Copy link
Copy Markdown
Contributor

Before, this code

from xml.etree.ElementTree import Element
reveal_type(Element('foo').findtext('bar'))

would reveal builtins.str. This is incorrect because if the path is
not found, a default None is returned.

After, it reveals Union[builtins.str, None].

The solution with overloads is the same as used by Mapping.get in
stdlib/3/typing.pyi.

Comment thread stdlib/2and3/xml/etree/ElementTree.pyi Outdated
def findtext(self, path: _str_argument_type, default: Optional[_T] = ..., namespaces: Optional[Dict[_str_argument_type, _str_argument_type]] = ...) -> Union[_T, _str_result_type]: ...
def get(self, key: _str_argument_type, default: Optional[_T] = ...) -> Union[_str_result_type, _T]: ...
@overload
def findtext(self, path: _str_argument_type, namespaces: Optional[Dict[_str_argument_type, _str_argument_type]] = ...) -> Optional[_str_result_type]: ...

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't namespaces be keyword-only in this overload?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's a good idea. Changed.

Before, this code

    from xml.etree.ElementTree import Element
    reveal_type(Element('foo').findtext('bar'))

would reveal `builtins.str`. This is incorrect because if the path is
not found, a default None is returned.

After, it reveals `Union[builtins.str, None]`.

The solution with `overload`s is the same as used by `Mapping.get` in
`stdlib/3/typing.pyi`.
@bluetech
bluetech force-pushed the xml-findtext-none branch from 4599aad to 52fa0fb Compare June 28, 2019 14:36

@srittau srittau left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@srittau
srittau merged commit c666998 into python:master Jul 1, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants