From 56682f9d19d1c57d5ca9a01fad5f8b4f954e3e7b Mon Sep 17 00:00:00 2001 From: jpy-git Date: Mon, 3 Jan 2022 22:45:20 +0000 Subject: [PATCH 01/10] Add missing function attributes to builtins.function --- stdlib/builtins.pyi | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index 56c5a4d28af9..359cfe2e7c1f 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, @@ -766,11 +766,16 @@ class tuple(Sequence[_T_co], Generic[_T_co]): 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 class list(MutableSequence[_T], Generic[_T]): @overload From 98310262eb52bc476d1e05972ba785012a3e56f5 Mon Sep 17 00:00:00 2001 From: jpy-git Date: Mon, 3 Jan 2022 23:03:08 +0000 Subject: [PATCH 02/10] Add @final and __get__ --- stdlib/builtins.pyi | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index 359cfe2e7c1f..46998d63982c 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, _Cell +from types import CodeType, MethodType, TracebackType, _Cell from typing import ( IO, AbstractSet, @@ -764,6 +764,7 @@ class tuple(Sequence[_T_co], Generic[_T_co]): if sys.version_info >= (3, 9): def __class_getitem__(cls, __item: Any) -> GenericAlias: ... +@final class function: # TODO not defined in builtins! __closure__: tuple[_Cell, ...] | None @@ -776,6 +777,7 @@ class function: __annotations__: dict[str, Any] __kwdefaults__: dict[str, Any] __module__: str + def __get__(self, obj: object | None, type: type | None) -> MethodType: ... class list(MutableSequence[_T], Generic[_T]): @overload From 552cb40482b0fa7a182f3b8886e3c6ccb20e841b Mon Sep 17 00:00:00 2001 From: Joseph Young <80432516+jpy-git@users.noreply.github.com> Date: Mon, 3 Jan 2022 23:25:00 +0000 Subject: [PATCH 03/10] Update stdlib/builtins.pyi Co-authored-by: Alex Waygood --- stdlib/builtins.pyi | 1 + 1 file changed, 1 insertion(+) diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index 46998d63982c..08599557f297 100644 --- a/stdlib/builtins.pyi +++ b/stdlib/builtins.pyi @@ -764,6 +764,7 @@ 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! From 971d2c379189e97ca0a4353ac9382941a9e992b5 Mon Sep 17 00:00:00 2001 From: Joseph Young <80432516+jpy-git@users.noreply.github.com> Date: Mon, 3 Jan 2022 23:25:06 +0000 Subject: [PATCH 04/10] Update stdlib/builtins.pyi Co-authored-by: Alex Waygood --- stdlib/builtins.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index 08599557f297..b0549bf88c3e 100644 --- a/stdlib/builtins.pyi +++ b/stdlib/builtins.pyi @@ -778,7 +778,7 @@ class function: __annotations__: dict[str, Any] __kwdefaults__: dict[str, Any] __module__: str - def __get__(self, obj: object | None, type: type | None) -> MethodType: ... + def __get__(self, obj: object | None, type: type | None = ...) -> MethodType: ... class list(MutableSequence[_T], Generic[_T]): @overload From 527825823d978e47e518786e6d8eb56691492368 Mon Sep 17 00:00:00 2001 From: jpy-git Date: Mon, 3 Jan 2022 23:26:44 +0000 Subject: [PATCH 05/10] fix types.FunctionType.__get__ signature --- stdlib/types.pyi | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 From a1ab18f7750520fb84ec764c5f9eac6bd5bcb164 Mon Sep 17 00:00:00 2001 From: jpy-git Date: Mon, 3 Jan 2022 23:40:22 +0000 Subject: [PATCH 06/10] Add types.{FunctionType,LambdaType}.__get__ to stubtest_allowlist --- tests/stubtest_allowlists/py3_common.txt | 3 +++ 1 file changed, 3 insertions(+) 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\..* From 2cf7490964d116ce1df10639c1f25880bdb600f0 Mon Sep 17 00:00:00 2001 From: jpy-git Date: Tue, 4 Jan 2022 09:46:01 +0000 Subject: [PATCH 07/10] __get__ returns Any --- stdlib/builtins.pyi | 3 ++- stdlib/types.pyi | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index b0549bf88c3e..4b0b2b13cc30 100644 --- a/stdlib/builtins.pyi +++ b/stdlib/builtins.pyi @@ -778,7 +778,8 @@ class function: __annotations__: dict[str, Any] __kwdefaults__: dict[str, Any] __module__: str - def __get__(self, obj: object | None, type: type | None = ...) -> MethodType: ... + # __get__ can return either a method or a property so type 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 92f8daa7ebed..2217e66e1380 100644 --- a/stdlib/types.pyi +++ b/stdlib/types.pyi @@ -60,7 +60,8 @@ 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: ... + # __get__ can return either a method or a property so type as Any. + def __get__(self, obj: object | None, type: type | None = ...) -> Any: ... LambdaType = FunctionType From 16432e542f907f6dddcab0b37eda093fcc62bf92 Mon Sep 17 00:00:00 2001 From: Joseph Young <80432516+jpy-git@users.noreply.github.com> Date: Tue, 4 Jan 2022 09:49:14 +0000 Subject: [PATCH 08/10] Update stdlib/builtins.pyi Co-authored-by: Alex Waygood --- stdlib/builtins.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index 4b0b2b13cc30..3e12ff1c1ef7 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, MethodType, TracebackType, _Cell +from types import CodeType, TracebackType, _Cell from typing import ( IO, AbstractSet, From 27042d68aff7f10ab1f5891cb2670915f2f322cd Mon Sep 17 00:00:00 2001 From: Joseph Young <80432516+jpy-git@users.noreply.github.com> Date: Tue, 4 Jan 2022 16:33:52 +0000 Subject: [PATCH 09/10] Update types.pyi --- stdlib/types.pyi | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/stdlib/types.pyi b/stdlib/types.pyi index 2217e66e1380..92f8daa7ebed 100644 --- a/stdlib/types.pyi +++ b/stdlib/types.pyi @@ -60,8 +60,7 @@ class FunctionType: closure: tuple[_Cell, ...] | None = ..., ) -> None: ... def __call__(self, *args: Any, **kwargs: Any) -> Any: ... - # __get__ can return either a method or a property so type as Any. - def __get__(self, obj: object | None, type: type | None = ...) -> Any: ... + def __get__(self, obj: object | None, type: type | None = ...) -> MethodType: ... LambdaType = FunctionType From 8c715c6b62e53ee36907c608645f568c1cf4f699 Mon Sep 17 00:00:00 2001 From: Joseph Young <80432516+jpy-git@users.noreply.github.com> Date: Tue, 4 Jan 2022 16:36:37 +0000 Subject: [PATCH 10/10] Update stdlib/builtins.pyi --- stdlib/builtins.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index 3e12ff1c1ef7..f1c1b864f5c7 100644 --- a/stdlib/builtins.pyi +++ b/stdlib/builtins.pyi @@ -778,7 +778,7 @@ class function: __annotations__: dict[str, Any] __kwdefaults__: dict[str, Any] __module__: str - # __get__ can return either a method or a property so type as Any. + # 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]):