builtins: add a getattr overload for bool (#5518)

As brought up in https://github.com/python/typeshed/pull/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
This commit is contained in:
Shantanu
2021-05-29 07:09:38 -07:00
committed by GitHub
parent af376f4d1f
commit 04fb7ceb3c
3 changed files with 15 additions and 0 deletions

View File

@@ -1021,9 +1021,14 @@ class filter(Iterator[_T], Generic[_T]):
def format(__value: object, __format_spec: str = ...) -> str: ... # TODO unicode
@overload
def getattr(__o: Any, name: str) -> Any: ...
# While technically covered by the last overload, spelling out the types for None and bool
# help mypy out in some tricky situations involving type context (aka bidirectional inference)
@overload
def getattr(__o: Any, name: str, __default: None) -> Optional[Any]: ...
@overload
def getattr(__o: Any, name: str, __default: bool) -> Union[Any, bool]: ...
@overload
def getattr(__o: Any, name: str, __default: _T) -> Union[Any, _T]: ...
def globals() -> Dict[str, Any]: ...
def hasattr(__obj: Any, __name: str) -> bool: ...