From 8d56328a2447b7f1fee9baf45af8db3df6163b5b Mon Sep 17 00:00:00 2001 From: Neil Vyas Date: Fri, 21 May 2021 18:44:11 -0500 Subject: [PATCH] change getattr stubs to better type the default argument and return type (#5516) Co-authored-by: Shantanu <12621235+hauntsaninja@users.noreply.github.com> Co-authored-by: Akuli --- stdlib/@python2/__builtin__.pyi | 7 ++++++- stdlib/@python2/builtins.pyi | 7 ++++++- stdlib/builtins.pyi | 7 ++++++- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/stdlib/@python2/__builtin__.pyi b/stdlib/@python2/__builtin__.pyi index 4eb5424d8..b7b68bbb7 100644 --- a/stdlib/@python2/__builtin__.pyi +++ b/stdlib/@python2/__builtin__.pyi @@ -861,7 +861,12 @@ def filter(__function: None, __iterable: Iterable[Optional[_T]]) -> List[_T]: .. @overload def filter(__function: Callable[[_T], Any], __iterable: Iterable[_T]) -> List[_T]: ... def format(__value: object, __format_spec: str = ...) -> str: ... # TODO unicode -def getattr(__o: Any, name: Text, __default: Any = ...) -> Any: ... +@overload +def getattr(__o: Any, name: Text) -> Any: ... +@overload +def getattr(__o: Any, name: Text, __default: None) -> Optional[Any]: ... +@overload +def getattr(__o: Any, name: Text, __default: _T) -> Union[Any, _T]: ... def globals() -> Dict[str, Any]: ... def hasattr(__obj: Any, __name: Text) -> bool: ... def hash(__obj: object) -> int: ... diff --git a/stdlib/@python2/builtins.pyi b/stdlib/@python2/builtins.pyi index 4eb5424d8..b7b68bbb7 100644 --- a/stdlib/@python2/builtins.pyi +++ b/stdlib/@python2/builtins.pyi @@ -861,7 +861,12 @@ def filter(__function: None, __iterable: Iterable[Optional[_T]]) -> List[_T]: .. @overload def filter(__function: Callable[[_T], Any], __iterable: Iterable[_T]) -> List[_T]: ... def format(__value: object, __format_spec: str = ...) -> str: ... # TODO unicode -def getattr(__o: Any, name: Text, __default: Any = ...) -> Any: ... +@overload +def getattr(__o: Any, name: Text) -> Any: ... +@overload +def getattr(__o: Any, name: Text, __default: None) -> Optional[Any]: ... +@overload +def getattr(__o: Any, name: Text, __default: _T) -> Union[Any, _T]: ... def globals() -> Dict[str, Any]: ... def hasattr(__obj: Any, __name: Text) -> bool: ... def hash(__obj: object) -> int: ... diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index 1710de35d..865c6e9a1 100644 --- a/stdlib/builtins.pyi +++ b/stdlib/builtins.pyi @@ -1019,7 +1019,12 @@ class filter(Iterator[_T], Generic[_T]): def __next__(self) -> _T: ... def format(__value: object, __format_spec: str = ...) -> str: ... # TODO unicode -def getattr(__o: Any, name: str, __default: Any = ...) -> Any: ... +@overload +def getattr(__o: Any, name: str) -> Any: ... +@overload +def getattr(__o: Any, name: str, __default: None) -> Optional[Any]: ... +@overload +def getattr(__o: Any, name: str, __default: _T) -> Union[Any, _T]: ... def globals() -> Dict[str, Any]: ... def hasattr(__obj: Any, __name: str) -> bool: ... def hash(__obj: object) -> int: ...