diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index 56c5a4d28af9..f1c1b864f5c7 100644 --- a/stdlib/builtins.pyi +++ b/stdlib/builtins.pyi @@ -23,7 +23,7 @@ from _typeshed import ( SupportsWrite, ) from io import BufferedRandom, BufferedReader, BufferedWriter, FileIO, TextIOWrapper -from types import CodeType, TracebackType +from types import CodeType, TracebackType, _Cell from typing import ( IO, AbstractSet, @@ -764,13 +764,22 @@ class tuple(Sequence[_T_co], Generic[_T_co]): if sys.version_info >= (3, 9): def __class_getitem__(cls, __item: Any) -> GenericAlias: ... +# Make sure this class definition stays roughly in line with `types.FunctionType` +@final class function: # TODO not defined in builtins! - __name__: str - __module__: str + __closure__: tuple[_Cell, ...] | None __code__: CodeType + __defaults__: tuple[Any, ...] | None + __dict__: dict[str, Any] + __globals__: dict[str, Any] + __name__: str __qualname__: str __annotations__: dict[str, Any] + __kwdefaults__: dict[str, Any] + __module__: str + # mypy uses `builtins.function.__get__` to represent methods, properties, and getset_descriptors so we type the return as Any. + def __get__(self, obj: object | None, type: type | None = ...) -> Any: ... class list(MutableSequence[_T], Generic[_T]): @overload diff --git a/stdlib/types.pyi b/stdlib/types.pyi index 24c1f9fdb172..92f8daa7ebed 100644 --- a/stdlib/types.pyi +++ b/stdlib/types.pyi @@ -39,6 +39,7 @@ class _Cell: __hash__: None # type: ignore[assignment] cell_contents: Any +# Make sure this class definition stays roughly in line with `builtins.function` @final class FunctionType: __closure__: tuple[_Cell, ...] | None @@ -59,7 +60,7 @@ class FunctionType: closure: tuple[_Cell, ...] | None = ..., ) -> None: ... def __call__(self, *args: Any, **kwargs: Any) -> Any: ... - def __get__(self, obj: object | None, type: type | None) -> MethodType: ... + def __get__(self, obj: object | None, type: type | None = ...) -> MethodType: ... LambdaType = FunctionType diff --git a/tests/stubtest_allowlists/py3_common.txt b/tests/stubtest_allowlists/py3_common.txt index 08860038a9bc..fbeea97b546b 100644 --- a/tests/stubtest_allowlists/py3_common.txt +++ b/tests/stubtest_allowlists/py3_common.txt @@ -270,6 +270,9 @@ pydoc.Helper.symbols_ # Loop variable in class https://github.com/python/typesh pydoc.Helper.topic # Loop variable in class https://github.com/python/typeshed/issues/6401#issuecomment-981178522 # Dynamically specified by __getattr__, and thus don't exist on the class tempfile._TemporaryFileWrapper.[\w_]+ +# stubtest incorrectly highlights the type argument as not having a default value. +types.FunctionType.__get__ +types.LambdaType.__get__ # Various classes in typing aren't types at runtime. In addition, mypy thinks some special forms are tautologically defined. typing.[A-Z]\w+ typing_extensions\..*