Skip to content

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

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

Merged
merged 1 commit into from
Jul 1, 2019

Conversation

bluetech
Copy link
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.

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
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
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
Copy link
Collaborator

@srittau srittau left a comment

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