Skip to content

Commit 4599aad

Browse files
committed
xml.etree.ElementTree: fix missing None in get(), findtext() return type
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`.
1 parent 5dc89fe commit 4599aad

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

stdlib/2and3/xml/etree/ElementTree.pyi

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Stubs for xml.etree.ElementTree
22

3-
from typing import Any, Callable, Dict, Generator, IO, ItemsView, Iterable, Iterator, KeysView, List, MutableSequence, Optional, overload, Sequence, Text, Tuple, TypeVar, Union
3+
from typing import Any, Callable, Dict, Generator, IO, ItemsView, Iterable, Iterator, KeysView, List, MutableSequence, Optional, overload, Sequence, Text, Tuple, TypeVar, Union, overload
44
import io
55
import sys
66

@@ -53,8 +53,14 @@ class Element(MutableSequence[Element]):
5353
def extend(self, elements: Iterable[Element]) -> None: ...
5454
def find(self, path: _str_argument_type, namespaces: Optional[Dict[_str_argument_type, _str_argument_type]] = ...) -> Optional[Element]: ...
5555
def findall(self, path: _str_argument_type, namespaces: Optional[Dict[_str_argument_type, _str_argument_type]] = ...) -> List[Element]: ...
56-
def findtext(self, path: _str_argument_type, default: Optional[_T] = ..., namespaces: Optional[Dict[_str_argument_type, _str_argument_type]] = ...) -> Union[_T, _str_result_type]: ...
57-
def get(self, key: _str_argument_type, default: Optional[_T] = ...) -> Union[_str_result_type, _T]: ...
56+
@overload
57+
def findtext(self, path: _str_argument_type, namespaces: Optional[Dict[_str_argument_type, _str_argument_type]] = ...) -> Optional[_str_result_type]: ...
58+
@overload
59+
def findtext(self, path: _str_argument_type, default: _T, namespaces: Optional[Dict[_str_argument_type, _str_argument_type]] = ...) -> Union[_T, _str_result_type]: ...
60+
@overload
61+
def get(self, key: _str_argument_type) -> Optional[_str_result_type]: ...
62+
@overload
63+
def get(self, key: _str_argument_type, default: _T) -> Union[_str_result_type, _T]: ...
5864
def getchildren(self) -> List[Element]: ...
5965
def getiterator(self, tag: Optional[_str_argument_type] = ...) -> List[Element]: ...
6066
if sys.version_info >= (3, 2):
@@ -102,7 +108,10 @@ class ElementTree:
102108
def iter(self, tag: Optional[_str_argument_type] = ...) -> Generator[Element, None, None]: ...
103109
def getiterator(self, tag: Optional[_str_argument_type] = ...) -> List[Element]: ...
104110
def find(self, path: _str_argument_type, namespaces: Optional[Dict[_str_argument_type, _str_argument_type]] = ...) -> Optional[Element]: ...
105-
def findtext(self, path: _str_argument_type, default: Optional[_T] = ..., namespaces: Optional[Dict[_str_argument_type, _str_argument_type]] = ...) -> Union[_T, _str_result_type]: ...
111+
@overload
112+
def findtext(self, path: _str_argument_type, namespaces: Optional[Dict[_str_argument_type, _str_argument_type]] = ...) -> Optional[_str_result_type]: ...
113+
@overload
114+
def findtext(self, path: _str_argument_type, default: _T, namespaces: Optional[Dict[_str_argument_type, _str_argument_type]] = ...) -> Union[_T, _str_result_type]: ...
106115
def findall(self, path: _str_argument_type, namespaces: Optional[Dict[_str_argument_type, _str_argument_type]] = ...) -> List[Element]: ...
107116
def iterfind(self, path: _str_argument_type, namespaces: Optional[Dict[_str_argument_type, _str_argument_type]] = ...) -> List[Element]: ...
108117
if sys.version_info >= (3, 4):

0 commit comments

Comments
 (0)