Skip to content

Commit 88f86a0

Browse files
authored
Reduce use of Any in builtins (#6292)
1 parent 34c91be commit 88f86a0

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

stdlib/builtins.pyi

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,11 @@ class object:
109109
def __getattribute__(self, __name: str) -> Any: ...
110110
def __delattr__(self, __name: str) -> None: ...
111111
def __sizeof__(self) -> int: ...
112-
def __reduce__(self) -> str | Tuple[Any, ...]: ...
112+
def __reduce__(self) -> str | Tuple[object, ...]: ...
113113
if sys.version_info >= (3, 8):
114-
def __reduce_ex__(self, __protocol: SupportsIndex) -> str | Tuple[Any, ...]: ...
114+
def __reduce_ex__(self, __protocol: SupportsIndex) -> str | Tuple[object, ...]: ...
115115
else:
116-
def __reduce_ex__(self, __protocol: int) -> str | Tuple[Any, ...]: ...
116+
def __reduce_ex__(self, __protocol: int) -> str | Tuple[object, ...]: ...
117117
def __dir__(self) -> Iterable[str]: ...
118118
def __init_subclass__(cls) -> None: ...
119119

@@ -170,7 +170,7 @@ class type(object):
170170
def __instancecheck__(self, __instance: Any) -> bool: ...
171171
def __subclasscheck__(self, __subclass: type) -> bool: ...
172172
@classmethod
173-
def __prepare__(metacls, __name: str, __bases: Tuple[type, ...], **kwds: Any) -> Mapping[str, Any]: ...
173+
def __prepare__(metacls, __name: str, __bases: Tuple[type, ...], **kwds: Any) -> Mapping[str, object]: ...
174174
if sys.version_info >= (3, 10):
175175
def __or__(self, __t: Any) -> types.UnionType: ...
176176
def __ror__(self, __t: Any) -> types.UnionType: ...
@@ -1013,6 +1013,7 @@ if sys.version_info >= (3, 10):
10131013
@overload
10141014
async def anext(__i: SupportsAnext[_T], default: _VT) -> _T | _VT: ...
10151015

1016+
# TODO: `compile` has a more precise return type in reality; work on a way of expressing that?
10161017
if sys.version_info >= (3, 8):
10171018
def compile(
10181019
source: str | bytes | AST,
@@ -1037,18 +1038,23 @@ else:
10371038

10381039
def copyright() -> None: ...
10391040
def credits() -> None: ...
1040-
def delattr(__obj: Any, __name: str) -> None: ...
1041+
def delattr(__obj: object, __name: str) -> None: ...
10411042
def dir(__o: object = ...) -> list[str]: ...
10421043
@overload
10431044
def divmod(__x: SupportsDivMod[_T_contra, _T_co], __y: _T_contra) -> _T_co: ...
10441045
@overload
10451046
def divmod(__x: _T_contra, __y: SupportsRDivMod[_T_contra, _T_co]) -> _T_co: ...
1047+
1048+
# The `globals` argument to `eval` has to be `dict[str, Any]` rather than `dict[str, object]` due to invariance.
1049+
# (The `globals` argument has to be a "real dict", rather than any old mapping, unlike the `locals` argument.)
10461050
def eval(
1047-
__source: str | bytes | CodeType, __globals: dict[str, Any] | None = ..., __locals: Mapping[str, Any] | None = ...
1051+
__source: str | bytes | CodeType, __globals: dict[str, Any] | None = ..., __locals: Mapping[str, object] | None = ...
10481052
) -> Any: ...
1053+
1054+
# Comment above regarding `eval` applies to `exec` as well
10491055
def exec(
1050-
__source: str | bytes | CodeType, __globals: dict[str, Any] | None = ..., __locals: Mapping[str, Any] | None = ...
1051-
) -> Any: ...
1056+
__source: str | bytes | CodeType, __globals: dict[str, Any] | None = ..., __locals: Mapping[str, object] | None = ...
1057+
) -> None: ...
10521058
def exit(code: object = ...) -> NoReturn: ...
10531059

10541060
class filter(Iterator[_T], Generic[_T]):
@@ -1077,14 +1083,15 @@ def hash(__obj: object) -> int: ...
10771083
def help(*args: Any, **kwds: Any) -> None: ...
10781084
def hex(__number: int | SupportsIndex) -> str: ...
10791085
def id(__obj: object) -> int: ...
1080-
def input(__prompt: Any = ...) -> str: ...
1086+
def input(__prompt: object = ...) -> str: ...
10811087
@overload
10821088
def iter(__iterable: Iterable[_T]) -> Iterator[_T]: ...
10831089
@overload
10841090
def iter(__function: Callable[[], _T | None], __sentinel: None) -> Iterator[_T]: ...
10851091
@overload
1086-
def iter(__function: Callable[[], _T], __sentinel: Any) -> Iterator[_T]: ...
1092+
def iter(__function: Callable[[], _T], __sentinel: object) -> Iterator[_T]: ...
10871093

1094+
# We need recursive types to express the type of the second argument to `isinstance` properly, hence the use of `Any`
10881095
if sys.version_info >= (3, 10):
10891096
def isinstance(
10901097
__obj: object, __class_or_tuple: type | types.UnionType | Tuple[type | types.UnionType | Tuple[Any, ...], ...]
@@ -1334,6 +1341,9 @@ def round(number: SupportsRound[Any]) -> int: ...
13341341
def round(number: SupportsRound[Any], ndigits: None) -> int: ...
13351342
@overload
13361343
def round(number: SupportsRound[_T], ndigits: SupportsIndex) -> _T: ...
1344+
1345+
# See https://github.com/python/typeshed/pull/6292#discussion_r748875189
1346+
# for why arg 3 of `setattr` should be annotated with `Any` and not `object`
13371347
def setattr(__obj: object, __name: str, __value: Any) -> None: ...
13381348
@overload
13391349
def sorted(__iterable: Iterable[SupportsLessThanT], *, key: None = ..., reverse: bool = ...) -> list[SupportsLessThanT]: ...
@@ -1352,6 +1362,8 @@ else:
13521362
@overload
13531363
def sum(__iterable: Iterable[_T], __start: _S) -> _T | _S: ...
13541364

1365+
# The argument to `vars()` has to have a `__dict__` attribute, so can't be annotated with `object`
1366+
# (A "SupportsDunderDict" protocol doesn't work)
13551367
def vars(__object: Any = ...) -> dict[str, Any]: ...
13561368

13571369
class zip(Iterator[_T_co], Generic[_T_co]):
@@ -1433,8 +1445,8 @@ class zip(Iterator[_T_co], Generic[_T_co]):
14331445

14341446
def __import__(
14351447
name: str,
1436-
globals: Mapping[str, Any] | None = ...,
1437-
locals: Mapping[str, Any] | None = ...,
1448+
globals: Mapping[str, object] | None = ...,
1449+
locals: Mapping[str, object] | None = ...,
14381450
fromlist: Sequence[str] = ...,
14391451
level: int = ...,
14401452
) -> Any: ...

0 commit comments

Comments
 (0)