Skip to content

Commit f08d72b

Browse files
committed
Add getattr overload variants to help mypy type inference
These silence errors about missing type annotations for calls like these: ``` x = getattr(o, 'a', []) y = getattr(o, 'b', {}) ``` This is basically a generalization of #5518 and other overloads we already have. This works around #11572. I encountered the issue in several places when testing recent typeshed against an internal repo. Manually cherry-picked from python/typeshed#6355.
1 parent 5733d29 commit f08d72b

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

mypy/typeshed/stdlib/@python2/__builtin__.pyi

+7-2
Original file line numberDiff line numberDiff line change
@@ -850,13 +850,18 @@ def format(__value: object, __format_spec: str = ...) -> str: ... # TODO unicod
850850
@overload
851851
def getattr(__o: Any, name: Text) -> Any: ...
852852

853-
# While technically covered by the last overload, spelling out the types for None and bool
854-
# help mypy out in some tricky situations involving type context (aka bidirectional inference)
853+
# While technically covered by the last overload, spelling out the types for None, bool
854+
# and basic containers help mypy out in some tricky situations involving type context
855+
# (aka bidirectional inference)
855856
@overload
856857
def getattr(__o: Any, name: Text, __default: None) -> Any | None: ...
857858
@overload
858859
def getattr(__o: Any, name: Text, __default: bool) -> Any | bool: ...
859860
@overload
861+
def getattr(__o: object, name: str, __default: list[Any]) -> Any | list[Any]: ...
862+
@overload
863+
def getattr(__o: object, name: str, __default: dict[Any, Any]) -> Any | dict[Any, Any]: ...
864+
@overload
860865
def getattr(__o: Any, name: Text, __default: _T) -> Any | _T: ...
861866
def globals() -> Dict[str, Any]: ...
862867
def hasattr(__obj: Any, __name: Text) -> bool: ...

mypy/typeshed/stdlib/@python2/builtins.pyi

+7-2
Original file line numberDiff line numberDiff line change
@@ -850,13 +850,18 @@ def format(__value: object, __format_spec: str = ...) -> str: ... # TODO unicod
850850
@overload
851851
def getattr(__o: Any, name: Text) -> Any: ...
852852

853-
# While technically covered by the last overload, spelling out the types for None and bool
854-
# help mypy out in some tricky situations involving type context (aka bidirectional inference)
853+
# While technically covered by the last overload, spelling out the types for None, bool
854+
# and basic containers help mypy out in some tricky situations involving type context
855+
# (aka bidirectional inference)
855856
@overload
856857
def getattr(__o: Any, name: Text, __default: None) -> Any | None: ...
857858
@overload
858859
def getattr(__o: Any, name: Text, __default: bool) -> Any | bool: ...
859860
@overload
861+
def getattr(__o: object, name: str, __default: list[Any]) -> Any | list[Any]: ...
862+
@overload
863+
def getattr(__o: object, name: str, __default: dict[Any, Any]) -> Any | dict[Any, Any]: ...
864+
@overload
860865
def getattr(__o: Any, name: Text, __default: _T) -> Any | _T: ...
861866
def globals() -> Dict[str, Any]: ...
862867
def hasattr(__obj: Any, __name: Text) -> bool: ...

mypy/typeshed/stdlib/builtins.pyi

+7-2
Original file line numberDiff line numberDiff line change
@@ -1023,13 +1023,18 @@ def format(__value: object, __format_spec: str = ...) -> str: ... # TODO unicod
10231023
@overload
10241024
def getattr(__o: object, name: str) -> Any: ...
10251025

1026-
# While technically covered by the last overload, spelling out the types for None and bool
1027-
# help mypy out in some tricky situations involving type context (aka bidirectional inference)
1026+
# While technically covered by the last overload, spelling out the types for None, bool
1027+
# and basic containers help mypy out in some tricky situations involving type context
1028+
# (aka bidirectional inference)
10281029
@overload
10291030
def getattr(__o: object, name: str, __default: None) -> Any | None: ...
10301031
@overload
10311032
def getattr(__o: object, name: str, __default: bool) -> Any | bool: ...
10321033
@overload
1034+
def getattr(__o: object, name: str, __default: list[Any]) -> Any | list[Any]: ...
1035+
@overload
1036+
def getattr(__o: object, name: str, __default: dict[Any, Any]) -> Any | dict[Any, Any]: ...
1037+
@overload
10331038
def getattr(__o: object, name: str, __default: _T) -> Any | _T: ...
10341039
def globals() -> dict[str, Any]: ...
10351040
def hasattr(__obj: object, __name: str) -> bool: ...

0 commit comments

Comments
 (0)