Skip to content

Commit 04fb7ce

Browse files
authored
builtins: add a getattr overload for bool (#5518)
As brought up in #5516 Alternatives include: - Use another type var that has a value restriction - Doing something fancy with Protocols that have a __bool__ that returns a Literal (which may not work) - Doing nothing
1 parent af376f4 commit 04fb7ce

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

stdlib/@python2/__builtin__.pyi

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -863,9 +863,14 @@ def filter(__function: Callable[[_T], Any], __iterable: Iterable[_T]) -> List[_T
863863
def format(__value: object, __format_spec: str = ...) -> str: ... # TODO unicode
864864
@overload
865865
def getattr(__o: Any, name: Text) -> Any: ...
866+
867+
# While technically covered by the last overload, spelling out the types for None and bool
868+
# help mypy out in some tricky situations involving type context (aka bidirectional inference)
866869
@overload
867870
def getattr(__o: Any, name: Text, __default: None) -> Optional[Any]: ...
868871
@overload
872+
def getattr(__o: Any, name: Text, __default: bool) -> Union[Any, bool]: ...
873+
@overload
869874
def getattr(__o: Any, name: Text, __default: _T) -> Union[Any, _T]: ...
870875
def globals() -> Dict[str, Any]: ...
871876
def hasattr(__obj: Any, __name: Text) -> bool: ...

stdlib/@python2/builtins.pyi

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -863,9 +863,14 @@ def filter(__function: Callable[[_T], Any], __iterable: Iterable[_T]) -> List[_T
863863
def format(__value: object, __format_spec: str = ...) -> str: ... # TODO unicode
864864
@overload
865865
def getattr(__o: Any, name: Text) -> Any: ...
866+
867+
# While technically covered by the last overload, spelling out the types for None and bool
868+
# help mypy out in some tricky situations involving type context (aka bidirectional inference)
866869
@overload
867870
def getattr(__o: Any, name: Text, __default: None) -> Optional[Any]: ...
868871
@overload
872+
def getattr(__o: Any, name: Text, __default: bool) -> Union[Any, bool]: ...
873+
@overload
869874
def getattr(__o: Any, name: Text, __default: _T) -> Union[Any, _T]: ...
870875
def globals() -> Dict[str, Any]: ...
871876
def hasattr(__obj: Any, __name: Text) -> bool: ...

stdlib/builtins.pyi

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,9 +1021,14 @@ class filter(Iterator[_T], Generic[_T]):
10211021
def format(__value: object, __format_spec: str = ...) -> str: ... # TODO unicode
10221022
@overload
10231023
def getattr(__o: Any, name: str) -> Any: ...
1024+
1025+
# While technically covered by the last overload, spelling out the types for None and bool
1026+
# help mypy out in some tricky situations involving type context (aka bidirectional inference)
10241027
@overload
10251028
def getattr(__o: Any, name: str, __default: None) -> Optional[Any]: ...
10261029
@overload
1030+
def getattr(__o: Any, name: str, __default: bool) -> Union[Any, bool]: ...
1031+
@overload
10271032
def getattr(__o: Any, name: str, __default: _T) -> Union[Any, _T]: ...
10281033
def globals() -> Dict[str, Any]: ...
10291034
def hasattr(__obj: Any, __name: str) -> bool: ...

0 commit comments

Comments
 (0)